Php code for Online Quiz

Php code for Online Quiz


Php code for Online Quiz

One Question will appear on a screen when user select the option and click on next question another question appear on screen and after all opting the answer user will click on finish online test then quiz result report will be appear on screen .

Mysql Table “Quiz”

CREATE TABLE `quiz` (

  `qid` int(5) default NULL,

  `Question` text,

  `opt1` text,

  `opt2` text,

  `opt3` text,

  `opt4` text,

  `woptcode` varchar(5) default NULL

) ENGINE=InnoDB DEFAULT CHARSET=utf8;

Quiz.php

<!DOCTYPE html>

<html lang="en">

<head>

<title>Online Quiz</title>

<meta charset="utf-8">

<meta name="viewport" content="width=device-width, initial-scale=1">

<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">

<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>

</head>

<body class="m-4">

<a href="index.php" >

<div class="mt-4 p-5 bg-primary text-white rounded">

  <h1>Online Quiz</h1>

</div>

</a> <br />

<form name="quiz" method="post" action="quiz.php">

  <?php

 $mysqli = new mysqli("localhost","","","tutorial");

 

// Check connection

if ($mysqli -> connect_errno) {

echo "Failed to connect to MySQL: " . $mysqli -> connect_error;

exit();

}

error_reporting(0); 

if($_POST["do"]=="finish")

{

$rans=$_POST["rans"];

$tq=$_POST["tq"];

$end=$_POST["end"];

$startposition=$_POST["startposition"];

echo "<table cellpadding='5px' align='center' style='border:1px solid silver' width='100%' >";

echo "<tr><td>Total Question Attempt</td><td>",$tq,"</td><tr>";

echo "<tr><td>Correct Answer</td><td>",$rans,"</td></tr>";

echo "<tr><td>Wrong Answer</td><td>",$tq-$rans,"</td></tr>";

echo "<tr><td>Correct Answer Percentage</td><td>",$rans/$tq*100,"%</td></tr>";

echo "<tr><td>Wrong Answer Percenntage</td><td>",($tq-$rans)/$tq*100,"%</td></tr>";

echo "</table><br><br>";

$query="select * from quiz where qid<='$end' and qid>='$startposition'";

echo "<table cellpadding='5px' align='center' style='border:1px solid silver; width:100%'>";

echo "<tr><th colspan='4' id='heading'>Online Quiz Test Question</td></tr>";

$result=mysqli_query($mysqli,$query);

while ($row = mysqli_fetch_array($result)) {

 echo "<tr><td>",$row[0],"</td><td colspan='2'>",$row[1],"</td></tr><tr><td></td>";

echo "<td colspan='2'>A. ",$row[2],"</td>";

echo "<td colspan='2'>B. ",$row[3],"</td></tr>";

echo "<tr><td></td><td colspan='2'>C. ",$row[4],"</td>";

echo "<td colspan='1'>D. ",$row[5],"</td></tr>";

echo "<tr><td colspan='4' align='right' style='color:orange'>Correct option is ",strtoupper($row[6]),"</td></tr>";

echo "<tr><td colspan='4' align='right' style='color:orange'><hr></td></tr>";

 }

 echo "</table>";

 echo "<p align='right'><a href='#' onclick='window.print()'>Print</a></p>";

 echo "<div style='visibility:hidden;display:none'>";

}

?>

  <table cellpadding="5px" width="100%" style="border:1px solid silver">

    <?php

$start=$_POST["start"];

$s=$_POST["startposition"];

if($start==NULL)

{

$start=$_GET["start"];

$s=$_GET["start"];

}

$useropt=$_POST["useropt"];

$qid=$_POST["qid"];

$rans=$_POST["rans"];

$name=$_POST["name"];

$totalquestion=$_POST["totalquestion"];

if($start==NULL)

$query="select * from quiz where qid='1'";

else

{

$query="select * from quiz where qid='$start'";

}

$result=mysqli_query($mysqli,$query);

while ($row = mysqli_fetch_array($result)) {

    echo "<tr><td>",$row[0],"</td><td colspan='2'>",$row[1],"</td></tr><tr><td></td><td colspan='2'><input type='radio' name='useropt' value='a' />  ",$row[2],"</td><td colspan='2'><input type='radio' name='useropt' value='b' /> ",$row[3],"</td></tr><tr><td></td><td colspan='2'><input type='radio' name='useropt' value='c' /> ",$row[4],"</td><td colspan='2'><input type='radio' name='useropt' value='d' /> ",$row[5],"</td></tr>";

echo "<tr ><td colspan='5' align='right'><input type='hidden' name='name' value='",$name,"'><input type='hidden' name='start' value='",$row[0]+1,"'><input type='hidden' name='qid' value='",$row[0],"'><input type='hidden' name='startposition' value='",$s,"'><input type='submit' value='Next Question'><input type='hidden' name='totalquestion' value='",$totalquestion+1,"'>";

 echo "</td></tr>";

}

echo "<tr><td colspan='4'>";

