1

回答
我能够使用 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";}

}?>
4

2 回答 2

1

$titled = mysql_fetch_array($result)给你一个与整数不可比较的数组。您将需要使用$titled['totalpoints'].

于 2013-09-29T22:05:01.533 回答
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;
}

演示

于 2013-09-29T22:13:45.603 回答