I have two related 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 a quiz the row also displays a link to check.php so that the student can view their quiz attempt in 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. The problem I have is that 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.

Recommended Answers

All 4 Replies

Member Avatar for diafol
$_SESSION['quiztitle'][]=$quizTitle;

This creates an array - as you want, I assume.

$quizTitle  = $_SESSION['quizTitle'];
        $sqlchk = "SELECT Result FROM quiz WHERE managerId = '$managerId' AND userIdRec = '$userIdRec' AND quizTitle = '$quizTitle'";
        $rschk = mysql_query($sqlchk);

$quizTitle is an array. So $quizTitle should mess up your query big time.

I know it's wrong. I just don't know how to fix it.

Member Avatar for diafol

Here's what I don't understand.
$_SESSION is an array of a number of quiztitles (from your loop) in the first piece of code.

You use $_SESSION in the second piece as if it were a single value. What are you trying to achieve? If you just want one quiztitle to check, why are you getting a whole lot of quiztitles in the first place?

I have solved the problem by using $_GET to get a variable on the page check.php. The thing I was trying to do was to use the string in the column quizTitle, say quiz_1 or quiz_2 or quiz_100. The quizTitle can be repeated in the database because a student can have a correct and an incorrect result.
I did this after the SELECT.

<?php $quizTitle = "{$row[quizTitle]}"; $_GET['quiztitle']=$quizTitle; echo "{$row['quizTitle']} <br />\n"; ?>

then

<?php if ("{$row['passState']}" == 0) {echo "<a href='check/check.php?quizTitle=". urlencode($quizTitle) ."'><font color='#0000FF'>Check your answers.</font></a><br />\n";} ?>

Then when check.php is called

$quizTitle = mysql_real_escape_string($_GET['quizTitle']);

and then I can use SELECT containing the variable $quizTitle derived from the earlier page.
Thanks for your help though, because it made me think of a new solution.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.