我试图在php页面中进行图形更改,具体取决于mySQL数据库查询的结果。
<?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>';
}
?>
如果第一个查询的结果是“是”,那么我给“count”变量赋值“red”。
如果第一个查询的结果是“访问过”,那么我给“count”变量赋值“yellow”。
逻辑可能是正确的,但编码错误。
如何让“if / elseif / else”在上面工作?我实际上有10块可以使用,而不仅仅是两块。但如果这样可行,我可以添加其余部分。
更新的解决方案:好的。我设法以不同的方式做到了(有很多帮助):
<?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>';
}
}
?>
答案 0 :(得分:1)
你最后错过了}
。另外,请在代码中添加适当的选项卡序列,以便更好地使用任何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>';
}
}
答案 1 :(得分:0)
您必须使用while循环从数据库中获取所有结果,然后您可以使用带有已获取值的嵌套if else检查
答案 2 :(得分:0)
我建议您从SQL查询中删除AND
pagename ='_I2'
部分(使代码更加笨重,对SQL服务器进行多次重复查询)
您还可以通过在SQL查询中指定PHP逻辑来跳过它 (见:http://dev.mysql.com/doc/refman/5.0/en/case.html)
看起来像编号所以orderby pagename应该没问题
答案 3 :(得分:0)
你应该在街区上方宣布计数而你错过了一个}支架..
<?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>';
}
?>