ekbayrak2
ekbayrak2

Reputation: 41

Show next database value when a button is clicked

I have this quiz which displays a question with 4 answers beneath. What I'm trying to do is show the next question when the first question is answered.

The questions and multiple choice answers are grabbed from the database and put inside buttons with a loop. I want the next question to show when any of the 4 buttons is clicked. I have tried to do this with questionId where question 1 has an id of 1 and question 2 an id of 2. The value of k goes to 2 but it doesn't show the question with id 2. If I manually change line 3 from $k=1; to $k=2; it shows the question with id 2. The end goal is to increment $k every time an option is clicked to show the next question.

<?php
$conn = mysqli_connect("127.0.0.1", "root", "", "vragendb");
$k=1;

$sql = "SELECT * FROM vraag WHERE vraagId = '".$k."'";
$result = $conn->query($sql);

if ($result->num_rows > 0) {

      $i =0;
     while($row = $result->fetch_assoc()) {

         echo " Vraag: ". $row["vraag"].

         "<ul class = 'answers".$i."'.>
         </br> <input type='button' class='btn btn-default' name='Optie".$i."' value=".$row['optie1']." id='optie1".$i."'>  </br>".

         "</br> <input type='button' class='btn btn-default' name='Optie".$i."' value=". $row["optie2"]." id='optie2".$i."'> </br> ".

         "</br> <input type='button' class='btn btn-default' name='Optie".$i."' value=". $row["optie3"]." id='optie3".$i."'>  </br>".

         "</br> <input type='button' class='btn btn-default' name='Optie".$i."' value=". $row["optie4"]." id='optie4".$i."'>  </br>".

         "</br></br>
         </ul>";
         $i++;
         $k++;
     }
} else {
     echo "0 results";

}

echo $k;
?>

Upvotes: 0

Views: 53

Answers (1)

Rave Alroone
Rave Alroone

Reputation: 71

Have you tried using $_GET parameters? You can redirect the user to a URL for the next question, something like:

http://example.com/quiz.php?question=1

where question will be the $k in your case.

As demonstrated here, the user navigates to the ^above-mentioned URL and processes the stuff as follows:

<?php
$conn = mysqli_connect("127.0.0.1", "root", "", "vragendb");

$k = $_GET['question']; //this gets the question depending on your parameter

$sql = "SELECT * FROM vraag WHERE vraagId = '".$k."'";
$result = $conn->query($sql);

if ($result->num_rows > 0) {

      $i =0;
     while($row = $result->fetch_assoc()) {
         // we changed stuff here to accommodate the changes
         echo " Vraag: ". $row["vraag"].

         "<ul class = 'answers".$i."'.>
         </br> <a href='http://example.com/quiz.php?question=".$k++."'> <input type='button' class='btn btn-default' name='Optie".$i."' value=".$row['optie1']." id='optie1".$i."'> </a> </br>".

         "</br> <a href='http://example.com/quiz.php?question=".$k++."'>  <input type='button' class='btn btn-default' name='Optie".$i."' value=". $row["optie2"]." id='optie2".$i."'</a> </br> ".

         "</br> <a href='http://example.com/quiz.php?question=".$k++."'>  <input type='button' class='btn btn-default' name='Optie".$i."' value=". $row["optie3"]." id='optie3".$i."'> </a> </br>".

         "</br> <a href='http://example.com/quiz.php?question=".$k++."'>  <input type='button' class='btn btn-default' name='Optie".$i."' value=". $row["optie4"]." id='optie4".$i."'> </a> </br>".

         "</br></br>
         </ul>";
         $i++;
         $k++;
     }
} else {
     echo "0 results";

}

echo $k;
?>

Upvotes: 0

Related Questions