简体   繁体   中英

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.
  • Not Working, I see the browser is self-closing the opening <tr> tag.

eg:

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

<tr class="alt"></tr> (Repeats in this fashion)

UPDATE

I have discovered that the reiterating banana always returns ODD NUMBERS: 1, 3, 5, etc

MySQL is not running correctly

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
  2. person1 $0.00
  3. person2 $10.00
  4. person2 $0.00

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.

EDIT: I'm not entirely sure giving a class to your <tr> will filter down into the <td> based on certain browser DOM parsing. Whenever I do a zebra row, I'll assign the class to the <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:

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 . It seems you should only output the closing </tr> tag inside that IF block.

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>";
} 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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