PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Moderator: General Moderators
bawla
Forum Contributor
Posts: 116 Joined: Fri Mar 19, 2004 9:15 am
Post
by bawla » Wed Mar 24, 2004 8:51 pm
i get this error
Code: Select all
Parse error: parse error in /home/krunk/public_html/ghetto.php on line 12
and this is the script im using
Code: Select all
<?php
$points=NULL
if (isset($_POST['ques1'] = 'ques1_true')){
$points+5;
}
if (isset($_POST['ques2'] = 'ques2_true')){
$points+8;
}
if (isset($_POST['ques3'] = 'ques3_true')){
$points+7;
}
if (isset($_POST['ques4'] = 'ques4_true')){
$points+6;
}
if (isset($_POST['ques5'] = 'ques5_true')){
$points+5;
}
if (isset($_POST['ques5_2'] = 'ques5_2_true')){
$points+5;
}
if (isset($_POST['ques6'] = 'ques6_true')){
$points+3;
}
if (isset($_POST['ques7'] = 'ques7_true')){
$points+3;
}
if (isset($_POST['ques8'] = 'ques8_true')){
$points+15;
}
if (isset($_POST['ques9'] = 'ques9_true')){
$points+2;
}
if (isset($_POST['ques10'] = 'ques10_true')){
$points+7;
}
echo = $points;
?>
line 12 is
Code: Select all
if (isset($_POST['ques1'] = 'ques1_true')){
John Cartwright
Site Admin
Posts: 11470 Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:
Post
by John Cartwright » Wed Mar 24, 2004 8:52 pm
Code: Select all
<?php
$points=NULL
if (isset($_POST['ques1'] == 'ques1_true')){
$points+5;
}
elseif (isset($_POST['ques2'] == 'ques2_true')){
$points+8;
}
elseif (isset($_POST['ques3'] == 'ques3_true')){
$points+7;
}
elseif (isset($_POST['ques4'] == 'ques4_true')){
$points+6;
}
elseif (isset($_POST['ques5'] == 'ques5_true')){
$points+5;
}
elseif (isset($_POST['ques5_2'] == 'ques5_2_true')){
$points+5;
}
elseif (isset($_POST['ques6'] == 'ques6_true')){
$points+3;
}
elseif (isset($_POST['ques7'] == 'ques7_true')){
$points+3;
}
elseif (isset($_POST['ques8'] == 'ques8_true')){
$points+15;
}
elseif (isset($_POST['ques9'] == 'ques9_true')){
$points+2;
}
elseif (isset($_POST['ques10'] == 'ques10_true')){
$points+7;
}
echo $points;
?>
Not sure if your setting question to question_true.. if you are then use the = if not just use == ( checks to see if they are equal )
Last edited by
John Cartwright on Wed Mar 24, 2004 8:55 pm, edited 2 times in total.
tim
DevNet Resident
Posts: 1165 Joined: Thu Feb 12, 2004 7:19 pm
Location: ohio
Post
by tim » Wed Mar 24, 2004 8:53 pm
$points=NULL
whatever that is, $points=NULL;
bawla
Forum Contributor
Posts: 116 Joined: Fri Mar 19, 2004 9:15 am
Post
by bawla » Wed Mar 24, 2004 9:01 pm
even after i change that i still get the same error
markl999
DevNet Resident
Posts: 1972 Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)
Post
by markl999 » Wed Mar 24, 2004 9:06 pm
You need to change all those..
if (isset($_POST['ques1'] == 'ques1_true')){
$points+5;
}
to
if (!empty($_POST['ques1']) && $_POST['ques1'] == 'ques1_true'){
$points += 5;
}
I'm sure you're just learning how to do stuff so i'll leave it there, but there is an infintely easier way to do what you're doing
Goowe
Forum Commoner
Posts: 94 Joined: Mon Mar 15, 2004 9:51 am
Location: Southeast Alaska
Post
by Goowe » Wed Mar 24, 2004 9:06 pm
Did you add that semi-colan after "NULL" like Tim said?
What is the error now?
[Sorry I'm running late]
bawla
Forum Contributor
Posts: 116 Joined: Fri Mar 19, 2004 9:15 am
Post
by bawla » Wed Mar 24, 2004 9:30 pm
I'm sure you're just learning how to do stuff so i'll leave it there, but there is an infintely easier way to do what you're doing
tell me!! lol
bawla
Forum Contributor
Posts: 116 Joined: Fri Mar 19, 2004 9:15 am
Post
by bawla » Wed Mar 24, 2004 9:36 pm
this is after i added the semicolon
Code: Select all
Parse error: parse error, expecting `','' or `')'' in /home/krunk/public_html/ghetto.php on line 12
markl999
DevNet Resident
Posts: 1972 Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)
Post
by markl999 » Wed Mar 24, 2004 9:44 pm
tell me!! lol
Well, here's an example
Code: Select all
<?php
$pointsarr = array(
'ques1' => 5,
'ques2' => 8,
'ques3' => 7,
'ques4' => 6,
'ques5' => 5,
'ques5_2' => 5,
'ques6' => 3,
'ques7' => 3,
'ques8' => 15,
'ques9' => 2,
'ques10' => 7
);
if(!empty($_POST)){
$points = 0;
foreach($_POST as $key=>$val){
if(isset($pointsarr[$key])){
$points += $pointsarr[$key];
}
}
echo $points;
}
?>
<html>
<body>
<form method="post" action="">
one <input type="checkbox" name="ques1" value="1"><br />
two <input type="checkbox" name="ques2" value="1"><br />
three <input type="checkbox" name="ques3" value="1"><br />
four <input type="checkbox" name="ques4" value="1"><br />
five <input type="checkbox" name="ques5" value="1"><br />
five_2 <input type="checkbox" name="ques5_2" value="1"><br />
six <input type="checkbox" name="ques6" value="1"><br />
seven <input type="checkbox" name="ques7" value="1"><br />
eight <input type="checkbox" name="ques8" value="1"><br />
nine <input type="checkbox" name="ques9" value="1"><br />
ten <input type="checkbox" name="ques10" value="1"><br />
<input type="submit" name="submit" value="submit" />
</form>
</body>
</html>
bawla
Forum Contributor
Posts: 116 Joined: Fri Mar 19, 2004 9:15 am
Post
by bawla » Wed Mar 24, 2004 9:52 pm
complicated enough
<<<- that is wut im doing right now, lol
how did u get $key, and $val, if i know wut they are , i'd understand it much better
markl999
DevNet Resident
Posts: 1972 Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)
Post
by markl999 » Wed Mar 24, 2004 11:26 pm
Here's the PHP bit again, with comments this time
Code: Select all
<?php
//an array that holds form field names and
//their matching points value
$pointsarr = array(
'ques1' => 5,
'ques2' => 8,
'ques3' => 7,
'ques4' => 6,
'ques5' => 5,
'ques5_2' => 5,
'ques6' => 3,
'ques7' => 3,
'ques8' => 15,
'ques9' => 2,
'ques10' => 7
);
//check if the form was submitted
if(!empty($_POST)){
$points = 0; //initialise the points to 0
//the line below will loop over all the posted form elements
//$key will be the form name (eg ques1)
//$val will be it's value (eg 1)
foreach($_POST as $key=>$val){
//the next line sees if the key is set in the points array
if(isset($pointsarr[$key])){
//if it is, then we take add the points value from the
//pointsarr array and add it onto the total points.
$points += $pointsarr[$key];
}
}
echo $points;
}
?>
tim
DevNet Resident
Posts: 1165 Joined: Thu Feb 12, 2004 7:19 pm
Location: ohio
Post
by tim » Thu Mar 25, 2004 7:36 am
go to php.net and type in foreach() in the function search.