Jump to content

Recommended Posts

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
{
...
?>

Link to comment
https://forums.phpfreaks.com/topic/155518-solved-counter-problem/
Share on other sites

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 />";
}

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']++;

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.

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
}

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?

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 
}


?>

 

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 :)

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.