6 Replies - 905 Views - Last Post: 26 April 2008 - 08:32 PM Rate Topic: -----

#1 smallfatguy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 20-June 05

showing rows from database on separate pages

Post icon  Posted 19 April 2008 - 09:30 PM

what I am trying to do is to display the rows from my database (containing quiz questions) on separate pages, so that the user will have to click a 'next' button to view the next question. At the moment, I can get all the questions and answers to display on a single page with :

<?php
include("header.php");

	
	

$sql = "SELECT * FROM tbl_questions";

$result = mysql_query($sql);
$row = mysql_fetch_assoc($result);
$numrows = mysql_numrows($result);


$i = 0;
while ($i < $numrows) {
			$question_no = $row['ref'];
			$q = $row['question'];
			$A = $row['ans1'];
			$B = $row['ans2'];
			$C = $row['ans3'];
			$D = $row['ans4'];

	
	


?>




<h2><?PHP print $q; ?></h2>
<table>
<tr>
<td><INPUT TYPE = 'radio' Name ='q'  value= 'A' <?PHP print $answerA; ?> /></td><td> <?PHP print $A; ?></td>
</tr>
<tr>
<td><INPUT TYPE = 'radio' Name ='q'  value= 'B' <?PHP print $answerB; ?> /></td><td> <?PHP print $B; ?></td>
</tr>
<tr>
<td><INPUT TYPE = 'radio' Name ='q'  value= 'C' <?PHP print $answerC; ?> /></td><td> <?PHP print $C; ?></td>
</tr>
<tr>
<td><INPUT TYPE = "radio" NAME = "q" value = "D" <?php print $answerD; ?> /></td><td> <?php print $C; ?></td>
</tr>
</table>

<p></p>
<p></p>


<?php


$i++;
}


require("footer.php");				
?>




Where $i is incremented in the above code, I tried the following change to the code, my idea being to include a button within the loop that would only increment $i if the button had been clicked. (I am sure that anyone who knows php well is laughing on the floor at the moment). I thought at worst, the adjusted code would reload the page and set $i back to zero each time, but it completely locked up the whole system and I had to stop apache and mysql running to get things working again.

<form action="<?php echo $_SERVER['PHP_SELF']?>" method="post">
<INPUT TYPE = "submit" Name = "submit"  VALUE = "Next Question">
</form>

<?php


/*
if (isset($_POST['submit'])) {
	$i++;
//	unset($_POST['submit']);
}	// end if
else {
	echo "you must choose one of the answers by clicking the button beside it";
	}  // end else
*/



Any suggestions or clues on the best way to set up this quiz so that only a single question and its answers appear on the page with the next button would be great, as I haven't any more ideas at the moment.

thanks

Is This A Good Question/Topic? 0
  • +

Replies To: showing rows from database on separate pages

#2 no2pencil  Icon User is online

  • Head MFIC
  • member icon

Reputation: 5060
  • View blog
  • Posts: 26,429
  • Joined: 10-May 07

Re: showing rows from database on separate pages

Posted 19 April 2008 - 09:35 PM

I would start a session, & then you can set session variables that will carry from page to page, as long as you pass the session over.

PHP Sessions
Was This Post Helpful? 0
  • +
  • -

#3 smallfatguy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 20-June 05

Re: showing rows from database on separate pages

Posted 19 April 2008 - 11:49 PM

no2pencil,

many thanks for you really fast reply. And VERY helpful reply!!!!

The link you provided was far too in-depth for me, so after hitting the books I came up with the following solution which does the trick. And even better than that it opens up a huge range of possibilities, so I think I will even be able to include scores, statistics and maybe even proper feedback.

On the index page of the 'site', I have included start_session(); and set a session variable with
session_register("question_number");
$_SESSION['question_number'] = 0;

Next the actual quiz page consists of the following code. Obviously it is not properly complete yet, but hopefully this will be of some use to any other php/mysql newbies like myself.

<?php
session_start();
include("header.php");

$_SESSION['question_number']++;
$question_no = $_SESSION['question_number'];

$sqltest = "SELECT * FROM tbl_questions";
$resulttest = mysql_query($sqltest);
$rowtest = mysql_fetch_assoc($resulttest);
$numrows = mysql_numrows($resulttest);


