Jump to content

Create a variable in an array


facarroll

Recommended Posts

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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']));?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

I think you missed Nodral's point here :D

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']); 

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.