Reputation: 10261
i want to make a multi-question poll in steps. i want the questions to be displayed one after the other in sequence and the questions should be pulled from the DB.
i am successfully fetching one question from the DB. but the way i want to make it work is when the user clicks on Submit for one question it should take the user to the next question.
$query = "SELECT qid, qtitle FROM questions where qid = $qid";
$result = mysql_query($query) or die("ERROR: $query.".mysql_error());
// if records are present
if (mysql_num_rows($result) > 0) {
$row = mysql_fetch_object($result);
// get question ID and title
$qid = $row->qid;
echo '<div id=ques>';
echo '<h2>'.$row->qtitle .'</h2>';
echo '</div>';
can someone please help me with the loop and constructs for this type of query?
i dont want to make seperate pages for the questions.
Upvotes: 1
Views: 222
Reputation: 2287
Probably the easiest way to do it, since you mentioned that you don't want to use multiple pages, would be to list out all the questions on your page, but hide all but the first. Then when the user clicks "next" it hides the first and shows the second, and so on.
$query = "SELECT qid, qtitle FROM questions ORDER BY qid ASC";
$result = mysql_query($query) or die("Error: $query.".mysql_error());
while ($row = mysql_fetch_object($result)) {
echo "<div class='question' id='$row->qid'><h2>$row->qtitle</h2></div>";
}
echo "<button type='button' id='button'>Next</button>";
And then you can use jQuery to write some code like this:
$(function() {
$current_question = 1;
$(".question").each (function () {$(this).hide();})
$("#"+current_question).show();
$("#button").click(function() {
$("#"+current_question).hide();
current_question++;
$("#"+current_question).show():
})
)}
Upvotes: 0
Reputation: 747
The way I'd approach this is to post the next question id as a hidden field on your form (I'm assuming you have a form for the user to enter their answer?).
The first time you load the page, you're not going to have a question id, so you'd just grab the first one. Then in that same page, you'd write a query to get the next question id. You'd put that in your hidden field:
<input type="hidden" name="questionID" value="<?php echo $nextID ?>">
When the page is resubmitted you have a $_POST['questionID']; variable that you can use to generate the next question with.
Upvotes: 0
Reputation: 27643
You could store the question number in a hidden field on the forum.
if (isset($_POST["qnum"])) {
$questionNum = (integer) $_POST["qnum"];
} else {
$questionNum = 0;
}
$query = "SELECT qid, qtitle FROM questions where qid = $qid";
$result = mysql_query($query) or die("ERROR: $query.".mysql_error());
// if records are present
if (mysql_num_rows($result) > $questionNum) {
for ($_i = 0; $_i < $questionNum; $_i += 1)
$row = mysql_fetch_object($result);
// get question ID and title
$qid = $row->qid;
echo '<input type="hidden" value="$qnum" />';
echo '<div id=ques>';
echo '<h2>'.$row->qtitle .'</h2>';
echo '</div>';
}
That's assuming you're in an HTML form. If not, you could store them in the $_SESSION variable.
Upvotes: 1