DEV Community

JeevaSaravanan
JeevaSaravanan

Posted on

Display records from Database one at a time

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(); 
       ?>

Top comments (1)

Collapse
 
njungejnr profile image
ChangeIsMe

I will try to answer your question based on what I have understood.

  1. SELECT * FROM cquest ORDER BY yourprimkey DESC LIMIT 1
    -This will return only one record, am not sure whether they have a systematic order, in such a case, just change your 'ORDER BY...*' but retain the LIMIT 1.

  2. When a user clicks next to go to next question, you can pass a parameter . of question id and increment by one, ie yourprimkeyid+1 or yourprimkey -1 to get the next question then just do
    SELECT * FROM cquest where yourprimkeyid = yourprimkeyid+1

I hope thats answers your question.