在PHP中嵌套if else语句

I am trying to effect a graphic change in a php page depending on the results of a mySQL database query.

<?php
$find_howto=mysql_query("SELECT * FROM `answers` WHERE `username`='$username' AND `pagename`='_I1' ORDER BY `answer` DESC") or die("Error fetching course selection, How To Use The Course");
if(mysql_num_rows($find_howto)>=1)
{   
$howto_data=mysql_fetch_assoc($find_howto);
if($howto_data['answer'] == "yes")
{
$count="red";
}elseif($howto_data['answer'] == "visited")
{
$count="yellow";
}
$find_quiz=mysql_query("SELECT * FROM `answers` WHERE `username`='$username' AND `pagename`='_I2' ORDER BY `answer` DESC") or die("Error fetching course selection, Quick Quiz");
if(mysql_num_rows($find_quiz)>=1)
{   
$quiz_data=mysql_fetch_assoc($find_quiz);
if($quiz_data['answer'] == "yes")
{
$count="red";
}elseif($quiz_data['answer'] == "visited")
{
$count="yellow";
}
}

if($count == "red")
{
echo '<img name="Introduction" border="0" src="../img/directories/main/IntroductionRed.gif" width="250" height="18"></a></td>';
}elseif($count == "yellow")
{
echo '<img name="Introduction" border="0" src="../img/directories/main/IntroductionYellow.gif" width="250" height="18"></a></td>';
}else{
echo '<img name="Introduction" border="0" src="../img/directories/main/IntroductionGreen.gif" width="250" height="18"></a></td>';
}
?>

If the result of the first query is "yes", then I have given the "count" variable the value "red".

If the result of the first query is "visited", then I have given the "count" variable the value "yellow".

The logic might be right, but the coding errors.

How can I make "if/elseif/else" work above? I've actually got 10 blocks to work with, not just two. But if this works, I can add the rest.


UPDATED SOLUTION: OK. I managed to do it a different way (with a lot of help):

<?php
$find_intro_comp=mysql_query("SELECT * FROM `answers` WHERE `username`='$username' AND (`pagename`='_I1' OR `pagename`='_I2' OR `pagename`='_I3' OR `pagename`='_I4' OR `pagename`='_I5' OR `pagename`='_I6' OR `pagename`='_I7' OR `pagename`='_I8' OR `pagename`='_I9' OR `pagename`='_I10') AND `answer`='yes'") or die("Error pulling intro completed");
if(mysql_num_rows($find_intro_comp)>=10) // the number 10 depends on how many pages there are.
{
    echo '<img name="Introduction" border="0" src="../img/directories/main/IntroductionRed.gif" width="250" height="18"></a></td>';
}else{
$find_intro_visit=mysql_query("SELECT * FROM `answers` WHERE `username`='$username' AND (`pagename`='_I1' OR `pagename`='_I2' OR `pagename`='_I3' OR `pagename`='_I4' OR `pagename`='_I5' OR `pagename`='_I6' OR `pagename`='_I7' OR `pagename`='_I8' OR `pagename`='_I9' OR `pagename`='_I10') AND `answer`='visited'") or die("Error pulling intro visited");
if(mysql_num_rows($find_intro_visit)>=1)
    {
        echo '<img name="Introduction" border="0" src="../img/directories/main/IntroductionYellow.gif" width="250" height="18"></a></td>';
    }else{
        echo '<img name="Introduction" border="0" src="../img/directories/main/IntroductionGreen.gif" width="250" height="18"></a></td>';
    }
}
?>

You have to use while loop for fetching all the results from the database and then you can check with your nested if else with fetched values

You missed } at last. Also please add proper tab sequence to your code, for that better to use any IDE.

$find_howto = mysql_query("SELECT * FROM `answers` WHERE `username`='$username' AND `pagename`='_I1' ORDER BY `answer` DESC") or die("Error fetching course selection, How To Use The Course");
if (mysql_num_rows($find_howto) >= 1) {
    $howto_data = mysql_fetch_assoc($find_howto);
    if ($howto_data['answer'] == "yes") {
        $count = "red";
    } elseif ($howto_data['answer'] == "visited") {
        $count = "yellow";
    }
    $find_quiz = mysql_query("SELECT * FROM `answers` WHERE `username`='$username' AND `pagename`='_I2' ORDER BY `answer` DESC") or die("Error fetching course selection, Quick Quiz");
    if (mysql_num_rows($find_quiz) >= 1) {
        $quiz_data = mysql_fetch_assoc($find_quiz);
        if ($quiz_data['answer'] == "yes") {
            $count = "red";
        } elseif ($quiz_data['answer'] == "visited") {
            $count = "yellow";
        }
    }

    if ($count == "red") {
        echo '<img name="Introduction" border="0" src="../img/directories/main/IntroductionRed.gif" width="250" height="18"></a></td>';
    } elseif ($count == "yellow") {
        echo '<img name="Introduction" border="0" src="../img/directories/main/IntroductionYellow.gif" width="250" height="18"></a></td>';
    } else {
        echo '<img name="Introduction" border="0" src="../img/directories/main/IntroductionGreen.gif" width="250" height="18"></a></td>';
    }
}

I recommend you remove the ANDpagename='_I2' portion from the SQL query (makes it bulkier to code, many repeated queries to the SQL server)

You can also skip out on PHP logic by specifying it in the SQL query (see: http://dev.mysql.com/doc/refman/5.0/en/case.html)

seems like numbered so orderby pagename should be ok

You should declare count abobe the block and you have missed one } braket..

  <?php

  $count;
$find_howto=mysql_query("SELECT * FROM `answers` WHERE `username`='$username' AND `pagename`='_I1' ORDER BY `answer` DESC") or die("Error fetching course selection, How To Use The Course");
if(mysql_num_rows($find_howto)>=1)
{   
$howto_data=mysql_fetch_assoc($find_howto);
if($howto_data['answer'] == "yes")
{
$count="red";
}elseif($howto_data['answer'] == "visited")
{
$count="yellow";
}
}//
$find_quiz=mysql_query("SELECT * FROM `answers` WHERE `username`='$username' AND `pagename`='_I2' ORDER BY `answer` DESC") or die("Error fetching course selection, Quick Quiz");
if(mysql_num_rows($find_quiz)>=1)
{   
$quiz_data=mysql_fetch_assoc($find_quiz);
if($quiz_data['answer'] == "yes")
{
$count="red";
}elseif($quiz_data['answer'] == "visited")
{
$count="yellow";
}
}

if($count == "red")
{
echo '<img name="Introduction" border="0" src="../img/directories/main/IntroductionRed.gif" width="250" height="18"></a></td>';
}elseif($count == "yellow")
{
echo '<img name="Introduction" border="0" src="../img/directories/main/IntroductionYellow.gif" width="250" height="18"></a></td>';
}else{
echo '<img name="Introduction" border="0" src="../img/directories/main/IntroductionGreen.gif" width="250" height="18"></a></td>';
}
?>