4

私の質問はとても簡単です。私がここに書いたコードは、ウェブページに全く出力を生成しません。私は一日中それに取り組んできましたが、私が見逃しているのは非常に単純なことだと確信しています。だから私はあなたの気さくな新鮮な目にアピールしています!これがうまくいかない理由を誰かが見つけられたら、とても感謝しています。

前提:

これは、次の条件を持つ意思決定ツリーのオンライン調査です。ユーザーがすでに調査を開始している場合、データベースでそれらを見つけ、最後に回答された質問を見つけて、次の質問を表示します。ただし、開始していない場合は、最初の質問が表示されます。

すべてのアンケートの質問は、デシジョン ツリー ロジックと同様にデータベースにも保持されます (たとえば、ユーザーが質問 1 に対してオプション 2 を選択した場合、質問は 2 ではなく質問 3 に送られます)。

現時点では、関連情報をデータベースから直接更新しており、ウェブサイトで自動化していないと仮定してください。

ありがとう :)

PHP:

    <?php
//Find the latest question reached by the user for display on the page
$sql = mysql_query("SELECT QuestionNumberReached FROM User WHERE EmailAddress = '***'");
$sqlCount = mysql_num_rows($sql);
if ($sqlCount > 0) {
    while ($row = mysql_fetch_array($sql)) {
        $QuestionNumberReached = $row["QuestionNumberReached"];
    }
}
?>

<?php
//Find the last question answered by the user from the database
$StartedQuery = mysql_query("SELECT LastQuestionAnswered FROM User WHERE EmailAddress = '***'");
//Count the number of rows that the query produces
$StartedQueryCount = mysql_num_rows($StartedQuery);
//If data is found, whether it be a number or null, define the value
if ($StartedQueryCount > 0) {
    while ($row = mysql_fetch_array($sql)) {
        $LastQuestionAnswered = $row["LastQuestionAnswered"];
        //If the field has a value and is not null, find the next question from the database
        if (!empty($LastQuestionAnswered)) {
            //Find the User's ID and the ID of the last question answered
            $sqlA = mysql_query("SELECT PKID, LastQuestionAnswered FROM User WHERE EmailAddress = '***'");
            //If the operation produces an error, output an error message
            if (!$sqlA) {
                die('Invalid query for SQLA: ' . mysql_error());
            }
            //Count the number of rows output
            $sqlACount = mysql_num_rows($sqlA);
            //If rows exist, define the values
            if ($sqlACount > 0) {
                while ($row = mysql_fetch_array($sqlA)) {
                    $sqlAPKID = $row["PKID"];
                    $sqlALastQuestionAnswered = $row["LastQuestionAnswered"];
                }
            }
            //Find the answer given by the user to the last answered question
            $sqlB = mysql_query("SELECT Answer FROM Responses WHERE User = $sqlAPKID");
            //If the operation produces an error, output an error message
            if (!$sqlB) {
                die('Invalid query for SQLB: ' . mysql_error());
            }
            //Count the number of rows output
            $sqlBCount = mysql_num_rows($sqlB);
            //If rows exist, define the values
            if ($sqlBCount > 0) {
                while ($row = mysql_fetch_array($sqlB)) {
                    $sqlBAnswer = $row["Answer"];
                }
            }
            //Find the number of the next question to be answered based on the user's previous answer and the question they answered
            $sqlC = mysql_query("SELECT NextQuestion FROM Answers WHERE QuestionNumber = $sqlALastQuestionAnswered AND PKID = $sqlBAnswer");
            //If the operation produces an error, output an error message
            if (!$sqlC) {
                die('Invalid query for SQLC: ' . mysql_error());
            }
            //Count the number of rows output
            $sqlCCount = mysql_num_rows($sqlC);
            //If rows exist, define the values
            if ($sqlCCount > 0) {
                while ($row = mysql_fetch_array($sqlC)) {
                    $sqlCNextQuestion = $row["NextQuestion"];
                }
            }
            //Find the question text pertaining to the ID of the next question that needs to be answered
            $sqlD = mysql_query("SELECT QuestionText FROM Questions WHERE PKID = $sqlCNextQuestion");
            //If the operation produces an error, output an error message
            if (!$sqlD) {
                die('Invalid query for SQLD: ' . mysql_error());
            }
            //Count the number of rows output
            $sqlDCount = mysql_num_rows($sqlD);
            //If rows exist, define the values
            if ($sqlDCount > 0) {
                while ($row = mysql_fetch_array($sqlD)) {
                    $SurveyStartedQuestionText = $row["QuestionText"];
                }
            }
            //Set a string of information that will show the question number and question text as appropriate
            $ToDisplay = '' . $QuestionNumberReached . ': ' . $SurveyStartedQuestionText . '<br /><br />Answer Text Here';
        //If the value for QuestionNumberReached is null, the user has not started the survey
        } else if (empty($LastQuestionAnswered)) {
            //Find the question text of the first question in the survey
            $sql3 = mysql_query("SELECT QuestionText FROM Questions WHERE PKID IN (SELECT FirstQuestion FROM Batch WHERE BatchNumber IN (SELECT BatchNumber FROM User WHERE EmailAddress = '***'))");
            //Count the number of rows output
            $sql3Count = mysql_num_rows($sql3);
            //If rows exist, define the values
            if ($sql3Count > 0) {
                while ($row = mysql_fetch_array($sql3)) {
                    $SurveyNotStartedQuestionText = $row["QuestionText"];
                }
            }
            //Set a string of information that will show the question number and question text as appropriate
            $ToDisplay = '' . $QuestionNumberReached . ': ' . $SurveyNotStartedQuestionText . '<br /><br />Answer Text Here';
        }
    }
}
?>

HTML:

<body>

<?php
// Display the concatenated information that has been previously defined
echo $ToDisplay;
?>

</body>
4

2 に答える 2

0

このビット:

if ($StartedQueryCount > 0) {

おそらく false と評価され、コンテンツを追加する一致する else タグがありません。変更してみてください:

    }
?>

と:

    }
    else {
        $ToDisplay = 'Error: no rows found to display!';
    }
?>

編集:

また、このビット:

} else if (empty($LastQuestionAnswered)) {

より読みやすいものに置き換えることができます:

} else {

それはまったく同じことをするからです。while ループ内では、常に $ToDisplay を再定義していますが、これは望ましい動作だと思いますか? それ以外の場合は、次のように (while() ループの前に) 上で変数を初期化します。

$ToDisplay = '';

次のように、ループ内の割り当てを連結に変更します。

$ToDisplay = 'text assignment';

に:

$ToDisplay .= 'text concat'; // look at the dot before =
于 2013-01-15T15:44:21.040 に答える
0

お世話になりました!時間を割いてくださった皆様、本当にありがとうございました。

やっと何がいけなかったのかわかった…

PHP コードの 18 行目には、次のように記述されていました。

while ($row = mysql_fetch_array($sql)) {

もちろん、これは次のようにする必要があります。

while ($row = mysql_fetch_array($StartedQuery)) {

基本的に、間違ったクエリから行を呼び出していました。そして、私はそれのために血栓を感じます!

皆さん、ありがとうございました:)

于 2013-01-15T16:39:31.180 に答える