如果total是两个数字之间的echo语句,否则如果不同的数字集之间回显不同的语句,等等

时间:2013-09-29 22:03:04

标签: php sql if-statement

回答
我能够使用Glavić发布的示例来实现这一点。谢谢大家的提示,它们都非常有用。

原始问题
弄清楚if / else语句是多么糟糕。我已经查看了多个教程,但是无法得到我正在尝试做的工作。我需要这个: 如果总点数小于#状态,则表示没有达到标题 如果总金额介于#和#之间,那将是什么标题 如果总金额介于较高的#和#状态之间,那将是什么标题 等

添加总积分工作得很好,但我无法弄清楚如何获得要显示的总积分的正确标题。我已经包含了获取总分的代码,以防它可以与我想要的东西结合起来。任何帮助非常感谢

这很好用:

<?php
$query = "SELECT points, SUM(points) AS totalpoints FROM competition WHERE id = $id"; 
$result = mysql_query($query) or die(mysql_error());

while($row = mysql_fetch_array($result)){
    echo "<b>";
    echo $row['totalpoints'];
    echo "pts</b>";
}?>

这不起作用: (无论总分是多少,它都说名人堂成员)

<?php
$query = "SELECT points, SUM(points) AS totalpoints FROM competition WHERE id = $id"; 
$result = mysql_query($query) or die(mysql_error());

while($titled = mysql_fetch_array($result)) {

if ($titled <= 999)
 {echo ", not titled";}

elseif (($titled == 1000) && ($titled <= 1999)) 
 {echo ", Champion";}

elseif (($titled >= 2000) && ($titled <= 2999)) 
 {echo ", Grand Champion";}

elseif (($titled >= 3000) && ($titled <= 3999)) 
 {echo ", Honor Champion";}

elseif (($titled >= 4000) && ($titled <= 5999)) 
 {echo ", Regional Champion";}

elseif (($titled >= 6000) && ($titled <= 7999)) 
 {echo ", State Champion";}

elseif (($titled >= 8000) && ($titled <= 9999)) 
 {echo ", National Champion";}

elseif (($titled >= 10000) && ($titled <= 19999)) 
 {echo ", World Champion";}

elseif ($titled >= 20000) 
 {echo ", Hall of Fame";}

}?>

2 个答案:

答案 0 :(得分:1)

$titled = mysql_fetch_array($result)为您提供了一个与整数无法比较的数组。您需要使用$titled['totalpoints']

答案 1 :(得分:1)

一个更好的代码(没有所有if语句):

$titles = array(
        0 => 'not titled',
     1000 => 'Champion',
     2000 => 'Grand Champion',
     3000 => 'Honor Champion',
     4000 => 'Regional Champion',
     6000 => 'State Champion',
     8000 => 'National Champion',
    10000 => 'World Champion',
    20000 => 'Hall of Fame',
);
$titles = array_reverse($titles, true);

while ($titled = mysql_fetch_array($result)) {
    foreach ($titles as $score => $title) {
        if ($titled['totalpoints'] >= $score) break;
    }
    echo ', ' . $title;
}

Demo