Hey Guys.
Can anyone see why this short script isn't working. Its a basic bit of programming to record a users answers in a multiple choice quiz. Each answer should be stored in the same array which should be passed through the 10 questions. I have written a check to see if the answers have been added to the array but the only thing that seems to be in the array is the username (posted from a previous page). Anyway any help or tips would be greatfully recieved. :)
Cheers.

Whoops i nearly forgot the code....

<?php
$qid = $_POST[qNum];
$name = $_POST[tutname];
$My_Array[0] = $name;


require_once('db_login.php');
$query="select * from question where Q_id = \"$qid\"";


$result = $db->query($query);
if (DB::isError($result)) die($result->getMessage());


$count = $qid + 1;


if ($row = $result->fetchRow())
{
if (DB::isError($row)) die($result->getMessage());
foreach($My_Array as $value)
{
echo "$value\n";
}
echo "<center><img src = \"$row[6]\"></center><br />";
echo "<center>$row[1]</center>";
echo "
<center>
<form action=\"$PHP_SELF\" name=\"Q1\" method=\"post\">
<input type=\"radio\" name=\"question\" value=\"1\">$row[2]<br />
<input type=\"radio\" name=\"question\" value=\"2\">$row[3]<br />
<input type=\"radio\" name=\"question\" value=\"3\">$row[4]<br />
<input type=\"radio\" name=\"question\" value=\"4\">$row[5]<br />
<br />
<input type=\"hidden\" name=\"qNum\" value=\"$count\">
<input type=\"hidden\" name=\"tutname\" value=\"$name\">
<input type=\"submit\" value=\"Next\" />
</form></center>";
$My_Array[] = $question;
}
?>

Cheers.

Try this:

<input type=\"radio\" name=\"question[]\" value=\"1\">$row[2]<br />
<input type=\"radio\" name=\"question[]\" value=\"2\">$row[3]<br />
<input type=\"radio\" name=\"question[]\" value=\"3\">$row[4]<br />
<input type=\"radio\" name=\"question[]\" value=\"4\">$row[5]<br />

You also may need to add this somewhere:

$My_Array[] = $_POST['question[]'];

or maybe just:

$My_Array[] = $_POST['question'];

can't remember lol.

Cheers for the help "DanceInstructor".
It still doesn't work tho! :sad: I think i am getting closer and i appreciate the help. It now passes something to the array, which is a start, but it says "array" when i print it out, e.g

Username array

However every time i wish to add another element to the end of the array it replaces the last element so i only ever have two elements in the array at best. Confusing? Yep!
Here is the amended code:

<?php
$qid = $_POST[qNum];
$name = $_POST[tutname];
$My_Array[0] = $name;
$My_Array[] = $_POST'];

require_once('db_login.php');
$query="select * from question where Q_id = \"$qid\"";

$result = $db->query($query);
if (DB::isError($result)) die($result->getMessage());

$count = $qid + 1;

if ($row = $result->fetchRow())
{
if (DB::isError($row)) die($result->getMessage());
foreach($My_Array as $value)
{
echo "$value\n";
}
echo "<center><img src = \"$row[6]\"></center><br />";
echo "<center>$row[1]</center>";
echo "
<center>
<form action=\"$PHP_SELF\" name=\"Q1\" method=\"post\">
<input type=\"radio\" name=\"question[]\" value=\"1\">$row[2]<br />
<input type=\"radio\" name=\"question[]\" value=\"2\">$row[3]<br />
<input type=\"radio\" name=\"question[]\" value=\"3\">$row[4]<br />
<input type=\"radio\" name=\"question[]\" value=\"4\">$row[5]<br />
<br />
<input type=\"hidden\" name=\"qNum\" value=\"$count\">
<input type=\"hidden\" name=\"tutname\" value=\"$name\">
<input type=\"submit\" value=\"Next\" />
</form></center>";

}
?>

Any more clues would be great!
Cheers.

Try this to see what is in your array:

echo('<pre>');
print_r($My_Array);
echo('</pre>');

Try this to see what is in your array:

echo('<pre>');
print_r($My_Array);
echo('</pre>');
<?
foreach ($My_Array as $some_value); 
echo $some_value;
?>

would work also right?

Unless $some_value is an array itself....

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.