0

i have created a quiz website. i have created a file like below and t the below file is used to fetch the data comming from it is called using ajax in the main html page.data from database

<?php
// Start the session
session_start();

$con=mysqli_connect("localhost","root","","quiz"); // change here to your data
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

// Check the number of all questions, if next_id is more than last question, back to first or whatever you want;
$response=mysqli_query($con,"select * from prometric");
$number_of_all_questions = mysqli_num_rows($response);

if($_POST['next_id'] == 0){
 // reset to default
 $_SESSION["correct_score"] = 0;
 $_SESSION["not_correct_score"] = 0;
}


if($number_of_all_questions <= $_POST['next_id']){
 // Quiz finished, show results
    echo"<div>
 <h2>Results:</h2>
 <p>Correct answers: {$_SESSION['correct_score']}</p>
 <p>Wrong answers: {$_SESSION['not_correct_score']}</p>

 </div>";



}else{

 // query next question
 $response=mysqli_query($con,"select * from prometric WHERE id =(select min(id) from prometric where id > {$_POST['next_id']})");
 ?>

 <?php while($result=mysqli_fetch_array($response,MYSQLI_ASSOC)){ ?>

  <div id="question_<?= $result['id'] ?>" class='question' data-next-question="<?= $_POST['next_id'] ?>"> <!--check the class for plurals if error occurs-->
   <h2><?= $result['id'].".".$result['question_name'] ?></h2>
   <div class='align'>
    <input type="radio" value="1" id='radio1' name='1'>
    <label id='ans1' for='radio1'><?= $result['answer1'] ?></label>
    <br/>
    <input type="radio" value="2" id='radio2' name='2'>
    <label id='ans2' for='radio2'><?= $result['answer2'] ?></label>
    <br/>
    <input type="radio" value="3" id='radio3' name='3'>
    <label id='ans3' for='radio3'><?= $result['answer3'] ?></label>
    <br/>
    <input type="radio" value="4" id='radio4' name='4'>
    <label id='ans4' for='radio4'><?= $result['answer4'] ?></label>
   </div>
   <br/>
   <?php /*<input type="button" data-next-question="<?= $_POST['next_id'] ?>" id='next' value='Next!' name='question' class='butt'/> */?>
  </div>
 <?php }?>
<?php }?>
<?php mysqli_close($con); ?>

the data comes in the same order every time i fetch it, i want the data to be random and limit the data to certain number. how can i do this

2 Answers2

1

Just use ORDER BY and LIMIT

 $response=mysqli_query($con,"select * from prometric ORDER BY RAND() LIMIT 20");
Devsi Odedra
  • 5,244
  • 1
  • 23
  • 37
0

You can use the shuffle($your_array) function to randomise your array.

For the limit use $response = "select * from prometric LIMIT 100"; if this is what you're looking for.

  • where to add this shuffle function? –  Jun 08 '18 at 05:15
  • `$result=shuffle(mysqli_fetch_array($response,MYSQLI_ASSOC))` Probably clean this code up – code-mocker Jun 08 '18 at 05:37
  • can you tell me how to edit this query to get the id from the url $response=mysqli_query($con,"select * from prometric ORDER BY RAND() LIMIT echo $_GET['id']; "); to limit the questions –  Jun 08 '18 at 05:48