0

I have a few questions and answers stored in the same table. Column Q and A.

----ID----Q----A----
   1   |  Q1  | A1 |
   2   |  Q2  | A2 |
   3   |  Q3  | A3 |

I want to echo all questions into a div slider and all answers into another div slider.

Something like this:

<div>
<ul>
<li>Question 1</li>
<li>Question 2</li>
<li>Question 3</li>
</ul>
</div>

<div>
<ul>
<li>Answer 1</li>
<li>Answer 2</li>
<li>Answer 3</li>
</ul>
</div>

Im selecting all rows order by RAND, otherwise I would just run the selection again (n00b style). How do I perform this loop, starting with first column continue with next in correct order? My knowledge in PHP is quite limited.

Code (how Im used to print tables):

<ul id="fade">
<?php
$num = "100";
$questions = "SELECT * FROM quiz ORDER BY RAND() LIMIT $num";


$result = mysql_query($questions, $dbconnection);

for ($i=0; $i < mysql_num_fields($result); $i++) {
$rank = 1;
}

  while($myRow = mysql_fetch_array($result)){
  echo "<li>" .$myRow['q']. "</li>";
  echo "<li>" .$myRow['a']. "</li>";
  rank++;       
  }
  ?>
  </div>
5
  • Please, don't use mysql_* functions in new code. They are no longer maintained and are officially deprecated. See the red box? Learn about prepared statements instead, and use PDO or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial. Commented Feb 5, 2014 at 1:39
  • @Charles: some of the words in your comment aren't links yet :-( Commented Feb 5, 2014 at 1:41
  • @zerkms, they all seem to work for me. That's a c&p from the stock link guide in room 11... Commented Feb 5, 2014 at 1:42
  • @Charles: it was irony - my point was that you've had so many links so that it's less black words rather than blue :-) Commented Feb 5, 2014 at 1:43
  • brb, linking every unlinked word to a dictonary... Commented Feb 5, 2014 at 1:44

2 Answers 2

1

Instead of echo them right away you could do add the questions to one array and the answers to another:

$questions = Array();
$answers = Array();

while($myRow = mysql_fetch_array($result)){
  $questions[] = $myRow['q'];
  $answers[] = $myRow['a']; 
 }

And then build the uls:

foreach($question as $q){
    echo "<li>" . $q . "<li>";
}

...

foreach($answer as $a){
    echo "<li>" . $a . "<li>";
}

Or something similar based on your needs.

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

1 Comment

Instead of array_push, use the [] syntax to add elements to the end. Instead of the clunky for loop, use foreach to iterate over the members of the array.
1

You can do something like this:

$q = "";
$a = "";
while($myRow = mysql_fetch_array($result))
{
  $q .= "<li>" .$myRow['q']. "</li>";
  $a .= "<li>" .$myRow['a']. "</li>";
}

echo "<div><ul>$q</ul></div>";
echo "<div><ul>$a</ul></div>";

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.