4

My question is very simple. The code I have written here produces absolutely no output on the webpage. I've been at it all day and I'm sure that it's something very simple that I am being an idiot for missing. So I am appealing to your good-natured fresh eyes! If anyone can spot a reason why this isn't working, I'd be very grateful.

The premise:

This is a decision tree online survey that has the following conditions: if a user has already started the survey, it will find them in the database, find their last answered question and display the next one. But if they haven't started, it will display the first question.

All survey questions are held in the database as well as the decision tree logic (for instance, if the user chooses option 2 for question 1, they will be directed to question 3, not 2).

Please assume that for the moment, I am updating relevant info directly from the database and not automating it on the website.

Thanks :)

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>
Zulu Irminger
  • 442
  • 2
  • 8
  • 16
  • 1
    No output means enable error reporting and display. You likely have a fatal error. `error_reporting(E_ALL); ini_set('display_errors', 1);` – Michael Berkowski Jan 15 '13 at 15:30
  • I have exactly that at the top of my script, but there are no errors on the website! – Zulu Irminger Jan 15 '13 at 15:32
  • Check your logs, could be that PHP is segfaulting (not unheard of), which should give you a clue as to what's causing your problems. – BenLanc Jan 15 '13 at 15:33
  • I think it's a case sensitive problem. Try `lastquestionanswered` instead of `LastQuestionAnswered` in your keys. – emco Jan 15 '13 at 15:34
  • Do you mean in the variable name, EmCo? As in $lastquestionanswered ? EDIT - Just tried this and made no difference... thanks for the suggestion though :) – Zulu Irminger Jan 15 '13 at 15:38
  • No, I mean in the key. Instead of `$row["LastQuestionAnswered"];` try `$row["lastquestionanswered"];`. As I recall, the names of the field of the table are always retrieved in lower case and since the name of variables are case sensitive in PHP, that could be your problem. – emco Jan 15 '13 at 15:43
  • Are you sure you are making a successful connection to your database? – George Jan 15 '13 at 15:53
  • 1
    have you checked the error log? – eis Jan 15 '13 at 15:54
  • A successful connection is being made, F4r-20, yes. It was showing 2 of 3 bits of information I needed (when I was adding answers aswell), but since I started playing around with it, it just displays nothing. And yes, eis, I have. Nothing out of the ordinary! – Zulu Irminger Jan 15 '13 at 16:00
  • Can u echo the queries and copy and run it in mysql – Arun Unnikrishnan Jan 15 '13 at 16:07
  • Yep, Arunu, all the MYSQL queries are legitimate and they work with no problem. When all mashed together, though, as I've done, nothing appears. – Zulu Irminger Jan 15 '13 at 16:08
  • Reading your code, I noticed you're using the variable `$row` everywhere. I think you're over writing the value of this variable, giving you a logical error. Try using other variable names. – emco Jan 15 '13 at 16:36
  • Thanks EmCo - That's not it, but I've realised what I was doing wrong and posted the answer below. Really appreciate your input :) – Zulu Irminger Jan 15 '13 at 16:40
  • 1
    Son, I think you [may](http://www.makinggoodsoftware.com/2009/06/22/how-to-write-readable-code/) [need](http://www.codinghorror.com/blog/2006/06/pretty-code-ugly-code.html) [some](http://www.codinghorror.com/blog/2006/05/code-smells.html) [help](http://stackoverflow.com/questions/550861/improving-code-readability). Your code logic (and thus your errors) may be easier to spot if you'd clean up the code, get rid of those nested loops, and learn to use [PDO](http://php.net/manual/en/book.pdo.php). – PenguinCoder Jan 15 '13 at 16:45
  • Yep, PenguinCoder, I completely agree - this is a very early draft, and it will all be cleared up by the time it's deployed! PDO is on my list of things to learn. Thanks for the link :) – Zulu Irminger Jan 15 '13 at 16:49
  • 1
    I would recommend to split this code into functions. One function to return LastQuestionAnswered .Another one to return NextQuestion based on parameters "user's previous answer" and "LastQuestionAnswered "... – Arun Unnikrishnan Jan 15 '13 at 17:02

2 Answers2

0

This bit:

if ($StartedQueryCount > 0) {

probably evaluates to false, and there's no matching else tag that adds content. Try changing:

    }
?>

with:

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

Edit:

Also, this bit:

} else if (empty($LastQuestionAnswered)) {

Could be replaced with the more readable:

} else {

Since it does exactly the same thing. And within your while loop, you are constantly redefining $ToDisplay, I assume this is wanted behaviour? Otherwise initialize the variable on top (before the while() loop) like so:

$ToDisplay = '';

And change the assignments within the loop to concatenations, like so:

$ToDisplay = 'text assignment';

To:

$ToDisplay .= 'text concat'; // look at the dot before =
Johannes Konst
  • 428
  • 2
  • 8
  • Thanks for the suggestion, Johannes, but that makes no difference. Appreciate your input, though :) – Zulu Irminger Jan 15 '13 at 15:46
  • These appear to be the only logic-errors you've got. If this doesn't resolve things, then I suggest you follow Michael Berkowski's tip and add error_reporting(E_ALL); and ini_set('display_errors', 1); – Johannes Konst Jan 15 '13 at 15:52
  • Thanks again Johannes. I've added your suggestions, but to no happier ending unfortunately. I have error reporting turned on, but it's just not showing anything. In regards to redefining $ToDisplay, I need it to show the first question if the person has not started the survey, or the next question if they have previously started. Hence why the first $ToDisplay has $SurveyStartedQuestionText included, and the second has $SurveyNotStartedQuestionText (note the added "Not". My head's muddled! – Zulu Irminger Jan 15 '13 at 15:58
0

Thank you for all your help! I really appreciate you all taking the time.

I finally realised what was wrong...

On Line 18 of my PHP code, I had the following:

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

whereas it should of course have been this:

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

Essentially I was calling the rows from the wrong query. And I feel a clot because of it!

Thanks again, everyone :)

Zulu Irminger
  • 442
  • 2
  • 8
  • 16