Hello,
I have this alternate-row-color snippet:

<?php
$SSAdv_colors1 = array("#EEEEEE","#FFFFFF");
$SSAdv_k1 = 0;
$SSAdv_m1 = 0;
$SSAdv_change_every1 = 1;
?>

...and I used it like this:

<tr bgcolor="<?php if($SSAdv_m1%$SSAdv_change_every1==0 && $SSAdv_m1>0){$SSAdv_k1++;}print $SSAdv_colors1[$SSAdv_k1%count($SSAdv_colors1)];$SSAdv_m1++;?>">

...now I need to integrate it into this if..else statement, but it will not work

<?php 
  	$i = 0;
	echo '<tr bgcolor="if($SSAdv_m1%$SSAdv_change_every1==0 && $SSAdv_m1>0){$SSAdv_k1++;}print $SSAdv_colors1[$SSAdv_k1%count($SSAdv_colors1)];$SSAdv_m1++;">'; 
	while($row_result = mysql_fetch_assoc($result)) { 
    if ($i && $i % 5 == 0) 
        echo '</tr><tr class="break">'; 
    else if ($i) 
        echo '</tr><tr>'; 
    ++$i;
    // echo the tds and their data......

Can anybody point out what I'm doing wrong?

Thanks and all the best
Tom

    You can do this much more simple.

    By doing this test:
    if($i % 2) ..... then odd row number
    else ..... then even row

    Here is my example.
    I think you can use it and change it for your script.

    <?php
    
    $arr = str_split("abcdefghijkl"); //something to print
    
    $odd  = "#DDDDDD";
    $even = "#EEEEEE";
    
    echo '<table>';
    $i = 0;
    foreach($arr as $a){
    	if($i % 2) echo '<tr bgcolor="'.$odd.'">';
    	else       echo '<tr bgcolor="'.$even.'">';
    	echo '<td>'.$a.$a.$a.'</td></tr>'; //the contents
    	$i++;
    }
    echo '</table>';	
    
    ?>

      Hello halojoy. Thank you for answer. Im not too keen on breaking up a part now working that was hard to accomplish (for me); the show only 5 records per page printed. So Im hoping for some kind of solution within what I got now :bemused:

        Could someone please move the above post (by sammyIT) to wherever it belongs? Perhaps its own thread in client side tech for example.

        You have some issues with
        1. Trying to have part of a string parsed as code
        2. If the above was done, syntax error.

        If you want a conditional to be part of another statement, such as an echo, use the ternary ?: operator (and beware of operator precedence, especially when nesting several ?: operators...

        echo 'This clearly is ' . 1 == 2 ? 'true' : 'false';
        

        But, you can easily achieve what you're looking for as long as you stick to changing colors every row.
        Also, don't use bgcolor. Use style. The bgcolor attribute exists for table, but not for tr. See http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd for the full specification.

        $i = 0;
        
        echo '<tr style="background-color: '.$color[0].';">';
        while (...) {
            if ($i && $i % 5 == 0)
                echo '</tr><tr class="break" style="background-color: '.$color[$i % 2].';">';
            else if ($i)
                echo '</tr><tr style="background-color: '.$color[$i % 2].';">';
            ++$i;
        
        // ...
        }
        

          johanafm > Thanks alot for your answer. Can you also explain how/where I define the two colors #fff and #ddd for .$color?
          Thanks for all your help - its very kind 🙂

            TommyTom wrote:

            Can you also explain how/where I define the two colors #fff and #ddd for .$color?

            You would provide them as the first two elements of an array named $color that is defined somewhere before that code. That said, you probably should use CSS classes rather than actual colour values. The same goes for the table itself.

              Laserlight > Thanks for your answer - that made it work 🙂
              I did like this:

              <?php 
              $color = array("#EEEEEE","#FFFFFF"); 
              ?>
              

                This is what I usually do:

                for($i=0;$i<$some_value;$i++)
                {
                    $CSSclass = ($i % 2 == 0)?'class="odd_row_class"':'';
                    print "<tr $CSSclass>";
                    // rest of your code here.
                }
                

                I only bother changing the colour of the alternate rows, as the tables background colour will be used for the rest!

                (Edit: OK, not sure why, but this damned parser is screwing up the percent sign I used there and dumping out &amp;#37; instead)

                  Thanks all for input and solutions - I went for the snippet from johanafm. Im so happy everything is playing nicely now 🙂
                  All the best,
                  Tommy

                    Write a Reply...