$query="select woptcode from quiz where qid='$qid'";

 $result=mysqli_query($mysqli,$query);

 while ($row = mysqli_fetch_array($result)) {

 if(strcmp($row[0],$useropt)==0)

 {

 echo "<input type='hidden' name='rans' value='",$rans+1,"'>";

 $rans=$rans+1;

 }

 else

 echo "<input type='hidden' name='rans' value='",$rans,"'>";

 }

 echo "</td></tr>";

?>

  </table>

  <center>

  <br />

  <br />

</form>

<form method="post" action="quiz.php">

  <input type="hidden" name="do" value="finish" />

  <input type="hidden" name="rans" value="<?php echo $rans;?>" />

  <input type="hidden" name="name" value="<?php echo $name;?>" />

  <input type="hidden" name="tq" value="<?php echo $totalquestion;?>" />

  <input type="hidden" name="end"  value="<?php echo $start-1;?>" />

  <input type="hidden" name="startposition" value="<?php echo $s;?>" />

  <input type="submit" value="Finish Online Test"  />

</form>

</body>

</html>

 

Quiz_Entry.php

<!DOCTYPE html>

<html lang="en">

<head>

<title>Online Quiz</title>

<meta charset="utf-8">

<meta name="viewport" content="width=device-width, initial-scale=1">

<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">

<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">

<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>

</head>

<body class="m-4">

<a href="index.php" >

<div class="mt-4 p-5 bg-primary text-white rounded">

  <h1>Online Quiz</h1>

</div>

</a>

<?php

 $mysqli = new mysqli("localhost","","","tutorial");

 

// Check connection

if ($mysqli -> connect_errno) {

echo "Failed to connect to MySQL: " . $mysqli -> connect_error;

exit();

}

 error_reporting(0);

 if($_POST["do"]=="quiz")

{

$question=$_POST["question"];

$opt1=$_POST["opt1"];

$opt2=$_POST["opt2"];

$opt3=$_POST["opt3"];

$opt4=$_POST["opt4"];

$woptcode=$_POST["woptcode"];

$query="select * from quiz ";

$temp=1;

 $result=mysqli_query($mysqli,$query);

while ($row = mysqli_fetch_array($result)) {

$temp=$temp+1;

}

$query="insert into quiz values($temp,'$question','$opt1','$opt2','$opt3','$opt4','$woptcode')";

$result=mysqli_query($mysqli,$query);

echo "<br><br><center>successfully Saved</center><br><br>";

}

?>

<form method="post" >

  <table align="center" class="table table-hover" >

    <tr>

      <td colspan="2" class="table-dark">Online Quiz Test Question Entry Module</td>

    </tr>

    <tr>

      <td>Enter Question here </td>

      <td><input type="text" name="question" style="width:100%"  /></td>

    </tr>

    <tr>

      <td>Enter First option</td>

      <td><input type="text" name="opt1"  style="width:100%" /></td>

    </tr>

    <tr>

      <td>Enter Second option</td>

      <td><input type="text" name="opt2"  style="width:100%" /></td>

    </tr>

    <tr>

      <td>Enter Third option</td>

      <td><input type="text" name="opt3"  style="width:100%" /></td>

    </tr>

    <tr>

      <td>Enter Fourth option</td>

      <td><input type="text" name="opt4"   style="width:100%"/></td>

    </tr>

    <tr>

      <td>Select Right Option code</td>

      <td><select name="woptcode"  style="width:100%">

          <option value="a">A</option>

          <option value="b">B</option>

          <option value="c">C</option>

          <option value="d">D</option>

        </select>

      </td>

    </tr>

    <tr>

      <td colspan="2" align="center"><input type="hidden" name="do" value="quiz" />

        <input type="submit" value="SAVE QUESTION"  class='btn btn-primary' />

      </td>

    </tr>

  </table>

</form>

</body>

</html>

 

If you have any query regarding this, Please comment



Post a Comment

1 Comments

  1. PHP is a general-purpose scripting language geared toward web development. It was originally created by Danish-Canadian programmer Rasmus Lerdorf in 1994. The PHP reference implementation is now produced by The PHP Group. PHP originally stood for Personal Home Page, but it now stands for the recursive initialism PHP: Hypertext Preprocessor.

    PHP code is usually processed on a web server by a PHP interpreter implemented as a module, a daemon or as a Common Gateway Interface (CGI) executable. On a web server, the result of the interpreted and executed PHP code – which may be any type of data, such as generated HTML or binary image data – would form the whole or part of an HTTP response. Various web template systems, web content management systems, and web frameworks exist which can be employed to orchestrate or facilitate the generation of that response. Additionally, PHP can be used for many programming tasks outside the web context, such as standalone graphical applications and robotic drone control. PHP code can also be directly executed from the command line.

    The standard PHP interpreter, powered by the Zend Engine, is free software released under the PHP License. PHP has been widely ported and can be deployed on most web servers on a variety of operating systems and platforms.

    The PHP language evolved without a written formal specification or standard until 2014, with the original implementation acting as the de facto standard which other implementations aimed to follow. Since 2014, work has gone on to create a formal PHP specification.

    W3Techs reports that, as of January 2022, "PHP is used by 78.1% of all the websites whose server-side programming language we know." PHP version 7.4 is the most used version. Support for version 7.3 was dropped on 6 December 2021.

    ReplyDelete