【问题标题】:Different CSS style on last SQL row using PHP使用 PHP 在最后一个 SQL 行上使用不同的 CSS 样式
【发布时间】:2014-02-01 21:02:17
【问题描述】:

问题:

在不使用任何 CSS3 选择器的情况下设置 SQL 查询中最后一行的样式。

PHP 代码:

while ($item = mysql_fetch_assoc($itemsresult))
{
    $answer = "SELECT IID, Comment FROM betyg_answers WHERE EID = '{$EID}' AND CID = '{$item['CID']}' ORDER BY ID ASC";
    $answerresult = mysql_query($answer) or die ('Error (' . mysql_errno() . ') ' . mysql_error());
    $answer = mysql_fetch_assoc($answerresult);

    if ($answer['IID'] == $item['IID'])
    {
        $html .= '
            <tr>
                <td class="arrow" style="'.$arrow.'"><img src="./img/circle_arrow_right.png" class="arrowimage"></td>
                <td class="numbers" style="'.$numbers.'">'.$itemcounter.'.</td>
                <td class="text" style="'.$text.'">'.$item['Description'].'</td>
            </tr>
        ';
    }
    else
    {
        $html .= '
            <tr>
                <td class="arrow" style="'.$arrow.'">&nbsp;</td>
                <td class="numbers" style="'.$numbers.'">'.$itemcounter.'.</td>
                <td class="text" style="'.$text.'">'.$item['Description'].'</td>
            </tr>
        ';
    }

    $itemcounter++;
}

SQL 查询中的最后一行应改为打印:

$html .= '
        <tr>
            <td class="arrow" style="'.$lastarrow.'">&nbsp;</td>
            <td class="numbers" style="'.$lastnumbers.'">'.$itemcounter.'.</td>
            <td class="text" style="'.$lasttext.'">'.$item['Description'].'</td>
        </tr>
';

问题:

需要在 while 循环中添加什么以便它识别最后一行并打印不同的代码?

【问题讨论】:

  • 您可能需要考虑重写 SQL 以在一个查询中检索答案和项目。另外,如果最后一行匹配 $answer['IID'] == $item['IID'] 怎么办?

标签: php html sql css


【解决方案1】:

使用计数器:

$i = 0;
$c = mysql_num_rows($itemresult);
while ($item = mysql_fetch_assoc($itemsresult)) {
  $i++;
  if ($i == $c) {
    // This is a last row
  } else {
    // Regular row
  }
}

【讨论】:

    【解决方案2】:

    有几种方法可以做到这一点:

    1. 使用 :last-child (但这在 IE8 中不起作用)

      表 tr:last-child { 背景颜色:#ff0000; }​

    2. 使用jQuery方法(这是独立于浏览器的)

    在文档加载功能中,添加如下jQuery代码,

    $("td:last-child").css({background-color:"#ff0000"})
    

    【讨论】:

      猜你喜欢
      • 2013-08-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-06
      • 2010-09-26
      • 2012-11-18
      • 1970-01-01
      相关资源
      最近更新 更多