3
$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

16
  • the code seems to be correct. there's some other error. Commented Apr 4, 2012 at 16:26
  • Agreed; should absolutely work. But, try doing this instead of IF statement: printf( "<tr class=\"%s\"">, ($banana %2 ? '' : 'alt') ); Commented Apr 4, 2012 at 16:28
  • Yes I am puzzled. The css table colour row is definetly working too. Commented Apr 4, 2012 at 16:29
  • When $total_price2 == 0 there should be no <td>s inside that particular row (because of your if statement). Maybe that's what you're seeing? Commented Apr 4, 2012 at 16:30
  • 1
    @Lee That seems like the expected output if $total_price2 equals 0 for the second row. Commented Apr 4, 2012 at 16:40

3 Answers 3

3

If all else fails, you could just use CSS:

tr {background-color: blue;}
tr:nth-of-type(2n) {background-color: red;}
Sign up to request clarification or add additional context in comments.

Comments

3

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

3 Comments

thx for your help, i will try it out first in the morning. 3.15 am here x.x zzzzzzz
no luck again :( the Modulus keeps returning Odd numbers, skipping the even numbers (class="alt").
@Lee we're going to need more of your code then. Something else must be influencing $banana, also what was the html print from my code?
1

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

4 Comments

Thanks, this worked. Although how do I fix my Query? Its resulting each name twice, with the price 10 or 0. See above update.
also what is the "!" mean in if ( !$total_price2). why doesn't this if statement have {} brackets?
I don't know what your query is... please add it to your question.
The ! means a "falsey" value, like 0, and you don't need brackets for a single-line statement. Both are personal preference. You could write it if ($total_price2 == 0) { continue; }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.