如何根据用户选择遍历二叉树?

时间:2014-04-29 17:42:29

标签: php sql

我需要能够根据用户输入的是或否答案从我的自引用表中选择一个节点。这是一个猜测生物的游戏。我可以使用“WHERE parentID IS NULL”选择第一个节点。我有两个用户选择的分组单选按钮,然后使用提交按钮发送响应,表单操作是GET。我知道我需要锻炼我目前正在使用哪个nodeID,以及我需要根据单选按钮显示哪一个,但这就是我被困住的地方。

我的表格结构为:nodeIDparentIDanswerYesIDanswerNoIDmessage。如果适用,parentID,answerYesId和answerNoID都引用同一表中的nodeID。第一个节点在父ID中为NULL,任何答案节点在answerID中都为NULL。

我目前在'是'单选按钮中有answerYesID,在'否'单选按钮中有answerNoID。我花了这么多时间在这上面,我绕圈子走,所以任何帮助都会受到最高的赞赏。

<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 = "";

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);
} 

$current = &_GET['nodeID'];

if (isset($_GET['submit'])){

    if (isset($_GET['answer'])){

        $where = "WHERE `nodeID` ='" . $current. "'"; 
        echo 'yes';
        output($query, $where, $dbconn);
    }
}

?>

</body>

</html>

1 个答案:

答案 0 :(得分:0)

让我们查看您的代码:

<!-- Not sure why you are using GET here when you are posting to the same page
     unless you have some bookmarking feature to resume later I suppose -->
<form action="play.php" method="POST"> <!-- Changed to post -->
<input type="submit" name="start" value="start game">
</form>

<?php
// Perform a query that gets information from table creature WHERE .. not sure why
// you are performing a query every time the page is accessed.
$query = "SELECT `message`, `parentID`,`answerYesID`, `answerNoID`, `nodeID` FROM    `creature`";
$where = "";

// output() takes the query, where statement, and connection to display the output
// in a table
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>";
       }   
}

// display a new form that asks for a yes or no response, change to POST as your
// posting the data to the same page
echo'
<form action="play.php" method="POST">
<input type="radio" name="answer" value =`answerYesID`>yes
<input type="radio" name="answer" value=`answerNoID`>no
<input type="submit" name="submit" value="submit">
</form>';

// Changed to POST as you are calling this from the first form on the page
if (isset($_POST['start'])){
// You are setting the where variable here for the query in your function
$where = "WHERE `parentID` is NULL"; // Grab only where parentID is NULL, not sure
                                     // what you are really trying to do here
// Send the connection, query, and where condition to the output function
output($query,$where,$dbconn);
} 

$current = &_GET['nodeID']; // This is passed from another page to this page?

if (isset($_POST['submit'])){ // Changed to $_POST

    if (isset($_POST['answer'])){ // Changed to $_POST
        // Setup the where variable with the current ID for filtering
        $where = "WHERE `nodeID` ='" . $current. "'"; 
        echo 'yes'; // randomly output yes
        output($query, $where, $dbconn); // send data to output() for displaying results
    }
}

?>

</body>

</html>

目前还不清楚您使用$_POST$_GET做了什么,如果您从其他网页获取数据并使用上面发布的代码来处理数据,则可以使用$_GET传递给它的数据。否则我将其切换为发布并为$_GET留下一个$_GET['nodeID']

我不想重新编写您的代码,因为它对您的学习体验没有任何意义,这不是Stack-Codeforme,但它是一个很好的地方,您可以提出有关代码问题的问题格式化并且大部分都在工作,但是你会遇到无法解决的事情。

您发布的代码计划不周,不安全,应该使用逻辑流重新计算。在脑海中思考这个过程并将其写在纸上。一旦您建立了查询并使某些工作正常,您应该在此处发帖以获得帮助,如果您的查询失败或您的代码导致您无法弄清楚的错误。松散地为其他人发布代码框架以填补空白并不是获得帮助的好方法,也不会教你任何东西。

-Skewled