1

I am calling back the results for AnswerStatusID and AnswerResponse and need to apply them to a variable to compare against to see if the answer given is correct or incorrect and the corresponding response for the answer, my issue is that my variable is only being populated by the last row in the table instead of populating it with all of the data.

// Connect to the Database
require_once('mysqli_connect.php');

//create the query for the question
$q = "SELECT `Question` FROM tbl_Question WHERE QuestionID = 1";

//Create the query for the Answers
$q2 = "SELECT `Answer`,`AnswerStatusID`,`AnswerResponse` FROM tbl_Answer WHERE QuestionID = 1";


//Run the query
$r = mysqli_query($conn,$q);

//run the answer query
$r2 = mysqli_query($conn,$q2);


while($row = mysqli_fetch_array($r,MYSQLI_ASSOC)){
echo '<div id="Question1"><p>1) ' . $row['Question'] . '</div></p>';
}



while($row2 = mysqli_fetch_array($r2,MYSQLI_ASSOC)){
echo '<div id="Question1"><input name="q1" type="radio" value="'.$AnswerStatusID.'"/>' . $row2['Answer'] . '</div><br/>';

//Assign the AnswerStatusID to a var
$AnswerStatusID = $row2['AnswerStatusID'];

//Assign the AnswerResponse to a var
$AnswerResponse = $row2['AnswerResponse'];
}
3
  • So, do you want $AnswerStatusID and $AnswerResponse to be arrays of all the StatusIDs and Responses? Commented Jul 6, 2012 at 14:42
  • your variables are being set to all rows, but because you are using a scaler, all but the last get overwritten by the last. Commented Jul 6, 2012 at 14:44
  • @Wiseguy yes that is exactly what I want Commented Jul 6, 2012 at 14:49

1 Answer 1

2

As Wiseguy intimated, it looks like you want AnswerStatusID and AnswerResponse as arrays.

Two steps here. First declare them as arrays.

$AnswerResponse = array();
$AnswerStatusID = array();

Then store the values in each of them

while($row2 = mysqli_fetch_array($r2,MYSQLI_ASSOC)){
    echo '<div id="Question1"><input name="q1" type="radio" value="'.$AnswerStatusID.'"/>' . $row2['Answer'] . '</div><br/>';

    //Assign the AnswerStatusID to a var
    $AnswerStatusID[] = $row2['AnswerStatusID'];

    //Assign the AnswerResponse to a var
    $AnswerResponse[] = $row2['AnswerResponse'];
}

You can then see what's in the arrays using var_dump();

var_dump($AnswerResponse)

or

print_r($AnswerResponse)
Sign up to request clarification or add additional context in comments.

4 Comments

I tried that and when I echoed it to see the results and ensure that the variable was populated the echo was "array" and not any data
echo '<p>' .$AnswerResponse . '</p>'; echo '<p>' .$AnswerStatusID . '</p>';
That's a good thing! You can use var_dump($AnswerResponse) or echo print_r($AnswerResponse) to see what's in the arrays.
oh so that behavior is what SHOULD happen if the array is populated? I thought is would echo all the results with what I tried

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.