Printing out from database

I have a table quiz in my database and I want to print out each quiz with the questions.

Quiz 1
1 2 3
4 5 6

Quiz 2
1 2 3
4 5 6

Maybe my query is not good, because doesn’t show how I want.

My database:

CREATE TABLE IF NOT EXISTS `quiz` (
  `id` tinyint(4) NOT NULL AUTO_INCREMENT,
  `category` varchar(20) NOT NULL,
  `quiz_id` int(11) NOT NULL,
  `q` text NOT NULL,
  `question` text NOT NULL,
  `opt1` text NOT NULL,
  `opt2` text NOT NULL,
  `opt3` text NOT NULL,
  `answer` text NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=4 ;


How it looks:


id category quiz_id q  question   opt1 opt2 opt3 answer
1  a          1         question1     a    b      c      b
2  a          1         question2     a    b      c      c
3  a          1         question3     a    b      c      b
4  a          1         question4     a    b      c      a
5  a          1         question5     a    b      c      c
6  a          1         question6     a    b      c      b
1  a          2         question1     a    b      c      c
2  a          2         question2     a    b      c      c
3  a          2         question3     a    b      c      a
4  a          2         question4     a    b      c      a
5  a          2         question5     a    b      c      c
6  a          2         question6     a    b      c      b

Please someone tell me how to do the query and php to print out like this:

Quiz 1
1 2 3
4 5 6

Quiz 2
1 2 3
4 5 6


is this like what you want?


$sql = "SELECT * FROM quiz WHERE 1 ORDER BY category ASC, id ASC";
$result = mysql_query($sql) or die(mysql_error());
$counter = 1;
while($rows = mysql_fetch_object($result)){
	if($rows->quiz_id != $old_quiz_id){
		$old_quiz_id = $rows->quiz_id;
		echo '<strong>Quize ' . $old_quiz_id . '</strong><br />';
	}
	echo $rows->question . ' ';
	if($counter &#37; 3 == 0){
		echo '<br />';
	}
	$counter++;
}

Yes. Works like that, but if I put links doesn’t work.

sql = "SELECT * FROM quiz WHERE 1 ORDER BY category ASC, id ASC";
						$result = mysql_query($sql) or die(mysql_error());
						$counter = 1;
						while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
							if($row['quiz_id'] != $old_quiz_id){
								$old_quiz_id = $row['quiz_id'];
								echo '<li><b><a href="test.php?nr='.$old_quiz_id.'"></li>';
							}
							echo  ' <br />';
							if($counter % 3 == 0){
								echo '<br />';
							}
							$counter++;
						}

and please tell me what means WHERE 1 in that query.
In my example i wrote wrong. I mean I the ex looks like this:

Category a
123
345

where 123456 are the quizzes, and then when I click on 1, for ex, I will open test.php with the first quiz and the questions.

“Where 1” in a mySQL query can be said to mean “Get all records from the table”, whereas, for another example, "where name = ‘dave’ " only gets records who’s ‘name’ field contains the string ‘dave’.

Try this:


      echo '<li><b><a href="test.php?nr='.$old_quiz_id.'">'.$old_quiz_id.'</a></li>';

You were missing the second half of the link. :slight_smile:

Why don’t you post up the HTML you require?

Scary database structure aside, I’m sure the kind folk here would be able to generate it for you given the data provided. :slight_smile:

Thank you Dave Morton. Works.

All in a night’s lurking. :smiley: