facarroll Posted June 17, 2011 Share Posted June 17, 2011 I can make an array and create a variable in the array like this. <?php $query1 = mysql_query("SELECT quizTitle FROM quiz ORDER BY quizTitle ASC"); while ($row = mysql_fetch_array($query1)) { $quizTitle = "{$row[quizTitle]}"; $_SESSION['quizTitle'] = $quizTitle; echo "{$row['quizTitle']} <br />\n"; } If I echo the variable in each row of the array it looks great. The new quiz Title is displayed differently in each row. <?php echo $quizTitle; ?> The problem is that when I create a local variable from the session variable into the next required script like this $quizTitle = $_SESSION['quizTitle']; it just creates a variable based on the last row of the array. How can I retain the value of the variable as it is created in each row of the arrar, turn it into a session variable, then a local variable in the next called file, and retain the value relative to the selected row of the array? I want to be able to use the variable like this. <?php //Result is a block of HTML $sqlchk = "SELECT Result FROM quiz WHERE quizTitle = '$quizTitle'"; $rschk = mysql_query($sqlchk, $conn); $rowchk = mysql_fetch_array($rschk, MYSQL_ASSOC); echo (stripslashes($rowchk['Result'])); ?> hELP APPRECIATED. Quote Link to comment Share on other sites More sharing options...
Nodral Posted June 17, 2011 Share Posted June 17, 2011 This is because within the while loop you are just writing over the existing variable each time it loops. It may be worth having an array such as $_SESSION['quiztitle'][]=$quizTitle. This will create a new entry into an array for you which you can reference as you please. Quote Link to comment Share on other sites More sharing options...
facarroll Posted June 17, 2011 Author Share Posted June 17, 2011 I'm afraid I don't understand. I inserted your code but there was no difference. How should I reference it? Quote Link to comment Share on other sites More sharing options...
Nodral Posted June 17, 2011 Share Posted June 17, 2011 ok, imagine you are going through your while loop one step at a time. 1st result from query is quiz1, therefore within the loop $_SESSION['quizTitle'] = "quiz1" echo $_SESSION['quizTitle'] // gives quiz1 2nd result from query is quiz2, therefore within the loop $_SESSION['quizTitle'] = "quiz2" echo $_SESSION['quizTitle'] // gives quiz (replacing previous value of $_SESSION['quizTitle'] . . . . And so on every time you loop. It will simply replace the previous value. If you create an array as I descriped, you can use a foreach to get the individual values back out. <?php foreach ($_SESSION['quiztitle'] as $value){ //Result is a block of HTML $sqlchk = "SELECT Result FROM quiz WHERE quizTitle = '$value'"; $rschk = mysql_query($sqlchk, $conn); $rowchk = mysql_fetch_array($rschk, MYSQL_ASSOC); echo (stripslashes($rowchk['Result']));?> Quote Link to comment Share on other sites More sharing options...
facarroll Posted June 17, 2011 Author Share Posted June 17, 2011 OK. Maybe I should post all of the two scripts. The first script displays a pass or fail icon depending on whether the value of passState is 1 or 0. If a student failed the quiz the page also displays a link to check.php so that the student can view their quiz attempt as a block of HTML named Result in the database. I need to create a session variable at this point to use as a local variable in check.php which is shown further down here. As you correctly said, the variable is being overwritten each row of the array, so that I can display Result for only the final row. <?php $query1 = mysql_query("SELECT DISTINCT quizTitle, userId, passState, DATE_FORMAT(userDate,'%b %e, %Y') AS userDate FROM quiz WHERE managerId = '$managerId' AND userId = '$userId' ORDER BY quizTitle ASC, passState DESC, userDate DESC "); while ($row = mysql_fetch_array($query1)) { $quizTitle = "{$row[quizTitle]}"; $_SESSION['quiztitle'][]=$quizTitle; echo "{$row['quizTitle']} <br />\n"; echo "{$row['userDate']} <br />\n"; if ("{$row['passState']}" == 1) {echo "<img src='good.png' /><br />\n";} if ("{$row['passState']}" == 0) {echo "<img src='not_good.png' /><br />\n";} if ("{$row['passState']}" == 0) {echo "<a href='check.php'>Check your answers.</a><br />\n";} } ?> Here's the relevant code for check.php. <?php session_start(); // Put session variables into local variables // some other session variables converted to local variables $quizTitle = $_SESSION['quizTitle']; $sqlchk = "SELECT Result FROM quiz WHERE managerId = '$managerId' AND userIdRec = '$userIdRec' AND quizTitle = '$quizTitle'"; $rschk = mysql_query($sqlchk); $rowchk = mysql_fetch_array($rschk, MYSQL_ASSOC); echo (stripslashes($rowchk['Result']));} ?> The code is good but the transfer of values in the variable is wrong as it always returns the Result for the final quizTitle in the array, but no others. This code is obviously abbreviated to the necessary content. I appreciate any help. Quote Link to comment Share on other sites More sharing options...
iblood Posted June 17, 2011 Share Posted June 17, 2011 I think you missed Nodral's point here ok, imagine you are going through your while loop one step at a time. 1st result from query is quiz1, therefore within the loop $_SESSION['quizTitle'] = "quiz1" echo $_SESSION['quizTitle'] // gives quiz1 2nd result from query is quiz2, therefore within the loop $_SESSION['quizTitle'] = "quiz2" echo $_SESSION['quizTitle'] // gives quiz (replacing previous value of $_SESSION['quizTitle'] . . . . And so on every time you loop. It will simply replace the previous value. At the end of this script, $_SESSION will always have the value of the last record.. Regarding your implementation, I would recommend to just use $_GET instead of $_SESSION. something like: //if ("{$row['passState']}" == 0) {echo "<a href='check.php'>Check your answers.</a><br />\n";} if ("{$row['passState']}" == 0) {echo "<a href='check.php?quiztitle=". urlencode($quizTitle) ."'>Check your answers.</a><br />\n";} and in your check.php: //$quizTitle = $_SESSION['quizTitle']; $quizTitle = mysql_real_escape_string($_GET['quizTitle']); Quote Link to comment Share on other sites More sharing options...
facarroll Posted June 17, 2011 Author Share Posted June 17, 2011 It's past my bedtime here. I'll look at this in the morning and then get back to you. Thanks for your help. Quote Link to comment Share on other sites More sharing options...
facarroll Posted June 18, 2011 Author Share Posted June 18, 2011 Thanks all. It took me a while as I've never used $_GET before, but all done. Many thanks. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.