mardi 10 février 2015

Dynamic Quiz PHP

I am trying to create a quiz using php with a mysql backend. Heres what i have so far I have a logging in system which caches the login. I have a full database structure with a good few different tables What i need is a way to pull the questions i want from the table and display them with a answer given by the database (these arent yes or no answers and they should all have there own score -1 +2 etc.


This is my Question Table Stucture



# Name Type Collation Attributes Null Default
1 QuestionId varchar(20) latin1_swedish_ci No None
2 QuestionText varchar(1000) latin1_swedish_ci No None
3 QuestionActive bit(1) No 1
4 DateCreated timestamp No CURRENT_TIMESTAMP
5 InputTypeId tinyint(3) No None
6 OptionGroupId smallint(6) No None
7 QuestionSectionId smallint(6) UNSIGNED No None
8 QuestionRequirementId int(10) UNSIGNED Yes NULL


which will then when a answer is answered will submit to the answers table



# Name Type Collation Attributes Null Default
1 AnswersId int(10) UNSIGNED No None AUTO_INCREMENT
2 QuestionOptionId int(10) UNSIGNED No None
3 UserId int(10) UNSIGNED No None
4 SubmittedOn timestamp No CURRENT_TIMESTAMP
5 SessionId varchar(20) latin1_swedish_ci Yes NULL
6 AnswerText varchar(45) latin1_swedish_ci Yes NULL
7 AnswerT/F bit(1) Yes NULL
8 Answer int(5)


Okay my problem I want my code to read in a QuestionId which in turn displays the text and the answer types will be hardcoded. Once you answer a question it stores on the database. I am sure theres a simple way to do this I just don't know what it is and the various online forms I have looked at arent what i want to do.


Code So far



<?php
session_start();
//check if the user is already logged in.
if (!isset($_SESSION['username'])) {
header('Location: index.php');
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>Home</title>
<meta charset="utf-8">
<meta name="format-detection" content="telephone=no" />
<link rel="icon" href="images/favicon.ico" type="image/x-icon">
<link rel="stylesheet" href="css/grid.css">
<link rel="stylesheet" href="css/style.css">
<link rel="stylesheet" href="css/jquery.fancybox.css">
<link rel="stylesheet" href="css/owl-carousel.css">
<link rel="stylesheet" href="css/animate.css">


<script>
$(document).ready(function () {
if ($('html').hasClass('desktop')) {
new WOW().init();
}
});
</script>
<!--<![endif]-->
<script src="js/jquery.fancybox.js"></script>
<script src="js/jquery.fancybox-media.js"></script>
<script src="js/owl.carousel.min.js"></script>

<!--[if lt IE 9]>
<html class="lt-ie9">
<div id="ie6-alert" style="width: 100%; text-align:center; background: #232323;">
<img src="http://ift.tt/178LYEr" alt="Upgrade IE 6" width="640" height="344" border="0"
usemap="#Map" longdesc="http://ift.tt/1KDGKP4"/>
<map name="Map" id="Map">
<area shape="rect" coords="496,201,604,329"
href="http://ift.tt/nMFBAy" target="_blank"
alt="Download Interent Explorer"/>
<area shape="rect" coords="380,201,488,329" href="http://ift.tt/wFxxFW" target="_blank"
alt="Download Apple Safari"/>
<area shape="rect" coords="268,202,376,330" href="http://ift.tt/Ls1SK3" target="_blank"
alt="Download Opera"/>
<area shape="rect" coords="155,202,263,330" href="http://www.mozilla.com/" target="_blank"
alt="Download Firefox"/>
<area shape="rect" coords="35,201,143,329" href="http://ift.tt/e0e4R6" target="_blank"
alt="Download Google Chrome"/>
</map>
</div>

<script src="js/html5shiv.js"></script>
<link rel="stylesheet" type="text/css" media="screen" href="css/ie.css">
<![endif]-->
</head>

<body>

<div class="page">
<!--========================================================
HEADER
=========================================================-->
<header id="header" class="header">
<div class="header-bg">
<div class="container">
<?php
echo "Welcome ". $_SESSION['username'];
?>
<a href="logout.php">Logout</a>
<div class="row">
<div class="grid_6">
<div class="middle-box">
<div class="middle-box_wr">

<div class="middle-box_cnt">
<div class="hdr-brand">
<h1>
<a href="./">APPA</a>
</h1>
<p>Personal Personality Application </p>
</div>
</div>
</div>
</div>
</div>
<div class="grid_6 well__ins1 bg-image4 maxheight1"></div>
</div>
</div>
</div>
<div class="bg1">
<div id="stuck_container" class="stuck_container">
<div class="container">
<div class="row">
<div class="grid_12">
<nav class="nav">
<ul class="sf-menu">
<li>
<a href="indexlogged.php">Home</a>
</li>
<li >
<a href="importing.php">CSV Import</a>
</li>
<li >
<a href="logochanger.php">LOGO CHANGER</a>
</li>
<li>
<a href="populate.php">POPULATE DATABASE</a>
</li>
<li class="active">
<a href="test.php">Test Quiz</a>
</li>
</ul>
</nav>
</div>
</div>
</div>
</div>


</div>
</header>
<!--========================================================
CONTENT
=========================================================-->
<section id="content" class="content">

<form action="testingq.php" method="post">
<p>

<?php
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "this";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

$sql = "SELECT QuestionId, QuestionText, InputTypeId FROM question";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<br> QuestionId: ". $row["QuestionId"]. " - : ". $row["QuestionText"]."<br>";
}
} else {
echo "0 results";
}

$conn->close();
?>
<?php

$host="localhost"; // Host name
$user="root"; // Mysql username
$pass="root"; // Mysql password
$db_name="this"; // Database name

// Connect to server and select databse.
$con = mysqli_connect($host, $user, $pass, $db_name);


$sql = "SELECT UserId FROM user WHERE username='$username'";

$result = mysqli_query($con, $sql);

?>
<input type="Radio" name="Answer" value="1" checked/>Yes</p>
<input type="Radio" name="Answer" value="0"/>No</p>
<p><input type="submit" value="Send it!"></p>
</form>

</section>



<!--========================================================
FOOTER
=========================================================-->
<footer id="footer" class="footer">
<div class="container">
<div class="row">
<div class="grid_12 wow fadeInUp">
<div class="ftr-brand">
<h1>
<a href="./">This</a>
</h1>
<p>This</p>
</div>
<div class="info">
© <span id="copyright-year"></span> | <a href="index-5.html">Privacy Policy</a>
<!-- {%FOOTER_LINK} -->
</div>

<ul class="socials">
<li><a href="#" class="fa-facebook">Facebook</a></li>
<li><a href="#" class="fa-rss">RSS</a></li>
<li><a href="#" class="fa-twitter">Twitter</a></li>
<li><a href="#" class="fa-google-plus">Google Plus</a></li>
</ul>
</div>
</div>
</div>
</footer>
</div>


</body>
</html>


testingq.php



$host="localhost"; // Host name
$user="root"; // Mysql username
$pass="root"; // Mysql password
$db_name="this"; // Database name

// Connect to server and select databse.
$con = mysqli_connect($host, $user, $pass, $db_name);


$sql = "INSERT into answers (QuestionId, UserId, Answer) VALUES(
'$_POST[QuestionId]',
'$_POST[UserId]',
'$_POST[Answer]')";

$result = mysqli_query($con, $sql);

?>


If anyone could lend a hand that would be great !.


Thanks again





Aucun commentaire:

Enregistrer un commentaire