Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/266.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/83.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何在按下按钮时输入开关语句_Php_Sql - Fatal编程技术网

Php 如何在按下按钮时输入开关语句

Php 如何在按下按钮时输入开关语句,php,sql,Php,Sql,我正在为一个uni作业制作一个游戏,在这个游戏中,用户对计算机的问题回答“是”或“否”,计算机就会猜到那个动物。它使用一个自引用数据库作为后端,并用php进行编码 我认为最好的方法是创建一个名为$currentnode的变量,根据当前问题跟踪游戏的位置,并用它来决定下一个问题 我尝试对此使用switch语句,按下yes或no按钮会触发该语句。该语句应使用currentnode并将其与案例匹配,更新where子句并运行查询以从数据库中选择正确的问题。 我已经开始在yes按钮上使用Isset,并为所

我正在为一个uni作业制作一个游戏,在这个游戏中,用户对计算机的问题回答“是”或“否”,计算机就会猜到那个动物。它使用一个自引用数据库作为后端,并用php进行编码

我认为最好的方法是创建一个名为$currentnode的变量,根据当前问题跟踪游戏的位置,并用它来决定下一个问题

我尝试对此使用switch语句,按下yes或no按钮会触发该语句。该语句应使用currentnode并将其与案例匹配,更新where子句并运行查询以从数据库中选择正确的问题。 我已经开始在yes按钮上使用Isset,并为所有yes答案构建一个开关,但我无法在按下yes按钮时触发switch语句

我的目标是,每次按yes或no按钮时,都应该输入一个switch语句,currentNode匹配相应的where子句并将其添加到查询中并运行

任何帮助都将不胜感激

$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']}<br>";}


 echo '<form action="play.php" method="GET">
<input type="submit" name="yes" value="yes">
<input type="submit" name="no" value="no">
<input type = "submit" name = "restart" value = "restart game">
</form>';}

?>

<form action="play.php" method="POST">
<input type="submit" name="start" value="Start Game">
</form>

<?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['yes'])){

switch ($currentnode){

case 1: $where = "WHERE `nodeID` = '2'"; output($query,$where,$dbconn);
break;

case 2: $where = "WHERE `nodeID = '3'"; output($query,$where,$dbconn);
break;
}
}

?>
</body>
</html>
$query=“从“生物”中选择“消息”、“节点标识”//始终使用的基本查询
$where=“”//空where子句,将被更改
$currentnode//用于存储当前状态
//在给出答案后重新使用的函数
函数输出($query,$where,$dbconn){
$result=mysqli_查询($dbconn,$query.$where);
while($row=mysqli\u fetch\u assoc($result)){
回显“{$row['message']}
{$row['nodeID']}
“;} 回声' ';} ?>
作为一个交互式命令行应用程序,用户输入数据,程序等待,直到用户按下ENTER键。然后你可以继续你的代码不知道怎样才能改变这个问题来缩小范围?也许可以画一幅画。我想你想问的更多的是结构或设计问题。您可能会遇到程序流中的状态,而PHP/HTTP更像是一种无状态的方式。例如,将
$currentnode
作为参数传递到脚本中。我的问题是,当我将switch语句放在if块中测试按钮是否按下时,如何让它执行任何操作。此时,当按下“是”按钮时,什么也没有发生。您只需将其放入该块即可。也许PHP手册页面对if有帮助?