Responsive Quiz Application Using PHP, MySQL, jQuery, Ajax and Twitter Bootstrap Part-3

Posted by & filed under Ajax, JQUERY, MYSQL, PHP.

In this tutorial I made few changes in the Responsive Quiz Application Using PHP, MySQL, jQuery, Ajax and Twitter Bootstrap based on the request from users.

Note : You can refer my latest Quiz application
 
Responsive PHP Quiz Script

Before continuing this tutorial please refer my previous tutorials on this Responsive Quiz Application Using PHP, MySQL, jQuery, Ajax and Twitter Bootstrap.

1.  Responsive Quiz Application Using PHP, MySQL, jQuery, Ajax and Twitter Bootstrap

2. Responsive Quiz Application Using PHP, MySQL, jQuery, Ajax and Twitter Bootstrap Part-2

Now user can able select number of questions to be showed on each pages.

 

 

Responsive Quiz Application Using PHP, MySQL, jQuery, Ajax and Twitter Bootstrap - 3

Responsive Quiz Application Using PHP, MySQL, jQuery, Ajax and Twitter Bootstrap – 3

Step 1:

$number_question = $_POST['num_questions'];
$row = mysqli_query( $con, "select * from questions where category_id=$category ORDER BY RAND()");
$rowcount = mysqli_num_rows( $row );
$remainder = $rowcount/$number_question;

where

$number_question — set number of questions to be showed on each pages.

I am selecting quiz questions from particular category that the user choosed, then finding number questions in the result of mysqli_query() finally stored questions count in  $rowcount variable. Finally finding the remainder by dividing $rowcount  with $number_question (number of question to be showed on each page.)

 

 

Step 2:

<?php while ( $result = mysqli_fetch_assoc($row) ) {
						 if ( $i == 0) echo "<div class='cont' id='question_splitter_$j'>";?>
						<div id='question<?php echo $k;?>' >
						<p class='questions' id="qname<?php echo $j;?>"> <?php echo $k?>.<?php echo $result['question_name'];?></p>
						<input type="radio" value="1" id='radio1_<?php echo $result['id'];?>' name='<?php echo $result['id'];?>'/><?php echo $result['answer1'];?>
						<br/>
						<input type="radio" value="2" id='radio1_<?php echo $result['id'];?>' name='<?php echo $result['id'];?>'/><?php echo $result['answer2'];?>
						<br/>
						<input type="radio" value="3" id='radio1_<?php echo $result['id'];?>' name='<?php echo $result['id'];?>'/><?php echo $result['answer3'];?>
						<br/>
						<input type="radio" value="4" id='radio1_<?php echo $result['id'];?>' name='<?php echo $result['id'];?>'/><?php echo $result['answer4'];?>
						<br/>
						<input type="radio" checked='checked' style='display:none' value="5" id='radio1_<?php echo $result['id'];?>' name='<?php echo $result['id'];?>'/>                                                                      
						<br/>
						</div>
						<?php
							 $i++; 
							 if ( ( $remainder < 1 ) || ( $i == $number_question && $remainder == 1 ) ) {
							 	echo "<button id='".$j."' class='next btn btn-success' type='submit'>Finish</button>";
							 	echo "</div>";
							 }  elseif ( $rowcount > $number_question  ) {
							 	if ( $j == 1 && $i == $number_question ) {
									echo "<button id='".$j."' class='next btn btn-success' type='button'>Next</button>";
									echo "</div>";
									$i = 0;
									$j++;           
								} elseif ( $k == $rowcount ) { 
									echo " <button id='".$j."' class='previous btn btn-success' type='button'>Previous</button>
												<button id='".$j."' class='next btn btn-success' type='submit'>Finish</button>";
									echo "</div>";
									$i = 0;
									$j++;
								} elseif ( $j > 1 && $i == $number_question ) {
									echo "<button id='".$j."' class='previous btn btn-success' type='button'>Previous</button>
							                    <button id='".$j."' class='next btn btn-success' type='button' >Next</button>";
									echo "</div>";
									$i = 0;
									$j++;
								}

							 }
							  $k++;
					     } ?>

For example if I am going to show two questions on each page, if user choose category that contains only one question then I need to show that one question with finish button to complete quiz.

If $remainder<1 that means number  questions less than number questions to be showed on each page, so I need to complete quiz with finish button.

if ( ( $remainder < 1 ) || ( $i == $number_question && $remainder == 1 ) ) {
	echo "<button id='".$j."' class='next btn btn-success' type='submit'>Finish</button>";
	echo "</div>";
 }

if number questions goes greater than number questions to be showed on each page then following logic will come into play.

elseif ( $rowcount > $number_question  ) {
  if ( $j == 1 && $i == $number_question ) {
    echo "<button id='".$j."' class='next btn btn-success' type='button'>Next</button>";
    echo "</div>";
    $i = 0;
    $j++;           
  } elseif ( $k == $rowcount ) { 
    echo " <button id='".$j."' class='previous btn btn-success' type='button'>Previous</button>
		<button id='".$j."' class='next btn btn-success' type='submit'>Finish</button>";
    echo "</div>";
    $i = 0;
    $j++;
  } elseif ( $j > 1 && $i == $number_question ) {
     echo "<button id='".$j."' class='previous btn btn-success' type='button'>Previous</button>
		 <button id='".$j."' class='next btn btn-success' type='button' >Next</button>";
     echo "</div>";
     $i = 0;
     $j++;
}

.

Download Premium Only Scripts & 80+ Demo scripts Instantly at just 1.95 USD per month + 10% discount to all Exclusive Scripts

If you want any of my script need to be customized according to your business requirement,

Please feel free to contact me [at] muni[at]smarttutorials.net

Note: But it will be charged based on your customization requirement

Get Updates, Scripts & Other Useful Resources to your Email

Join 10,000+ Happy Subscribers on feedburner. Click to Subscribe (We don't send spam)
Every Email Subsciber could have access to download 100+ demo scripts & all future scripts.

Get Instant Script Download Access!