Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    New Coder
    Join Date
    Jul 2004
    Location
    Leamington, UK
    Posts
    29
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Alternating table row background colours

    Hi guys.

    ok here is my dilema
    PHP Code:
    <table width="419" height="31" border="1" align="center" cellpadding="0" cellspacing="0" bordercolor="#FFFFFF">
              <tr bordercolor="#E1E8F2" bgcolor="#E1E8F2" class="smalltxt">
                <td width="82" height="13"><strong>Date</strong></td>
                <td width="52"><strong>Status</strong></td>
                <td width="114"><strong>Sender</strong></td>
                <td width="148"><strong>Title</strong></td>
              </tr>
              <?php do { ?>
              <tr class="mediumtxt">
                <td height="18"><?php echo $row_listmessages['date']; ?></td>
                <td><?php if($row_listmessages['status'] == 'Unread'){ 
                echo 
    "<img src=\images/havemsg.gif alt=Unread>";
                } else {
                echo 
    "<img src=\images/nomsg.gif alt=Read>";} ?></td>
                <td><?php echo $row_listmessages['sender']; ?></td>
                <td><?php echo $row_listmessages['title']; ?></td>
              </tr>
              <?php } while ($row_listmessages mysql_fetch_assoc($listmessages)); ?>
            </table>
    as you can see i have a table that produces repeated rows containing data

    what i want to do is have the background colour of 1 row say #FF6600
    and the following row #FFFFCC for example
    and have that repeated down the page

    hope i explained that ok ?

    hanks in advance guys

  • #2
    Regular Coder
    Join Date
    Jun 2004
    Posts
    565
    Thanks
    0
    Thanked 18 Times in 18 Posts
    PHP Code:
    <table width="419" height="31" border="1" align="center" cellpadding="0" cellspacing="0" bordercolor="#FFFFFF">
              <tr bordercolor="#E1E8F2" bgcolor="#E1E8F2" class="smalltxt">
                <td width="82" height="13"><strong>Date</strong></td>
                <td width="52"><strong>Status</strong></td>
                <td width="114"><strong>Sender</strong></td>
                <td width="148"><strong>Title</strong></td>
              </tr>
              <?php 
              $i 
    0;
              do { 
    ?>
              <tr class="mediumtxt" style="background-color:<?php echo ($i == 0) ? '#FFFFCC' '#FF6600'?>">
                <td height="18"><?php echo $row_listmessages['date']; ?></td>
                <td><?php if($row_listmessages['status'] == 'Unread'){ 
                echo 
    "<img src=\images/havemsg.gif alt=Unread>";
                } else {
                echo 
    "<img src=\images/nomsg.gif alt=Read>";} ?></td>
                <td><?php echo $row_listmessages['sender']; ?></td>
                <td><?php echo $row_listmessages['title']; ?></td>
              </tr>
              <?php
              $i
    ++;
               } while (
    $row_listmessages mysql_fetch_assoc($listmessages)); ?>
            </table>
    dumpfi

  • #3
    New Coder
    Join Date
    Jul 2004
    Location
    Leamington, UK
    Posts
    29
    Thanks
    0
    Thanked 0 Times in 0 Posts
    works like a charm

    thanks dumfi

  • #4
    Regular Coder
    Join Date
    Oct 2003
    Posts
    603
    Thanks
    2
    Thanked 1 Time in 1 Post
    i just do it like this

    PHP Code:
    while($row mysql_fetch_array($query)){
    if(empty(
    $color)){
    $color "#FF6600";
    }elseif(
    $color == "#FF6600"){
    $color "#FFFFCC";
    }elseif(
    $color == "#FFFFCC"){
    $color "#FF6600";
    }

    //...preceding table code
    echo "<tr bgcolor=$color>"
    //...proceding table code

    is that a bad way to do it?
    Last edited by boeing747fp; 09-27-2004 at 08:56 PM.

  • #5
    Regular Coder trib4lmaniac's Avatar
    Join Date
    Feb 2004
    Location
    Cornwall, UK
    Posts
    535
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by boeing747fp
    is that a bad way to do it?
    I'd say it was a less effecient way to do it. You're having to enter each color twice. The modulus operator is generally the way to go about things like this...
    PHP Code:
    if($i 2) {
      echo 
    '#FFFFFF';
    } else {
      echo 
    '#E9E9E9';

    ...is (pretty much) the key part of dumpfi's script. It can be extended easily too...
    PHP Code:
    if($i == 1) {
      echo 
    '#FFFFFF';
    } else if(
    $i == 2) {
      echo 
    '#E9E9E9';
    } else {
      echo 
    '#C9C9C9';

    Last edited by trib4lmaniac; 09-27-2004 at 09:05 PM.

  • #6
    Regular Coder
    Join Date
    Oct 2003
    Posts
    603
    Thanks
    2
    Thanked 1 Time in 1 Post
    o
    ok... thanks

  • #7
    Regular Coder
    Join Date
    Aug 2002
    Location
    USA
    Posts
    625
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I just use a simple counter:



    $i = 0;

    while ($row = mysql_fetch_assoc($makes3)) {

    if($i == 1){
    echo "<tr bgcolor=\"#FFFFFF\">" . "\n";
    $i = 0;
    }else{
    echo "<tr bgcolor=\"#DFDFDF\">" . "\n";
    $i++;
    }
    echo "The rest of while markup here";
    }


    -james
    Last edited by jamescover; 09-28-2004 at 12:21 AM.
    "God so loved the world that he gave his only begotten son, so that whosoever believed in him would not perish, but have everlasting life. For God did not send his son into the world to condemn the world, but so that through him the world might be saved. "

  • #8
    Regular Coder
    Join Date
    May 2002
    Location
    Virginia, USA
    Posts
    621
    Thanks
    0
    Thanked 6 Times in 6 Posts
    I'd switch to a css solution.

    class="even_row" & class="odd_row"

  • #9
    Regular Coder
    Join Date
    Aug 2004
    Location
    The US of A
    Posts
    767
    Thanks
    1
    Thanked 0 Times in 0 Posts
    I know I'm gonna get smacked for this, but I use a tenary with two CSS classes in them for mine.

    *example from my tagboard*
    PHP Code:
    <?php
        $x 
    0;
        while(
    $row pg_fetch_array($sql,NULL,PGSQL_ASSOC))
            {
            
    $class = ($x 2) ? ("content1") : ("content2"); 
            
    $row["comment"]  = eregi_replace($patterns$replacements$row["comment"]);
            
    $row["name"]  = eregi_replace($patterns$replacements$row["name"]);
            print 
    "<tr><td class=\"$class\"><strong>"$row["name"] ."</strong>\n"$row["comment"] ."\n";
            if (
    $row["www"]) { print " <strong><a class=\"archive\" href=\""$row["www"] ."\">www</a></strong>";} 
            print 
    "</td></tr>\n"
            
    $x++;
            }
        empty(
    $x);
    ?>
    I've gotten into the habit of emptying my variables I use for counting. That's why there is a an empty() floating in there.
    Last edited by Kurashu; 09-28-2004 at 12:35 AM.

  • #10
    Regular Coder trib4lmaniac's Avatar
    Join Date
    Feb 2004
    Location
    Cornwall, UK
    Posts
    535
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Celtboy
    I'd switch to a css solution.

    class="even_row" & class="odd_row"
    How does that help? I can't seea completely css way of doing this.


  •  

    Posting Permissions

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