I am building a simple web page with a 20 questions style game. The first question is presented and the user answers yes or no get to the next question. I am unsure on how to point to the next required node with my query.
I have radio buttons for selecting the answer and a submit button. I presume I need to hand over something with the radio buttons with $_GET
, but I am really struggling. I am assuming that I need a way to know which node the user is on to the find the correct answer node.
<form action="play.php" method="GET">
<input type="submit" name="start" value="start game">
</form>
<?php
$query="SELECT `message`, `parentID`,`answerYesID`, `answerNoID`, `nodeID` FROM `creature`";
$where="";
$current=1;
function output($query, $where, $dbconn) {
$result=mysqli_query($dbconn, $query.$where);
while($row=mysqli_fetch_assoc($result)) {
echo("<tr><td>{$row['message']}</td></tr><br>");
echo "</table>";
}
}
echo '<form action="play.php" method="GET">
<input type="radio" name="answer" value ="`answerYesID`">yes
<input type="radio" name="answer" value="`answerNoID`">no
<input type="submit" name="submit" value="submit">
</form>';
if(isset($_GET['start'])) {
$where="WHERE `parentID` is NULL";
output($query, $where, $dbconn);
}
if(isset($_GET['submit']) && $_GET['answer'] == "answerYesID") {
$where="WHERE `nodeID` = ".$_GET['answer'];
output($query, $where, $dbconn);
}
?>
</body>
</html>
you can give a Question ID to each question and get data according to this Question ID
Make ID column an auto_increment
get the id using GET method and the for next button just increment by one
$next=$_GET['id']+1;
and make sure that this number doesn't exceed 20(number of Question)
$next%=20;
now action atribute for for would be
action="play.php?id=<?php echo $next;?>"