tecmeister Posted February 11, 2008 Share Posted February 11, 2008 Hi everyone, Im having problems implementing a score into a quiz. This is the code that im using. <?php if ($false =="false") { $score=$score-1; }; if ($true =="true") { $score=$score+1; }; echo $score; ?> The problem is when i answer the question it just displays 1 or -1 depending on the question. I doesn't add or subtract it on to the score. What is the correct code? Thanks for your help. tecmeister Quote Link to comment https://forums.phpfreaks.com/topic/90443-implement-a-score/ Share on other sites More sharing options...
vbnullchar Posted February 11, 2008 Share Posted February 11, 2008 post more codes please... Quote Link to comment https://forums.phpfreaks.com/topic/90443-implement-a-score/#findComment-463652 Share on other sites More sharing options...
mikefrederick Posted February 11, 2008 Share Posted February 11, 2008 either $score isn't set or the format is incorrect. Quote Link to comment https://forums.phpfreaks.com/topic/90443-implement-a-score/#findComment-463654 Share on other sites More sharing options...
vbnullchar Posted February 11, 2008 Share Posted February 11, 2008 set a initial value for $score.. then see if it add or subtracts.. Quote Link to comment https://forums.phpfreaks.com/topic/90443-implement-a-score/#findComment-463655 Share on other sites More sharing options...
tecmeister Posted February 11, 2008 Author Share Posted February 11, 2008 That is all the code that i have. I got it off a website. I guess that it is wrong. will someone be able to tell me the correct code? Thanks for your help tecmeister Quote Link to comment https://forums.phpfreaks.com/topic/90443-implement-a-score/#findComment-463656 Share on other sites More sharing options...
mikefrederick Posted February 11, 2008 Share Posted February 11, 2008 well if that is all the code you have then obviously score is not set. say you used a form w/ get method and score started at 0 then you use $score=$_GET['score']; and then the rest of your info. Or you could just put all the questions on one page and then make the final score the percent of correct answers over total answers Quote Link to comment https://forums.phpfreaks.com/topic/90443-implement-a-score/#findComment-463658 Share on other sites More sharing options...
vbnullchar Posted February 11, 2008 Share Posted February 11, 2008 <?php $score = 0; if($ans==TRUE) { $score++; } else { $score--; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/90443-implement-a-score/#findComment-463659 Share on other sites More sharing options...
vbnullchar Posted February 11, 2008 Share Posted February 11, 2008 if $true and $false are boolean.. you can remove the quotes in "true" and "false" Quote Link to comment https://forums.phpfreaks.com/topic/90443-implement-a-score/#findComment-463662 Share on other sites More sharing options...
blackcell Posted February 11, 2008 Share Posted February 11, 2008 Nevermind... Quote Link to comment https://forums.phpfreaks.com/topic/90443-implement-a-score/#findComment-463664 Share on other sites More sharing options...
vbnullchar Posted February 11, 2008 Share Posted February 11, 2008 Nevermind..??? Quote Link to comment https://forums.phpfreaks.com/topic/90443-implement-a-score/#findComment-463669 Share on other sites More sharing options...
tecmeister Posted February 11, 2008 Author Share Posted February 11, 2008 Im having problems with the score displaying on the next page. This is the code that it displayed on the next page: <?php $score = $_GET['score']; echo $score; if($true==TRUE) { $score++; } else { $score--; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/90443-implement-a-score/#findComment-463682 Share on other sites More sharing options...
vbnullchar Posted February 11, 2008 Share Posted February 11, 2008 move echo $score to the last line <?php $score = $_GET['score']; if($true==TRUE) { $score++; } else { $score--; } echo $score; ?> Quote Link to comment https://forums.phpfreaks.com/topic/90443-implement-a-score/#findComment-463683 Share on other sites More sharing options...
tecmeister Posted February 11, 2008 Author Share Posted February 11, 2008 The score is still not displaying. I dont know what the problem is. But thanks for the help. Quote Link to comment https://forums.phpfreaks.com/topic/90443-implement-a-score/#findComment-463686 Share on other sites More sharing options...
Northern Flame Posted February 11, 2008 Share Posted February 11, 2008 are you sure $_GET['score'] is being set correctly? can we see the form that the user fills out so that we can better assist you? Quote Link to comment https://forums.phpfreaks.com/topic/90443-implement-a-score/#findComment-463691 Share on other sites More sharing options...
tecmeister Posted February 11, 2008 Author Share Posted February 11, 2008 This is the code that im using on the first page: <?php $score = 0; echo $score; ?> this is what i using on the rest: <?php $score = $_GET['score']; if($true==TRUE) { $score++; } else { $score--; } echo $score; ?> Quote Link to comment https://forums.phpfreaks.com/topic/90443-implement-a-score/#findComment-463697 Share on other sites More sharing options...
Northern Flame Posted February 11, 2008 Share Posted February 11, 2008 thats ALL your code? how do they get from page1 to page2? Quote Link to comment https://forums.phpfreaks.com/topic/90443-implement-a-score/#findComment-463700 Share on other sites More sharing options...
tecmeister Posted February 11, 2008 Author Share Posted February 11, 2008 Sorry i thought that you was just asking for the php code. This is the first page code: <body> <table> <td width="884" align="center"> Welcome to the Quiz <?php $username = $_REQUEST['username']; echo $username; ?> </td> </table> <table> <td> score <?php $score = 0; echo $score; ?> </td> </table> <table> <p> </p> <p> </p> <tr> <td> Which State in America was the Titanic Heading to? </td> </tr> <tr> <td> <form method="post" action="q2.php"> <table width="133" align="center"> <tr> <td width="112"> Florida </td> <td> <input type="radio" name="q" value="false" /> </td> </tr> <tr> <td> New York </td> <td> <input type="radio" name="ans" value="true" /> </td> </tr> <tr> <td> Chicago </td> <td> <input type="radio" name="q" value="false" /> </td> </tr> <tr> <td> L.A </td> <td> <input type="radio" name="q" value="false" /> </td> </tr> <tr> <td align="center"><input type="submit" value="Submit" /> </td></tr> </table> </form> </table> </body> This is the next page: <body> <table> <td> score <?php $score = $_GET['score']; if($true==TRUE) { $score++; } else { $score--; } echo $score; ?></td> </table> <table> <p> </p> <p> </p> <tr> <td width="245"> How many feet are there in a meter? </td> </tr> <tr> <td> <form method="post" action="q3.php"> <table width="133" align="center"> <tr> <td width="112"> 2.4</td> <td> <input type="radio" name="q" value="false" /> </td> </tr> <tr> <td> 4.1</td> <td> <input type="radio" name="q" value="false" /> </td> </tr> <tr> <td> 3.3</td> <td> <input type="radio" name="ans" value="true" /> </td> </tr> <tr> <td> 3</td> <td><input type="radio" name="q" value="false" /> </td> </tr> <tr> <td align="center"> <input type="submit" value="Submit" /> </td></tr> </table> </form> </table> </body> Quote Link to comment https://forums.phpfreaks.com/topic/90443-implement-a-score/#findComment-463710 Share on other sites More sharing options...
Northern Flame Posted February 11, 2008 Share Posted February 11, 2008 alright, on the second page when you want to echo the score do something like <?php $score = 0; foreach($_POST as $answer){ if($answer == "true"){ $score++; } else{ $score--; } } echo $score; ?> Quote Link to comment https://forums.phpfreaks.com/topic/90443-implement-a-score/#findComment-463717 Share on other sites More sharing options...
tecmeister Posted February 11, 2008 Author Share Posted February 11, 2008 It doesn't add to the score. It just says 1 on the score on every page. Quote Link to comment https://forums.phpfreaks.com/topic/90443-implement-a-score/#findComment-463723 Share on other sites More sharing options...
Northern Flame Posted February 11, 2008 Share Posted February 11, 2008 alright try this instead: <?php $score = 0; foreach($_POST as $answer){ if($answer == "true"){ $score++; } } echo $score; ?> Quote Link to comment https://forums.phpfreaks.com/topic/90443-implement-a-score/#findComment-463726 Share on other sites More sharing options...
vbnullchar Posted February 11, 2008 Share Posted February 11, 2008 what method did you use to pass the $score variable? is it by form or url? Quote Link to comment https://forums.phpfreaks.com/topic/90443-implement-a-score/#findComment-463730 Share on other sites More sharing options...
Northern Flame Posted February 11, 2008 Share Posted February 11, 2008 he used post in the code posted above Quote Link to comment https://forums.phpfreaks.com/topic/90443-implement-a-score/#findComment-463735 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.