SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Addict Philip Toews's Avatar
    Join Date
    Dec 2001
    Location
    Kuala Belait, Brunei
    Posts
    367
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question alternating table row colors?

    What's the easiest way to get the following script to output alternating table row colors? I am sure this is a really easy question, but I am a complete PHP and math moron...please help

    <?php
    $html = '<table>';
    $html .= '<tr bgcolor="EFEFEF"><td>number</td><td>square</td></tr>';
    for ( $i=0 ; $i<10 ; $i++) {
    $square = $i * $i;
    $html .= '<tr bgcolor="EFEFEF"><td>' . $i . '</td><td>' . $square . '</td></tr>';
    }
    $html .= '</table>';
    echo $html;

    ?>
    Philip Toews Professional esl Educator and ASP.NET wannabe

    http://www.philiptoews.com
    philip@philiptoews.com

  2. #2
    Custom User Title v1.0 FireFly's Avatar
    Join Date
    Aug 2001
    Posts
    363
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    <?php 

    $html 
    '<table>'
    $html .= '<tr bgcolor="EFEFEF"><td>number</td><td>square</td></tr>'
    for ( 
    $i=$i<10 $i++) { 
        
    $square $i $i
        if ( 
    $i%2==) {
            
    $bgcolor '#000000';
        } else {
            
    $bgcolor '#EFEFEF';
        }
        
    $html .= '<tr bgcolor="' $bgcolor '"><td>' $i '</td><td>' $square '</td></tr>'

    $html .= '</table>'
    echo 
    $html

    ?>

  3. #3
    SitePoint Guru
    Join Date
    Aug 2001
    Location
    Amsterdam
    Posts
    788
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    what does the %2 stand for in $i%2 == 0

    I haven't found anything in the manual about that one...

    Greets,

    Peanuts
    the neigbours (free) WIFI makes it just a little more fun

  4. #4
    Custom User Title v1.0 FireFly's Avatar
    Join Date
    Aug 2001
    Posts
    363
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That's one of PHP's many operators:
    http://www.us2.php.net/manual/en/language.operators.php

    This one is called Modulus and returns the remainder of the division, for example:
    PHP Code:
    <?php

    echo 5%2// = 1
    echo 6%3// = 0
    echo 3%6// = 3

    ?>

  5. #5
    SitePoint Guru
    Join Date
    Aug 2001
    Location
    Amsterdam
    Posts
    788
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ohh and on the problem...

    Would it not be much nicer to take the html out of the php sort of.. like so
    PHP Code:
    <? 
    //your php stuff in the beginning
    ?>
    <html>
    <tr bgcolor="EFEFEF"><td>number</td><td>square</td></tr>
    <?
    for ( $i=$i<10 $i++) 

        
    $square $i $i
    ?>
    <tr bgcolor="<?if ( $i%2==) {echo '#000000';}else{ echo"#EFEFEF";}?>">
    <td>
       <?=$i?>
    </td>
    <td>
       <?=$square?>
    </td>
    </tr> 
    <?
    }
    ?>
    </table>
    Last edited by peanuts; Jan 7, 2002 at 07:53.
    the neigbours (free) WIFI makes it just a little more fun

  6. #6
    SitePoint Evangelist
    Join Date
    Jul 2001
    Location
    UK
    Posts
    407
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Another good one is the bitwise AND, along with compacter code:

    PHP Code:
    <?php 

    $html 
    '<table>'
    $html .= '<tr bgcolor="EFEFEF"><td>number</td><td>square</td></tr>'
    for ( 
    $i=$i<10 $i++) { 
        
    $square $i $i
        
    $html .= '<tr bgcolor="' . (($i 1) ? "#000000" "#EFEFEF") . '"><td>' $i '</td><td>' $square '</td></tr>'

    $html .= '</table>'
    echo 
    $html

    ?>
    Just noticed peanuts' reply in the topic review. That too!

  7. #7
    SitePoint Guru
    Join Date
    Aug 2001
    Location
    Amsterdam
    Posts
    788
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by FireFly
    That's one of PHP's many operators:
    http://www.us2.php.net/manual/en/language.operators.php

    This one is called Modulus and returns the remainder of the division, for example:
    PHP Code:
    <?php

    echo 5%2// = 1
    echo 6%3// = 0
    echo 3%6// = 3

    ?>
    Cool

    I didn't know that one... I always fixed it in a bit different way

    for instance:
    PHP Code:

    $d 
    2// number in row
    for($j=0;$j<10;$j++)
    {

        if( 
    $d == $j )
        {
            echo
    "</tr><tr>"// new rows 
            
    $d $d $d;
        }
    //etcetc 
    I think both will work but yours is for 2 in a row better

    thanks..

    Peanuts
    the neigbours (free) WIFI makes it just a little more fun

  8. #8
    SitePoint Addict Philip Toews's Avatar
    Join Date
    Dec 2001
    Location
    Kuala Belait, Brunei
    Posts
    367
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Thumbs up thanks

    Thanks very much...you guys are great!

    p
    Philip Toews Professional esl Educator and ASP.NET wannabe

    http://www.philiptoews.com
    philip@philiptoews.com


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •