i am new to php and i am trying to build a php quiz, that read questions and answers from the mysql db. so far i have the following code:
require_once("scripts/connect_db.php");
session_start();
$_SESSION['Qid'] = 1;
if (!isset($_POST['answer'])){
$Qid = $_SESSION['Qid'];
echo $Qid;
$stmt = $dbh->prepare("Select * FROM qtable WHERE id = ?");
$stmt->bindParam(1, $Qid);
$stmt->execute();
$result = $stmt->fetchObject();
echo $result->Q . "<br />";
$stmt = $dbh->prepare("SELECT * FROM atable WHERE Qid = ? ORDER BY rand()");
$stmt->bindParam(1, $Qid);
$stmt->execute();
$result = $stmt->fetchAll();
echo "<form action=\"test1.php\" method=\"post\">";
foreach ( $result as $row ) {
if ( $row['correct'] == 1 ){
echo "<input type=\"radio\" name=\"answer\" value=\"1\">" .$row['answer']."<br />";
} else {
echo "<input type=\"radio\" name=\"answer\" value=\"0\">" .$row['answer']."<br />";
}
}
echo "<input type=\"submit\" value=\"next\">";
echo "</form>";
}
else
{
$Qid = $_SESSION['Qid'] + 1;
echo $Qid;
$stmt = $dbh->prepare("Select * FROM qtable WHERE id = ?");
$stmt->bindParam(1, $Qid);
$stmt->execute();
$result = $stmt->fetchObject();
echo $result->Q . "<br />";
$stmt = $dbh->prepare("SELECT * FROM atable WHERE Qid = ? ORDER BY rand()");
$stmt->bindParam(1, $Qid);
$stmt->execute();
$result = $stmt->fetchAll();
echo "<form action=\"test1.php\" method=\"post\">";
foreach ( $result as $row ) {
if ( $row['correct'] == 1 ){
echo "<input type=\"radio\" name=\"answer\" value=\"1\">" .$row['answer']."<br />";
} else {
echo "<input type=\"radio\" name=\"answer\" value=\"0\">" .$row['answer']."<br />";
}
}
echo "<input type=\"submit\" value=\"next\">";
echo "</form>";
}
my idea is to increment $_SESSION['Qid'] by 1 every time i press submit so i can loop through the database, but so far i can only go up to the second row. Can anybody help or point me into the right direction on how to do this with out Jquery or AJAX. Thanks