Cybernetiquettes
Cybernetiquettes

Reputation: 55

PHP count variable outside a while loop does not increment

I want to select a couple of questions from questions table randomly. But, I want to echo a counter in ascending order as the I print out the questions using a while loop. But the counter variable is not incrementing. I do not know where I am going wrong.

<php
$sql = "select id, questions, ans1, ans2, ans3, correctAns from questions Order By RAND() Limit 1";
$result = mysql_query($sql);
$result2 = mysql_num_rows($result);
for($x = 1; $x <= $result2; $x++)
{
while($row = mysql_fetch_array($result))
{
$id = $row['id'];
$quest = $row['questions'];
$a1 = $row['ans1'];
$a2 = $row['ans2'];
$a3 = $row['ans3'];
$correct = $row['correctAns'];

echo $x.'<br />';
echo $quest.<br />;
echo $a1.'<br />';
echo $a2.'<br />';
echo $a3.'<br />';
echo $correct.'<br />';
} //end while loop
} // end for loop
?>

The counter variable is just echoing 1 as the value throughout the loop.

Upvotes: 0

Views: 2655

Answers (3)

Cybernetiquettes
Cybernetiquettes

Reputation: 55

I want to make you all understand that I appreciate your collective efforts in attempting to solve the challenge that I put forth. However, I have solved the challenge. Below is the source:

<?php
// Establish Connection with Database// Select Database
include_once( '../../Connections/QUIZ.php' );
// Specify the query to execute

Every student has an app id in the session after login. For every question the student answers, the app performs an UPDATE in another table u_answer. We will first of all check whether the student has already answered any questions by performing a select query on the u_answer table.

AS_Id is the student's Id from the session variable.

$sql = "select * from u_answer,questions where u_answer.AS_Id = '" . $_SESSION[ 'appID' ] . "' and questions.qid = u_answer.qid";
$result = mysql_query( $sql, $con ) or die( mysql_error() );
$records = mysql_num_rows( $result );

if ( $records < 1 ) {
//create and increment a counter(x) by 1
$x = $records + 1;

Perform the original select query for the questions.

$sql2 = "select * from questions ORDER BY RAND() LIMIT 1";
else
{

Still increment the counter by 1 using the number of records found.

$x = $records + 1;

This select query below ensures that no students answers a particular question more than 1 time.

$sql2 = "SELECT questions.qid, questions.question,questions.ans1, 
questions.ans2, questions.ans3, questons.ans4, questions.correctAns
FROM questions WHERE NOT EXISTS(SELECT * FROM u_answer WHERE u_answer.qid = questions.qid AND u_answer.AS_Id = '" . $_SESSION[ 'appID' ] . "') ORDER BY RAND() LIMIT 1";   
$result2 = mysql_query( $sql2 );

perform a while loop here at this point.

while ( $row = mysql_fetch_array( $result2 ) ) {
$id = $row['id'];
$quest = $row['questions'];
$a1 = $row['ans1'];
$a2 = $row['ans2'];
$a3 = $row['ans3'];
$correct = $row['correctAns'];

Print out the extracted data.

echo $x.'<br />'; //the counter variable.
echo $quest.<br />;
echo $a1.'<br />';
echo $a2.'<br />';
echo $a3.'<br />';
echo $correct.'<br />';
}

Close connection to the database.

mysql_close($con);
?>

This works for me.

Upvotes: 0

iam-decoder
iam-decoder

Reputation: 2564

It's because you have a loop around the database fetching, consolidate that to one loop, try this:

<?php
  $sql = "select id, questions, ans1, ans2, ans3, correctAns from questions Order By RAND()";
  $result = mysql_num_rows($sql);
  $x = 1; // Start the counter
  while($row = mysql_fetch_array($sql))
  {
      echo $x.'<br />'
          .$row['questions'].'<br />'
          .$row['ans1'].'<br />'
          .$row['ans2'].'<br />'
          .$row['ans3'].'<br />'
          .$row['correctAns'].'<br />';
      $x++;
  } //end while loop
?>

shortened it for a very slight efficiency increase.

Upvotes: 0

Michael Wagner
Michael Wagner

Reputation: 1038

You musn't put the counter in a second loop:

<?php
$sql = "select id, questions, ans1, ans2, ans3, correctAns from questions Order By RAND() Limit 1";
$result = mysql_query($sql); //ADDED
$x = 1; // ADDED
while($row = mysql_fetch_array($result)) // CHANGED
{
  $id = $row['id'];
  $quest = $row['questions'];
  $a1 = $row['ans1'];
  $a2 = $row['ans2'];
  $a3 = $row['ans3'];
  $correct = $row['correctAns'];

  echo $x.'<br />';
  echo $quest.<br />;
  echo $a1.'<br />';
  echo $a2.'<br />';
  echo $a3.'<br />';
  echo $correct.'<br />';
  x++; //ADDED
} //end while loop
?>

In addition, you must pass the query reusult to mysql_fetch_array insteach of the query string.
BTW, mysql_* is deprecated. You should use mysqli_* or PDO.

Upvotes: 2

Related Questions