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 6 of 6
  1. #1
    Regular Coder
    Join Date
    Sep 2009
    Location
    Calgary, Alberta
    Posts
    239
    Thanks
    47
    Thanked 3 Times in 3 Posts

    change Background color.

    How can you set it up to change the background color every second record.?


    Thanks in advance.


    Slayer.

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Use modulus or bit operands for the zebra striping. I'll use bit operands which is only useful for every other row, as opposed to modulus which can use multipliers like 3 or 4 row changes. I'll also use a table since I'm a bit on the lazy side:
    PHP Code:
    $a range(110);
    $i 0;
    print 
    '<table>';
    while (++
    $i && $current current($a))
    {
        if (
    $i 1)
        {
            
    $colour '#fff';
        }
        else
        {
            
    $colour '#eee';
        }
        
    printf('<tr style="background-color: %s"><td>%d</td></tr>'$colour$current);
        
    next($a);
    }
    print 
    '</table>'
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • Users who have thanked Fou-Lu for this post:

    SlayerACC (02-11-2013)

  • #3
    Regular Coder
    Join Date
    Sep 2009
    Location
    Calgary, Alberta
    Posts
    239
    Thanks
    47
    Thanked 3 Times in 3 Posts
    Hey Fou Lou,

    I was trying to figure our what you had posted... to no eval...

    Thanks...


    I am trying to incorperate it in with retrieving records from the database.
    PHP Code:
    $query "SELECT application_id, firstName, middleName, lastName, phone, email, date_format(appdate, '%m/%d/%Y') as appdate FROM applications";
    $results mysqli_query($mysqli,$query)or die(mysqli_error($mysqli));

    while(
    $row mysqli_fetch_array$results )) {
    $fname=$row['firstName'];
    $mname=$row['middleName'];
    $lname=$row['lastName'];
    $appdate=$row['appdate'];
    $phone=$row['phone'];
    $email=$row['email'];


    echo 
    "<tr><td width=\"255\" class=\"lborder\">";
    echo 
    $fname;
    echo 
    " ";
    echo 
    $mname;
    echo 
    " ";
    echo 
    $lname;
    echo 
    "</td><td width=\"100\">";
    echo 
    $appdate;
    echo 
    "</td><td width=\"125\">";
    echo 
    $phone;
    echo 
    "</td><td width=\"200\">";
    echo 
    $email;
    echo 
    "</td><td width=\"80\" class=\"rborder\">";
    echo 
    "E | D | P";
    echo 
    "</td></tr>";

    Thanks again Fou Lou.


    Slayer

  • #4
    Senior Coder
    Join Date
    Nov 2010
    Posts
    1,380
    Thanks
    263
    Thanked 32 Times in 31 Posts
    Basically what i did with mine (and thanks Fou-Lu for the official tech term zebra striping )

    The easiest way i found is to set your first color before the record loops start.

    Then as you finish the output of each row, check the color, if the color is color 1 then change it to color 2, if it is color 2 change it to color 1. That way each row is a different color.

    rowa and rowb are set as class in css or you can set them in the file your in using style

    Code:
    .rowa {
    background-color:#FFFFFF;  /* white */
    }
    
    .rowb{
    background-color:#000000; /* black */
    }
    set your rowb color and rowa colors before the loop starts, then at the beginning of the loop before the output of the first row, set $switchrow to either rowa or rowb,

    then like

    Code:
    <tr class="<?=$switchrow;?>" >
    then at the end of the loop before the next record try something like this.

    PHP Code:
    //switch the row color if need to
    if($switchrow == "rowb")
      {
      
    $switchrow "rowa";
       }else{
              
    $switchrow "rowb";
               }
    //close else 
    Last edited by durangod; 02-11-2013 at 06:11 PM. Reason: sorry forgot something, corrected

  • Users who have thanked durangod for this post:

    SlayerACC (02-11-2013)

  • #5
    Regular Coder
    Join Date
    Sep 2009
    Location
    Calgary, Alberta
    Posts
    239
    Thanks
    47
    Thanked 3 Times in 3 Posts
    Thanks Durangod..

    That works like a charm...

    I found that I was trying the same thing but could not get it to work..

    This was the part I was doing wrong.
    then at the end of the loop before the next record try something like this.
    Thanks...

    Slayer

  • #6
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Yep, that'll work too.
    Personally, I don't like working with strings when I can avoid them. So I prefer dealing with the integers or booleans to flip them instead of using strings.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 


  •  

    Posting Permissions

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