SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Enthusiast burgesszx's Avatar
    Join Date
    Jan 2003
    Location
    UK
    Posts
    41
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question How do I..make the <tr> table rows alternate colors?

    If you look at the SitePoint forum you will see that each row has alternate colors. This is a trick that I want to implement on a site that I am building at the moment, but I'm unsure of the solution. Perhaps something like this will work:
    <?php
    while ($bev = mysql_fetch_array($bevs)) {//get product list
    echo("<tr><td bgcolor="RED">$somevariablearray</td></tr>\n");

    If (count is not even){bgcolor="RED">$somevariablearray</td></tr>\n");
    }
    $count = $count +1;
    ?>

    Somethinglike this may work - have I just answered the question or does anybody have better ideas?

    Also have you spotted the bug in my code 'If (count is not even)' this may cause a run time error me thinks.

    So: ? do I user here then? I will find out I will...

    Yours gratefully,

    Simon.

  2. #2
    SitePoint Enthusiast
    Join Date
    May 2002
    Posts
    61
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You have it generally correct..here is some code that should work:
    PHP Code:
    <?php
    $count 
    0;
    $colors[0] = 'red';
    $colors[1] = 'blue';
    while (
    $bev mysql_fetch_array($bevs)) {//get product list
      
    $rowcolor $colors[$count 2];
      echo(
    "<tr><td bgcolor=\"$rowcolor\">$somevariablearray</td></tr>\n");
      
    $count++;
    }
    ?>
    Matt Wade
    http://codewalkers.com/ - Home of the PHP Coding Contest
    http://phphosts.codewalkers.com/ - Find a PHP host to meet all your needs. Add a host for FREE.

  3. #3
    Mlle. Ledoyen silver trophy seanf's Avatar
    Join Date
    Jan 2001
    Location
    UK
    Posts
    7,168
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Take a look at this tutorial

    Sean
    Harry Potter

    -- You lived inside my world so softly
    -- Protected only by the kindness of your nature

  4. #4
    SitePoint Enthusiast burgesszx's Avatar
    Join Date
    Jan 2003
    Location
    UK
    Posts
    41
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Table row colours continued...

    Great tutorial that.
    I am getting this error message:

    Parse error: parse error, unexpected T_INC in c:\program files\apache group\apache\htdocs\rswines\wine\winelistx.php on line 74

    I have got no idea why. Here is the code itself:

    <table border="0">
    <tr><th>Beverage Name</th><th>Vintage</th><th>Quantity</th><th>Price</th></tr>

    <?php
    $bevs = mysql_query($select . $from . $where);
    if (!$bevs) {
    echo("</table>");
    echo("<p>Error retrieving beverages from database!<br />".
    "Error: " . mysql_error() . "</p>");
    exit();
    }


    $num = 0;

    while ($bev = mysql_fetch_array ($bevs) ) {


    $colour = ( $num % 2 == 0 ) ? "#000000" : "#808080";



    echo ' <tr bgcolor="'.$colour.'">


    <td>&nbsp;</td>
    </tr>'."\n";

    num++;
    }
    ?>

    </table>

  5. #5
    Mlle. Ledoyen silver trophy seanf's Avatar
    Join Date
    Jan 2001
    Location
    UK
    Posts
    7,168
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Which is line 74?

    Sean
    Harry Potter

    -- You lived inside my world so softly
    -- Protected only by the kindness of your nature

  6. #6
    SitePoint Enthusiast burgesszx's Avatar
    Join Date
    Jan 2003
    Location
    UK
    Posts
    41
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Quick reply...

    Here is the errant code:

    <table border="0">
    <tr><th>Beverage Name</th><th>Vintage</th><th>Quantity</th><th>Price</th></tr>

    <?php
    $bevs = mysql_query($select . $from . $where);
    if (!$bevs) {
    echo("</table>");
    echo("<p>Error retrieving beverages from database!<br />".
    "Error: " . mysql_error() . "</p>");
    exit();
    }


    $num = 0;


    $id = $bev["ID"];

    $bdate = $bev["Bdate"];
    $bprice = $bev["Bprice"];
    $bquantity = $bev["Bquantity"];

    while ( $row = @ mysql_fetch_array ( $bevs ) ) {

    $colour = ( $num % 2 == 0 ) ? "#000000" : "#808080";

    echo ' <tr bgcolor="'.$colour.'">

    echo("<td><a href='wine.php?id=$id'>$bname</a></td>\n");
    echo("<td>$bdate</td>\n");
    echo("<td>$bquantity</td>\n");
    echo("<td nowrap>".number_format($bprice,2)."</td>\n");


    </tr>'."\n";

    $num++;

    }



    ?>

    </table>

    I am quickly trying to put the query row results into the table <td>'s.

    Parse error: parse error, unexpected T_STRING, expecting ',' or ';' in c:\program files\apache group\apache\htdocs\rswines\wine\winelistx.php on line 74


    Is the latest error message. This is this line:
    echo("<td><a href='wine.php?id=$id'>$bname</a></td>\n");

    the % operator is supposed to give the remainder from a calculation, in this case it is % 2 == 0 I do not see how this will ever evaluate to two different things - it is always going to be false (ie never 0 apart from the first run through the loop).

    I have tried the function in a test page, yes it works fine, but when I put data in the <td> then I get this error message. Its great to have some help though. I have recently come over to PHP from ASP, and I did not know much about that either! PHP is the best route for freelancers such as myself though - everything is affordable from hosting costs to licensing etc etc...
    Darned if I can get this to work in a hurry though.

  7. #7
    Mlle. Ledoyen silver trophy seanf's Avatar
    Join Date
    Jan 2001
    Location
    UK
    Posts
    7,168
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    echo ' <tr bgcolor="'.$colour.'"> 
    needs to be:

    PHP Code:
    echo '<tr bgcolor="'.$colour.'">'
    Sean
    Harry Potter

    -- You lived inside my world so softly
    -- Protected only by the kindness of your nature

  8. #8
    SitePoint Enthusiast burgesszx's Avatar
    Join Date
    Jan 2003
    Location
    UK
    Posts
    41
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Houston we are looking good.

    My customer will be pleased!

    I understand that operator thing now. Simple really, much like my first idea to divide by 2 eh ;-)
    Those are gross colours though sean, olive drab and black like a warplane.


    All the best,

    Simon.

  9. #9
    Mlle. Ledoyen silver trophy seanf's Avatar
    Join Date
    Jan 2001
    Location
    UK
    Posts
    7,168
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It's dark grey not olive

    Sean
    Harry Potter

    -- You lived inside my world so softly
    -- Protected only by the kindness of your nature

  10. #10
    SitePoint Member
    Join Date
    Dec 2002
    Posts
    16
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    alternating rows: my 2cents

    I use CSS for to set the colors of the rows. Here's how I do it:

    PHP Code:
    <html>
      <head>
        <style>
        .evenrow {background-color: #ffffff;}
        .oddrow  {background-color: #cccccc;}
        </style>
      </head>
      <body>
      <table border="0">
      <?php
        $data
    [] = Array('column1','column2','column3');
        
    $data[] = Array('column1','column2','column3');
        
    $data[] = Array('column1','column2','column3');
        
    $data[] = Array('column1','column2','column3');
        
    $rowtype = Array('evenrow','oddrow');
        for (
    $i=0,$rowselect=0;$i<sizeof($data);$i++)
        {
          echo(
    '<tr bgcolor="'.$rowtype[$rowselect].'">');
          echo(
    '<td>'.implode('</td><td>',$data[$i]).'</td>');
          echo(
    '</tr>');
          
    $rowselect $rowselect;
         }
      
    ?>
      </table>
      </body>
    </html>

    Though it's done by one of my objects, that's basically how I do it.


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
  •