$sql = "SELECT * FROM tbl_questions WHERE tbl_questions.ref = $question_no";
$result = mysql_query($sql);
$row = mysql_fetch_assoc($result);


	$question = 'Question not set';
	$answerA = 'unchecked';
	$answerB = 'unchecked';
	$answerC = 'unchecked';
	$answerD = 'unchecked';
	
	
	$q = $row['question'];
	$A = $row['ans1'];
	$B = $row['ans2'];
	$C = $row['ans3'];
	$D = $row['ans4'];

?>

<h2><?php print $q; ?></h2>
<table>
<tr>
<td><input type = 'radio' name ='q'  value= 'A' <?php print $answerA; ?> /></td><td> <?php print $A; ?></td>
</tr>
<tr>
<td><input type = 'radio' name ='q'  value= 'B' <?php print $answerB; ?> /></td><td> <?php print $B; ?></td>
</tr>
<tr>
<td><input type = 'radio' name ='q'  value= 'C' <?php print $answerC; ?> /></td><td> <?php print $C; ?></td>
</tr>
<tr>
<td><input type = "radio" name = "q" value = "D" <?php print $answerD; ?> /></td><td> <?php print $C; ?></td>
</tr>
</table>

<p></p>
<p></p>
<?php



if ($question_no < $numrows) {

	echo "<form action=".  $_SERVER['PHP_SELF'] . " method=\"post\">";
	echo "<input type=\"submit\" name = \"submit\"  value = \"Next Question\">";
	echo "</form>";
	}
else {
	echo "this looks like the last question";
	}




require("footer.php");				
?>




I have included the actual query twice; the first time returns all the rows so that I can get the number of rows (and therefore the total number of questions). The second time is to pick out the questions singly in sequence.

Hopefully this will be of some use to others, as I have seen quite a few quiz-related questions while I was researching this.

no2pencil - thanks again for your really fast reply

P.S. that little video clip in your signature is a bit vicious - is it a genuine incident or is it a set up for the camera?
Was This Post Helpful? 0
  • +
  • -

#4 smallfatguy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 20-June 05

Re: showing rows from database on separate pages

Posted 20 April 2008 - 12:00 AM

I forgot to mention that one of the better tutorials I found was at:

http://www.tizag.com...phpsessions.php

very straightforward with some really simple, clear examples. I know there is a lot more to sessions than this, but its a really nice introduction for the complete beginner.
Was This Post Helpful? 0
  • +
  • -

#5 no2pencil  Icon User is online

  • Head MFIC
  • member icon

Reputation: 5060
  • View blog
  • Posts: 26,429
  • Joined: 10-May 07

Re: showing rows from database on separate pages

Posted 20 April 2008 - 12:23 AM

View Postsmallfatguy, on 20 Apr, 2008 - 02:49 AM, said:

I have included the actual query twice

You should try to avoid running multiple sql queries when you don't have to. Why get the information twice, when you already have it once? It just adds load time, as well as pain & suffering to your mysql server.

The function mysql_num_rows will fit the bill for the previously run select query
$sql = "SELECT * FROM tbl_questions WHERE tbl_questions.ref = $question_no";
$result = mysql_num_rows($sql);
echo "Number of rows in table: ".$result;



View Postsmallfatguy, on 20 Apr, 2008 - 02:49 AM, said:

no2pencil - thanks again for your really fast reply
P.S. that little video clip in your signature is a bit vicious - is it a genuine incident or is it a set up for the camera?

You're very welcome. I have no idea where the video is from, I had it in an images directory & felt like throwing it on my signature one day. No real reason or intention.
Was This Post Helpful? 0
  • +
  • -

#6 smallfatguy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 20-June 05

Re: showing rows from database on separate pages

Posted 20 April 2008 - 02:21 PM

no2pencil - thanks for the extra comments and suggstions.

However, (you must dread seeing that word in posts that you thought were sorted) I have tried a few options and can't see how to get it to work without two queries. The first uses no parameters and allows me to find the number of rows in the table. The second query matches the row number in the table with the session number, allowing me to compare the question number with the total number of questions, so that I can change the button characteristics and actions at the bottom of the page.

I have tried changing the loop conditions so that if

$row['question'] == ""

the loop would end. Although this works it means that I cannot include an option to display something like 'question 3 of 10', and it also means that I would have to include an extra counter within the loop to calculate percentage of questions answered correctly.

I wonder are there any other ways to go about extracting the number of rows in the table?
Was This Post Helpful? 0
  • +
  • -

#7 cresol  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 26-April 08

Re: showing rows from database on separate pages

Posted 26 April 2008 - 08:32 PM

thanks for this topic
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1