savagenoob Posted April 24, 2009 Share Posted April 24, 2009 OK, I am making an online testing program, and the user inputs the number of questions they want before the test is done. I cant figure out how to start a variable at 1, then increase it each time the user answers a question. Here is the code I am having problems with. <?php $_SESSION['COUNTER'] = 1;//here is my problem... $num = $_SESSION['COUNTER']; echo $num; $numques = $_POST['numques']; if($num <= $numques) { require_once('config.php'); $testname = $_POST['testname']; echo $testname; $select = "SELECT * FROM pq_crtp_quiz WHERE testname = '$testname' ORDER BY RAND() DESC LIMIT 1"; $query = mysql_query($select); echo mysql_error(); $row = mysql_fetch_assoc($query); $randnum = $_POST['randnum']; ?> <table> <form action="<?php echo $_SERVER['PHP_SELF']?>" method="post" name="questionans"> <tr><td>Question #<?php echo $i;?></td></tr> <tr><td><?php echo $row['question'];?></td></tr> <tr><td>Option 1 <?php echo $row['option1']; ?></td></tr> <tr><td>Option 2 <?php echo $row['option2']; ?></td></tr> <tr><td>Option 3 <?php echo $row['option3']; ?></td></tr> <tr><td>Option 4 <?php echo $row['option4']; ?></td></tr><br /> <tr><td>Answer <select name="answer"><option id="option1">Option 1</option> <option id="option2">Option 2</option> <option id="option3">Option 3</option> <option id="option4">Option 4</option> </select> <input name="question" type="hidden" value="<?php echo $row['id'];?>" /> <input name="testname" type="hidden" value="<?php echo $testname;?>" /> <input name="randnum" type="hidden" value="<?php echo $randnum;?>" /> <input name="numques" type="hidden" value="<?php echo $numques;?>" /> <tr><td><input type="submit" name="next" value="Next"></td></tr> </form> </table> <?php $_SESSION['COUNTER'] = $_SESSION['COUNTER']++; //and this doesnt work obviously... } else { ... ?> Quote Link to comment https://forums.phpfreaks.com/topic/155518-solved-counter-problem/ Share on other sites More sharing options...
JonnoTheDev Posted April 24, 2009 Share Posted April 24, 2009 You require a loop. All you are doing is incrementing the counter by 1. Example // displays Question 1 Question 2 Question 3 Question 4 $numQuestions = 4; for($counter = 0; $counter < $numQuestions; $counter++) { print "Question ".$counter+1."<br />"; } Quote Link to comment https://forums.phpfreaks.com/topic/155518-solved-counter-problem/#findComment-818363 Share on other sites More sharing options...
JonnoTheDev Posted April 24, 2009 Share Posted April 24, 2009 or if you are reloading the same page // if the counter is set increment by 1 // if not set then default to 1 $_SESSION['COUNTER'] = (is_numeric($_SESSION['COUNTER'])) ? $_SESSION['COUNTER']++ : 1; Remove $_SESSION['COUNTER'] = $_SESSION['COUNTER']++; Quote Link to comment https://forums.phpfreaks.com/topic/155518-solved-counter-problem/#findComment-818366 Share on other sites More sharing options...
laffin Posted April 24, 2009 Share Posted April 24, 2009 <?php $_SESSION['COUNTER'] = 1;//here is my problem... and did u ever consider using session_start() before actually using session variables? Quote Link to comment https://forums.phpfreaks.com/topic/155518-solved-counter-problem/#findComment-818379 Share on other sites More sharing options...
savagenoob Posted April 24, 2009 Author Share Posted April 24, 2009 Thanks for the help, I figured a loop would be a solution but when I did a loop it showed multiple questions on the same page, I need it to show 1 per submit. Im a bit confused on how to put your code together to make it work, it might have already solved my problem. Also, I want it set up where when the counter gets to the number of questions requested, then it will loop through the answers and post the number right or wrong, that is what the original { else } was for. And my session has already been initialized at this point, I just posted the problem code. Quote Link to comment https://forums.phpfreaks.com/topic/155518-solved-counter-problem/#findComment-818387 Share on other sites More sharing options...
JonnoTheDev Posted April 24, 2009 Share Posted April 24, 2009 If this is at the very top of your script then the counter is set. $_SESSION['COUNTER'] = (is_numeric($_SESSION['COUNTER'])) ? $_SESSION['COUNTER']++ : 1; If you want to display the answers then you need to run a conditional statement on the counter value. i.e. display answers after 4 questions $_SESSION['COUNTER'] = (is_numeric($_SESSION['COUNTER'])) ? $_SESSION['COUNTER']++ : 1; if($_SESSION['COUNTER'] == 4) { // display answers } else { // display question } Quote Link to comment https://forums.phpfreaks.com/topic/155518-solved-counter-problem/#findComment-818391 Share on other sites More sharing options...
savagenoob Posted April 24, 2009 Author Share Posted April 24, 2009 I applied as said, I know I am close. But the counter is not incrementing like it should, it remains at "1". <?php $_SESSION['COUNTER'] = (is_numeric($_SESSION['COUNTER'])) ? $_SESSION['COUNTER']++ : 1; echo $_SESSION['COUNTER']; ?> This just keeps returning "1". Any suggestions? Quote Link to comment https://forums.phpfreaks.com/topic/155518-solved-counter-problem/#findComment-818707 Share on other sites More sharing options...
RussellReal Posted April 25, 2009 Share Posted April 25, 2009 uhm.. you're forgetting to start the session... session_start Quote Link to comment https://forums.phpfreaks.com/topic/155518-solved-counter-problem/#findComment-818788 Share on other sites More sharing options...
savagenoob Posted April 25, 2009 Author Share Posted April 25, 2009 I did start the session, someone already said that I even put it in again to make sure, it is started, if it wasnt, $_SESSION['COUNTER'] wouldnt return "1" I dont believe. Quote Link to comment https://forums.phpfreaks.com/topic/155518-solved-counter-problem/#findComment-818814 Share on other sites More sharing options...
savagenoob Posted April 25, 2009 Author Share Posted April 25, 2009 I know posting a ton of code is frowned upon, but feel its necessary as it might help someone see the problem... <?php session_start(); if(isset($_POST['next'])) { $quesid = $_POST['question']; $answer = $_POST['answer']; $testid = $_POST['testname']; $randnum = $_REQUEST['randnum']; $result = mysql_query("INSERT INTO quiz_answers SET quesid = '$quesid', answer = '$answer', testname = '$testid', testid = '$randnum'"); echo mysql_error(); } session_start(); $_SESSION['COUNTER'] = (is_numeric($_SESSION['COUNTER'])) ? $_SESSION['COUNTER']++ : 1; $numques = $_POST['numques']; echo "<table><td>" . $numques . " Question Test </td></table><br>"; if($_SESSION['COUNTER'] == $numques) { $rightanswers = 0; $wronganswers = 0; $testgrade = mysql_query("SELECT * FROM quiz_answers WHERE testid = '$randnum'"); while($row = mysql_fetch_assoc($testgrade)){ $quesid = $row['quesid']; $quesanswer = $row['answer']; $inside = mysql_query("SELECT * FROM pq_crtp_quiz WHERE id = '$quesid'"); $test = mysql_fetch_assoc($inside); $testanswer = $test['answer']; if($quesanswer == $testanswer) { $rightanswers = $rightanswers + 1; } else { $wronganswers = $wronganswers + 1; } } echo "Right Answers: "; echo $rightanswers . "\n"; echo "Wrong Answers: "; echo $wronganswers . "\n"; unset($_SESSION['COUNTER']); } else { $testname = $_POST['testname']; echo "\n" . $testname; $select = "SELECT * FROM pq_crtp_quiz WHERE testname = '$testname' ORDER BY RAND() DESC LIMIT 1"; $query = mysql_query($select); echo mysql_error(); $row = mysql_fetch_assoc($query); $randnum = $_POST['randnum']; echo "<br>"; echo "Test ID: " . $randnum; ?> <table> <form action="<?php echo $_SERVER['PHP_SELF']?>" method="post" name="questionans"> <tr><td>Question #<?php echo $_SESSION['COUNTER'];?></td></tr> <tr><td><?php echo $row['question'];?></td></tr> <tr><td>Option 1 <?php echo $row['option1']; ?></td></tr> <tr><td>Option 2 <?php echo $row['option2']; ?></td></tr> <tr><td>Option 3 <?php echo $row['option3']; ?></td></tr> <tr><td>Option 4 <?php echo $row['option4']; ?></td></tr><br /> <tr><td>Answer <select name="answer"><option id="option1">Option 1</option> <option id="option2">Option 2</option> <option id="option3">Option 3</option> <option id="option4">Option 4</option> </select> <input name="question" type="hidden" value="<?php echo $row['id'];?>" /> <input name="testname" type="hidden" value="<?php echo $testname;?>" /> <input name="randnum" type="hidden" value="<?php echo $randnum;?>" /> <input name="numques" type="hidden" value="<?php echo $numques;?>" /> <tr><td><input type="submit" name="next" value="Next"></td></tr> </form> </table> <?php } ?> Quote Link to comment https://forums.phpfreaks.com/topic/155518-solved-counter-problem/#findComment-818851 Share on other sites More sharing options...
laffin Posted April 25, 2009 Share Posted April 25, 2009 and counter dusn increment, the code looks ok. By any chance have u tried simplifying the code to only the problem in question. than add code in? <?php session_start(); $_SESSION['counter']=isset($_SESSION['counter'])?$_SESSION['counter']++:1; header('Content-type: text/plain'); echo "Counter at : {$counter}\n"; ?> And Bam it dun work... Than it occurs to me, why are we incrementing it by one, than assigning it to itself? [code] <?php session_start(); $_SESSION['counter']=isset($_SESSION['counter'])?$_SESSION['counter']+1:1; header('Content-type: text/plain'); echo "Counter at : {$counter}\n"; ?> And Bam it works why does it work? Increment ++ operator dusn need to be assigned. depending on location, the increment happens before or after the expression $count=$counter++; counter is returned, than 1 is added, $count==$counter-1 $count=++$counter; add 1 to counter, return valur, $count==$counter so what ya was seeing was, that counter never increased. because the counter was incrememted ($_SESSION['counter']++) but the value reset because the redefinition ($_SESSION['counter']=) Mystery Solved Quote Link to comment https://forums.phpfreaks.com/topic/155518-solved-counter-problem/#findComment-818863 Share on other sites More sharing options...
savagenoob Posted April 25, 2009 Author Share Posted April 25, 2009 OK, awesome, you are the man... EXCEPT... why the heck with this code does the counter start at 2 instead of 1? ??? ??? ??? Man, its not making sense. But it does increment afterwards, goes 2, 3, ect... but starts at 2 with the first question. Quote Link to comment https://forums.phpfreaks.com/topic/155518-solved-counter-problem/#findComment-818867 Share on other sites More sharing options...
savagenoob Posted April 25, 2009 Author Share Posted April 25, 2009 Nevermind, it works, thank you so much, big ups... Quote Link to comment https://forums.phpfreaks.com/topic/155518-solved-counter-problem/#findComment-818870 Share on other sites More sharing options...
laffin Posted April 25, 2009 Share Posted April 25, 2009 Shure thing, I hope my explanation was good enough of why it happened the way it happend... LOL Quote Link to comment https://forums.phpfreaks.com/topic/155518-solved-counter-problem/#findComment-819033 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.