Display records from Database one at a time
Tags: php, javascript, sql
Published: 2019-12-08 11:18:33

Can anyone tell me how to retrieve records from database one at a time.

I am attaching my code here:

Let me explain the code..

The database I am using is MariaDB.

There is a table named "cquest" (containing some questions) in database with a field named "question" and options are in field "opt1,opt2,opt3,opt4".

The below code display all the questions stored in the table into the screen.

My problem is here: I want to display the questions one by one. Like when we click the next button it will display the next question and so until all the records are displayed.

Help me out with the main logic I should use to implement it. Or help me with the script.

           $servername = "localhost";
           $username = "root";
           $password = "";
           $dbname = "quiz";
           $conn = new mysqli($servername, $username, $password, $dbname);
           if ($conn->connect_error) {
            die("Connection failed: " . $conn->connect_error);
            }

           $sql = "SELECT * FROM cquest";
           $result = $conn->query($sql);
           if ($result->num_rows > 0) {
            while($row = $result->fetch_assoc()) {
              echo "<div class='row'>";
                echo "<div class='col-md-6 mb-6'>";
                echo "<div class='card shadow border-0 h-100'>";
                echo "<div class='card-body'>";
                echo "<h5 class='dark-text'>".$row['question']."</h5>";
                echo "<div class='bg-light'>";
                echo "<code class='dark-text'>".$row['ccode']."</code>";
              echo "</div>";
                echo "<div class='custom-control custom-radio'>";
                      echo "<input type='radio' class='custom-control-input' id='defaultGroupExample1'  name='ans' value='a' checked>";
                      echo "<label class='custom-control-label' for=defaultGroupExample1>".$row["opt1"]."</label></br>";
                      echo "</div>";
                      echo "<div class='custom-control custom-radio'>";
                      echo "<input type='radio' class='custom-control-input' id='defaultGroupExample2'  name='ans' value='b'>";
                      echo "<label class='custom-control-label' for=defaultGroupExample2>".$row["opt2"]."</label></br>";
                      echo "</div>";
                      echo "<div class='custom-control custom-radio'>";
                      echo "<input type='radio' class='custom-control-input' id='defaultGroupExample3'  name='ans' value='c'>";
                      echo "<label class='custom-control-label' for=defaultGroupExample3>".$row["opt3"]."</label></br>";
                      echo "</div>";
                      echo "<div class='custom-control custom-radio'>";
                      echo "<input  type='radio' class='custom-control-input' id='defaultGroupExample4' name='ans' value='d'>";
                      echo "<label class='custom-control-label' for=defaultGroupExample4>".$row["opt4"]."</label></br>";
                      echo "</div>";
                      echo "<button name=submit class=next>NEXT</button>";
                      echo "</div>";
                      echo "</div>";
                      echo "</div>";
                      echo "</div>";

            }
            }
           }
           else {
            echo "Currently Unavailable";
           }
            $conn->close(); 
           ?>