Click to See Complete Forum and Search --> : mystery parse error!


PunkSktBrdr01
12-08-2003, 07:00 PM
I'm getting a parse error:


Parse error: parse error, expecting `','' or `';'' in /home/radioact/public_html/brandon/tests.php on line 17


and I can't figure out why. It says I'm missing a semicolon, but there is a semicolon there! Here's the problematic code:


function parseTest($testname) {
$fname = "/brandon/tests/" . $testname . ".txt";
if (file_exists($fname)) {
global $test = array(); // line 17
global $title = "";
global $i = 0;
$file = file($fname);
foreach ($file as $line) {
if (substr($line, 0, 1) != " ") {
$title = $line;
}
elseif (substr($line, 0, 4) == " " && substr($line, 4, 1) != " ") {
$test[substr($line, 4)] = array();
$i++;
}
elseif (substr($line, 0, 8) == " " && substr($line, 8, 1) != " ") {
list($name, $value) = explode("=", substr($line, 8));
$test[$i - 1][$name] = substr($line, 8);
}
else {
break;
}
}
}
else {
echo "<h1>Error! Specified test does not exist!</h1>\n";
}
}


I haven't gotten a parse error in months, so this is really weird. Please help! :)

Oh, here's a link to the live page:

http://www.radioactiverabbit.com/brandon/tests.php

pyro
12-08-2003, 07:09 PM
You can not define your variables as global like that. Try this one:

if (file_exists($fname)) {
global $test, $title, $i;
$test = array(); // line 17
$title = "";
$i = 0;
$file = file($fname);

PunkSktBrdr01
12-08-2003, 07:26 PM
Thanks! Never used global before. Anyway, I got that fixed, but now I'm getting this error:


Warning: Invalid argument supplied for foreach() in /home/radioact/public_html/brandon/tests.php on line 52


The purpose of this function is to parse files to create a multiple choice quiz. The $tests variable is an array containing the questions and answers. Each key in the array is the question, and the value is an array containing the answer name as the key and the answere value as the value. It's something like this:

$test -
question 1 -
answer name 1 - answer val 1
answer name 2 - answer val 2
question 2 -
answer name 1 - answer val 1
answer name 2 - answer val 2
question 3 - ...

Here's the updated code:


function parseTest($testname) {
$fname = "/brandon/tests/" . $testname . ".txt";
if (file_exists($fname)) {
global $test, $title, $i;
$test = array();
$title = "";
$i = 0;
$file = file($fname);
foreach ($file as $line) {
if (substr($line, 0, 1) != " ") {
$title = $line;
}
elseif (substr($line, 0, 4) == " " && substr($line, 4, 1) != " ") {
$test[substr($line, 4)] = array();
$i++;
}
elseif (substr($line, 0, 8) == " " && substr($line, 8, 1) != " ") {
list($name, $value) = explode("=", substr($line, 8));
$test[$i - 1][$name] = substr($line, 8);
}
else {
break;
}
}
}
else {
echo "<h1>Error! Specified test does not exist!</h1>\n";
}
}


Thanks! :)

PunkSktBrdr01
12-08-2003, 07:29 PM
Oops, just realized that line 52 isn't in the function. Here's the rest of the PHP from the file:


if (isset($_GET['test'])) {
parseTest($test);
echo "<h1>" . $title . "</h1>\n";
if (isset($_GET['submit'])) {
echo "<h1>You took the test!</h1>\n";
}
else {
echo "<form action=\"" . $PHP_SELF . "\" method=\"get\">\n";
foreach ($test as $key => $val) { // line 52
echo "<h2>" . $key . "</h2>\n";
foreach ($val as $subkey => $subval) {
echo "<input type=\"radio\" name=\"" . $a . "\" value=\"" . $subval . "\" /> " . $subkey . "<br />\n";
$a++;
}
echo "<br /><br />\n";
}
echo "<input type=\"submit\" name=\"submit\" value=\"Submit...\" />\n";
echo "</form>\n";
}
}
else {
echo "<h1>Error! No test specified!</h1>\n";
}

pyro
12-08-2003, 07:48 PM
$test probably is not an array, then...

PunkSktBrdr01
12-08-2003, 08:40 PM
Thanks! I realized that it happens when $_GET['testname'] is set but empty. I fixed that now, but I'm experiencing some unexpected PHP behaviour. During the foreach loops:


foreach ($test as $key => $val) {
echo "<h2>" . $key . "</h2>\n";
foreach ($val as $subkey => $subval) {
echo "<input type=\"radio\" name=\"" . $a . "\" value=\"" . $subval . "\" /> " . $subkey . "<br />\n";
}
echo "<br /><br />\n";
$a++;
}


it repeats the outer loop twice in a row, first for the text key, and then for the numeric key. Is there a way to prevent this? Here's an example:

http://www.radioactiverabbit.com/brandon/tests.php?testname=cruelty

Thanks so much! :)

pyro
12-08-2003, 09:54 PM
If you only want the first value, why use a foreach loop? Why not use $test[0]?

PunkSktBrdr01
12-08-2003, 10:04 PM
I guess I wasn't clear enough. The loop should do this:

echo "question 1"
echo "answer 1"
echo "answer 2"
increment $a
echo "question 2"
echo "answer 1"
echo "answer 2"
increment $a
...

but it is doing this:

echo "question 1"
increment $a
echo "0"
echo "answer 1"
echo "answer 2"
increment $a
echo "question 2"
increment $a
echo "1"
echo "answer 1"
...

I can't figure out why it is echoing the numeric key of each question along with the textual key.

YoN
12-09-2003, 05:52 PM
try going to this line:
echo "<h2>" . $key . "</h2>\n";

and changing it to:
if (gettype($key) != "integer") { echo "<h2>" . $key . "</h2>\n"; }

I think it would work in your specific case.

PunkSktBrdr01
12-09-2003, 09:11 PM
Thanks! Now it's finally working correctly. Yay! :)

YoN
12-10-2003, 02:13 PM
You're Welcome :)