I am building a game for a uni assignment where the user answers yes or no to computer question and the computer will guess that animal. It uses a self referencing database for the backend and is being coded in php.
I thought the best way to to this would be to create a variable called $currentnode to keep track of where the game was in terms of the current question being asked, and use that to make a decision of the next question.
I have tried to use a switch statement on this which should be triggered by the the yes or no button being pressed.The switch should take the currentnode and match it with a case and update the where clause and run the query to select the correct question from the database. I have started by using Isset on the yes button and building a switch for all yes answers, but I cannot get the switch statement to be trigger when the yes button is pressed.
My aim is for every button press of yes or no, a switch statement should be entered, the currrentnode matched an the corresponding where clause added to the query and ran.
Any help would be much appreciated.
[php]
Creatures Expert System <?php $dbconn = mysqli_connect("localhost", "s573022", "destroyer08") or die ("Could not connect to database server."); mysqli_select_db($dbconn, "s573022") or die ("Could not select db."); $query = "SELECT `message`, `nodeID` FROM `creature`";//basic query that will be used at all times $where = "";//empty where clause, will be altered $currentnode;//used to store current state //function created to be re used after an answer is given function output($query,$where,$dbconn){ $result = mysqli_query($dbconn, $query.$where); while($row = mysqli_fetch_assoc($result)) { echo "{$row['message']} {$row['nodeID']}";} echo ' ';} function outputanswer($query,$where,$dbconn){ $result = mysqli_query($dbconn, $query.$where); while($row = mysqli_fetch_assoc($result)) { echo "{$row['message']} {$row['nodeID']}
";} echo ' ';} ?> <?php $currentnode = '0'; if ( isset( $_POST['start'] )) { $currentnode = 1; $where = "WHERE `nodeID` = '1'"; output($query,$where,$dbconn); echo 'current node is '; echo $currentnode; } if (isset ($_GET['answer']) && ($_GET['answer']) == 'yes'){ echo "yes pressed"; switch ($currentnode){ case 1: $where = "WHERE `nodeID` = '2'"; output($query,$where,$dbconn); $currentnode = 2; break; case 2: $where = "WHERE `nodeID = '3'"; outputanswer($query,$where,$dbconn); $currentnode = 3; break; } } if (isset ($_GET['answer']) && ($_GET['answer']) == 'no'){ echo "no pressed";} //maybe the game loop needs to involve a huge nest elseif statement? otherwise the current node needs to stored in order to //determine which decision needs to me made. The first one is easy as it is obvious at what stage the game is at, it is harder from then //as it is not possible to know where you are on the binary tree //now using $currentnode to store the state of the program. ?>
[/php]