0

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.

1
  • This is because your while loop ends after all the rows have been fetched. after all rows are fetched, then $x gets incremented. Commented Aug 20, 2014 at 16:47

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

7 Comments

Guys, I have gone through all your suggestions. But they seem not to work. The point is: I have more than 100 questions in the <questions table>. Each question is fetched randomly by a LIMIT 1. That means, I have put a check that no question should repeat itself. So, each time a question is selected, the counter needs to read a different value. That is where the problem lies. The counter keeps printing the constant value (1) instead of incrementing. Meanwhile, I need to increment the counter variable each time the select statement runs and not the While() loop. Thank you.
But in your code, the query is executed one time only (in line 3). Where should the counter be updated?
You see, I have more than a hundred questions. That is why I decided to use a for loop as for(x=1; and x < total rows -1; x++). The loop should start above the while() loop. So that each time, the select statement comes, the x++ will execute.... OKAY! I have an idea. Maybe I should put the for loop above the select statement... what do you think?
The problem is that you limit your SQL statement to 1 row, but in the for-loop you use the number of rows returned (which is in this case always 1)
Does it mean that I have to construct an external query without a LIMIT on the rows, then use it to compare with a for loop? Please help more... thannks
|
0

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.

14 Comments

Guys, I have gone through all your suggestions. But they seem not to work. The point is: I have more than 100 questions in the <questions table>. Each question is fetched randomly by a LIMIT 1. That means, I have put a check that no question should repeat itself. So, each time a question is selected, the counter needs to read a different value. That is where the problem lies. The counter keeps printing the constant value (1) instead of incrementing. Meanwhile, I need to increment the counter variable each time the select statement runs and not the While() loop. Thank you.
Why didn't you explain all of that in your original question?
I am sorry, my mistake. I just realised that this is the problem all along and not the while loop. Because the while loop is only loop through the rows of the select query. I am sorry. So, how do I solve this problem please? Thank you!
rather than executing the query for random 1 row every time, it is better to get all the rows at once and then show them one by one with logic in php. That will be efficient as you need not worry about getting the same row twice and also the DB query will be saved
HOW? Notice that, the query is random selection of rows. How do you construct this logic please? I cannot figure out any ideas now. Thank you!
|
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.