douyun1950 2013-12-04 07:22
浏览 65

在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>';
    }
}
?>
  • 写回答

4条回答 默认 最新

  • dte8665 2013-12-04 07:27
    关注

    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

    评论

报告相同问题?

悬赏问题

  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line