/    Sign up×
Community /Pin to ProfileBookmark

mystery parse error!

I’m getting a parse error:

[QUOTE]

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

[/QUOTE]

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:

[code=php]
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”;
}
}
[/code]

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:

[url]http://www.radioactiverabbit.com/brandon/tests.php[/url]

to post a comment
PHP

10 Comments(s)

Copy linkTweet thisAlerts:
@pyroDec 09.2003 — You can not define your variables as global like that. Try this one:

[code=php]if (file_exists($fname)) {
global $test, $title, $i;
$test = array(); // line 17
$title = "";
$i = 0;
$file = file($fname);[/code]
Copy linkTweet thisAlerts:
@PunkSktBrdr01authorDec 09.2003 — 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
[/QUOTE]


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:

[code=php]
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";
}
}
[/code]


Thanks! ?
Copy linkTweet thisAlerts:
@PunkSktBrdr01authorDec 09.2003 — Oops, just realized that line 52 isn't in the function. Here's the rest of the PHP from the file:

[code=php]
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";
}
[/code]
Copy linkTweet thisAlerts:
@pyroDec 09.2003 — $test probably is not an array, then...
Copy linkTweet thisAlerts:
@PunkSktBrdr01authorDec 09.2003 — 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:

[code=php]
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++;
}
[/code]


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! ?
Copy linkTweet thisAlerts:
@pyroDec 09.2003 — If you only want the first value, why use a foreach loop? Why not use $test[0]?
Copy linkTweet thisAlerts:
@PunkSktBrdr01authorDec 09.2003 — 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.
Copy linkTweet thisAlerts:
@YoNDec 09.2003 — try going to this line:
[code=php] echo "<h2>" . $key . "</h2>n";
[/code]

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

I think it would work in your specific case.
Copy linkTweet thisAlerts:
@PunkSktBrdr01authorDec 10.2003 — Thanks! Now it's finally working correctly. Yay! ?
Copy linkTweet thisAlerts:
@YoNDec 10.2003 — You're Welcome ?
×

Success!

Help @PunkSktBrdr01 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...