表格最后第二行的背景颜色

时间:2017-11-17 11:16:36

标签: php css html-table row

我希望红色颜色作为表格最后一行的背景。

但它没有改变。

我试过了:

    $i = 0;
    $total = 0;
    $stmttr = $user_home->runQuery("SELECT * FROM invoice WHERE BRN = :inv"); // Your query
    $stmttr->bindParam(':inv',$irn);
    $allRows = $stmttr->rowCount(); // Count Total rows available in result
    $tri = 0; // initiate variable to check <tr> status
        while($rowc)
        {
            extract($rowc);
            $i++;

?>

    <tr  <?php if(++$tri==$allRows) { ?> style="background-color: red;"<?php } ?>   >
      <td style="border-top:none !important; border-bottom:none !important;">

请参阅完整代码:

<?php
    $i = 0;
    $total = 0;
    $stmttr = $user_home->runQuery("SELECT * FROM invoice WHERE BRN = :inv"); // Your query
    $stmttr->bindParam(':inv',$irn);
    $allRows = $stmttr->rowCount(); // Count Total rows available in result
    $tri = 0; // initiate variable to check <tr> status
        while($rowc)
        {
            extract($rowc);
            $i++;

?>

    <tr  <?php if(++$tri==$allRows) { ?> style="background-color: red;"<?php } ?>   >
      <td style="border-top:none !important; border-bottom:none !important;">
        <?php echo $i; ?>
      </td>
      <td style="border-top:none !important; border-bottom:none !important;">
        <?php
        $itm = $IRN;
        $stmti = $user_home->runQuery('SELECT * FROM item WHERE IRN = :iinv ');
        $stmti->bindParam(':iinv',$itm);
        $stmti->execute();
        $rowci = $stmti->fetch(PDO::FETCH_ASSOC); 
        echo $rowci['Name']; 
        $srv = $SRN;
        $stmts = $user_home->runQuery('SELECT * FROM service WHERE SRN = :sinv ');
        $stmts->bindParam(':sinv',$srv);
        $stmts->execute();
        $rowcs = $stmts->fetch(PDO::FETCH_ASSOC); 
        ?>
        &nbsp;(<?php echo $rowcs['Name']; ?>)
      </td>
      <td style="border-top:none !important; border-bottom:none !important;">
        <?php echo $Quantity; ?>
      </td>
      <td style="border-top:none !important; border-bottom:none !important;">
        <?php echo $Amnt; ?>
      </td>
    </tr> 

2 个答案:

答案 0 :(得分:1)

错误发生在:$allRows = $stmttr->rowCount(); // Count Total rows available in result

不计算MySQL中可用的行

更新的代码:

$i = 0;
    $total = 0;
    $stmttr = $user_home->runQuery("SELECT COUNT(*) As rows FROM invoice WHERE BRN = :inv"); // Your query
    $stmttr->bindParam(':inv',$irn);
    $stmttr->execute();
$cresult = $stmttr->fetch(PDO::FETCH_ASSOC);
$tf= ($cresult['rows']);
        while($rowc)
        {
            extract($rowc);
            $i++;

?>

    <tr  <?php if($i == $tf) { ?> style="background-color: red;"<?php } ?>   >
      <td style="border-top:none !important; border-bottom:none !important;">

希望它对你有所帮助。祝你好运!!

答案 1 :(得分:0)

  • 也许你的循环有问题。什么是变量$ rowc?
  • 也许您应该在之后的第一个<table><tr>之前添加</table> 最后</tr>

CSS应该在css文件中,更好的练习!

我没有你的数据库,但是这个代码正常,如果它是你想要的。

<?php
$tri = 0;
$nbrows=10;

 ?>
<table>
<?php
while ($tri<$nbrows) {  ?>
    <tr <?php if(++$tri==$nbrows) { echo "style=\"background-color: red;\"";} ?> >
     <td style="border-top:none !important; border-bottom:none !important;">Your content </td>
      <td style="border-top:none !important; border-bottom:none !important;">Content 2 </td>
    </tr>
<?php
}
 ?>
</table>