2014-04-29 4 views
0

사용자 입력의 예 또는 아니오 응답에 따라 자체 참조 테이블에서 노드를 선택할 수 있어야합니다. 그것은 생물을 추측하는 게임입니다. "where parentID IS NULL"을 사용하여 첫 번째 노드를 선택할 수 있습니다. 사용자가 선택하는 두 개의 그룹화 된 라디오 버튼이 있고 제출 버튼이 응답을 보내기 위해 사용되는 경우 양식 작업은 GET입니다. 나는 내가 현재 nodeID를 가지고 있고 라디오 버튼을 기반으로 표시 할 필요가있는 운동을해야한다는 것을 알고있다. 그러나 이것이 내가 붙어있는 곳이다.사용자 선택에 따라 이진 트리를 어떻게 트래버스합니까?

내 테이블 구조는 다음과 같습니다 nodeID, parentID, answerYesID, answerNoID, message. 해당되는 경우 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> 
+0

제대로 작동하지 않는 기능은 무엇입니까? – Skewled

+0

어떤 노드가 현재 노드인지 검색하는 방법과 라디오 버튼을 사용하여 다음 nodeID를 제공하는 방법을 모르겠습니다. – user3519506

답변

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> 

당신이 다른 페이지에서 데이터를 가져오고 사용하는 경우 $_GET 사용할 수 있습니다, 당신은 $_POST$_GET와 함께 무엇을하고 있는지 불분명하다 위에 게시 한 코드를 전달 된 데이터로 작업하십시오. 그렇지 않으면 나는 그것을 게시하기 위해 바꾸었고 $_GET['nodeID']에 대해 $_GET을 남겼다.

학습 경험이 없기 때문에 코드를 다시 작성하고 싶지는 않지만 Stack-Codeforme는 아니지만 코드가있는 문제에 대해 질문 할 수있는 좋은 곳입니다. 형식이 지정되어 있고 대부분은 작동하지만 아직 해결할 수없는 것에 집착합니다.

게시 한 코드가 잘 계획되어 있지 않고 보안이 설정되지 않았으므로 논리적 인 흐름으로 다시 고려해야합니다. 당신의 마음 속에서 그 과정을 생각해보고 그것을 종이에 적어보십시오. 쿼리를 설정하고 작동하는 무언가를 얻은 후에는 쿼리가 실패했거나 코드 결과로 알 수없는 오류가 발생하면 여기에 도움을 요청해야합니다. 느슨하게 다른 사람들이 빈칸을 채울 수 있도록 코드의 골격을 게시하면 도움을받을 수있는 좋은 방법이 아니며, 아무것도 가르쳐주지 않습니다.

스켈링 된

+0

"parentID가 null 인 곳"을 사용하는 이유는 부모 노드가없는 유일한 노드이기 때문에 데이터베이스에서 시작 노드를 찾는 것입니다. '예'출력은 블록이 작동하는지 테스트하는 데 사용되었습니다. 아무 데이터도 얻지 못했습니다. 라디오 버튼 값에 nodeID를 코딩하면 원하는 데이터를 추출 할 수 있지만 코드가 동적 인 것은 아니기 때문에이 코드가 필요합니다. 코드 작성을 요청하는 사람이 없습니다. 하지만 사용자가 요구하는 값을 얻을 수있는 방법을 알려줍니다. – user3519506

관련 문제