PHP根据不同的列更改行输出

时间:2013-02-23 10:17:28

标签: php row

我通过PHP / MySQL输出了一些数据。它只是一个与某个字符串对应的中文字符列表。

$i = 0;

while ($i <= $x) {
    echo "<div class=\"char-option\"><a href=\"character.php?char=" . $results[$i]['id']. "\">" . $results[$i]['mainchar'] . "</a></div>";
    $i++;
}

我有另一行,其中有一个角色排名,大约20%的角色有排名。我想:

  1. 为排名最高的角色将班级更改为“char-option char-common”。
  2. 如果所有角色都没有排名,请不要更改班级。
  3. 提前致谢。

2 个答案:

答案 0 :(得分:1)

步骤1:确定,哪个角色排名最高。

$high = -1, $hrank = 0;
for($i = 0; $i <= $x; ++$i) {
    if(isset($results[$i]['rank']) && $hrank < $results[$i]['rank']) {
        $high = $i;
        $hrank = $results[$i]['rank'];
    }
}

步骤2:使用具有最高角色排名的信息,我们可以确定谁获得正常班级以及谁获得额外班级。

for($i = 0; $i <= $x; ++$i) {
    $class = "char-option";   //Default class
    if($i == $high) $class .= " char-common"; //Adds the second class for the highest ranked character.
    echo "<div class=\"$class\"><a href=\"character.php?char=" . $results[$i]['id']. "\">" . $results[$i]['mainchar'] . "</a></div>";
}

$high中的-1确保该类不会被更改,除非列表中某处有排名。

答案 1 :(得分:0)

在结果中设置排名并以此方式执行。

$i = 0;
while ($i <= $x) {
  if ($results[$i]['highest_ranking'] == 1) {
    $class = "char-option char-common";
  } else {
    $class = "char-option";
  }
  echo "<div class=\"$class\"><a href=\"character.php?char=" . $results[$i]['id'] . "\">" . $results[$i]['mainchar'] . "</a></div>";
  $i++;
}