简体   繁体   English

PHP - “if”中的“while”问题交替出现 html 表行颜色

[英]PHP - "if" in "while" problems to alternate html table rows colour

$banana=0;
$view = mysql_query('SELECT ......') or die ('Encountered an error.') ;
while($rows3=mysql_fetch_array($view))
{
    $total_price2=$rows3['qty']*$rows3['number'];
    $banana = $banana + 1;

    if ($total_price2!=0)
    {   
        if ($banana %2 ==0)
        {   
            echo "<tr class=\"alt\">";
        }   
        else
        {
            echo "<tr>";
        }   
        echo "<td>".$rows3['member']."</td>";
        echo "<td>".$rows3['payment']."</td>";
        echo "<td>$".number_format($total_price2,2)."</td>";                
    }

    echo "</tr>";
}   

Problems:问题:

  • The "banana" alternates the colour of the table row (class="alt") by using modulus (%) to check if banana is a odd number. “banana”通过使用模数 (%) 来检查 banana 是否为奇数来交替表行的颜色 (class="alt")。
  • Not Working, I see the browser is self-closing the opening <tr> tag.不工作,我看到浏览器正在自动关闭开始的<tr>标签。

eg:例如:

<tr><td>person</td><td>data</td><td>$10.00</td></tr>

<tr class="alt"></tr> (Repeats in this fashion) <tr class="alt"></tr> (以这种方式重复)

UPDATE更新

I have discovered that the reiterating banana always returns ODD NUMBERS: 1, 3, 5, etc我发现重复的banana总是返回奇数:1、3、5 等

MySQL is not running correctly MySQL 运行不正常

SELECT table1.member, table1.paid, table1.payment,table2.qty,table3.number FROM table1,table2,table3 WHERE table1.member = table2.member AND table1.payment="fruit"

It is giving me wrong data like so:它给我这样的错误数据:

  1. person1 $10.00人 1 $10.00
  2. person1 $0.00人 1 $0.00
  3. person2 $10.00人 2 $10.00
  4. person2 $0.00人 2 $0.00

etc ETC

Try doing this for a debug first:首先尝试进行调试:

echo "<tr class=\"alt\">&nbsp;";

My guess is that you have no data contained in your <tr> and is being squished to 0 px tall by your browser.我的猜测是您的 <tr> 中没有包含任何数据,并且正在被您的浏览器压缩到 0 px 高。

EDIT: I'm not entirely sure giving a class to your <tr> will filter down into the <td> based on certain browser DOM parsing.编辑:我不完全确定给你的 <tr> 一个 class 会根据某些浏览器 DOM 解析过滤到 <td> 中。 Whenever I do a zebra row, I'll assign the class to the <td>.每当我做斑马线时,我都会将 class 分配给 <td>。 I'm no designer by any standard, though.不过,我不是任何标准的设计师。 :) :)

Humor me and try this please:幽默一下,请试试这个:

$banana=0;
$view = mysql_query('SELECT ......') or die ('Encountered an error.') ;
while($rows3=mysql_fetch_array($view))
{
    $total_price2=$rows3['qty']*$rows3['number'];
    $banana++;
    if ($banana % 2 == 0) {   
        $td_class = "alt";
    } else {
        $td_class = "";
    }
    echo "<tr>";
    if ($total_price2!=0) {     
        echo "<td class='{$td_class}'>".$rows3['member']."</td>";
        echo "<td class='{$td_class}'>".$rows3['payment']."</td>";
        echo "<td class='{$td_class}'>$".number_format($total_price2,2)."</td>";                            
    }
    echo "</tr>";
}

If all else fails, you could just use CSS:如果一切都失败了,你可以使用 CSS:

tr {background-color: blue;}
tr:nth-of-type(2n) {background-color: red;}

That's not going to work.那是行不通的。 You're only opening the table row if $total_price2!=0 .如果$total_price2!=0 ,您只打开表格行。 It seems you should only output the closing </tr> tag inside that IF block.看来你应该只 output 结束</tr>标签内的 IF 块。

Try this instead:试试这个:

<?php
$banana=0;
$view = mysql_query('SELECT ......') or die ('Encountered an error.') ;
while($rows3=mysql_fetch_array($view))
{
    $total_price2=$rows3['qty']*$rows3['number'];

    if ( !$total_price2)
        continue;

    $banana = $banana + 1;

    if ($banana %2 ==0)
    {   
        echo "<tr class=\"alt\">";
    }   
    else
    {
        echo "<tr>";
    }

    echo "<td>".$rows3['member']."</td>";
    echo "<td>".$rows3['payment']."</td>";
    echo "<td>$".number_format($total_price2,2)."</td>";

    echo "</tr>";
} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM