Alternate Colors on MySQL result rows

05/10/08

Alternate Colors on MySQL result rows

Permalink 09:56:38 pm, Categories: PHP, MySQL  

How to Have Alternating Colors in Table Row Results

1. Pick two colors for table cell backgrounds. For example, light cream and light green.

2. When you dip into the MySQL table, you will be using a while loop. You will also be using two other elements.

3. The first is the use of $i=0, $i++ while using the “while loop”

4. The second is the use of the modulo % to get the remainder.

5. As you use the “while” loop, you increment the $i by 1 each time, and you will be dividing $i by 2, $i % 2.

6. You also place the condition, if the remainder is 0, then use the light cream, and if the remainder is 1, then use the light green.

Note, if you want three colors, just do the same thing, use $i % 3, and if the remainder is 0, 1 or 2, then use color1, color2 or color3. Obviously, if your site is gay, and you want the gay colors to appear, use $i % 6, assigning 6 colors, but I think that would look tacky. I think red and white with a little blue and some stars appearing in the background would look like an American flag.

Val Zubiri

Programming by Val Zubiri
Art by Val Zubiri
Arts Global Impact.org / Original Art and Art Books for Collectors for sale
Chicago Art Movement.com
Get a Massage Contact and Shop using your mobile phone

Comments, Pingbacks:

Comment from: Jan [Visitor] Email
Hi Val, thanks for that info. However I couldn't get it working with the three colors example.
Could you write some example ?

I had this :

php $i = 0;
php $i++;

and then :

php if($i&1) { echo 'class="green"';} else {echo 'class="yellow"';}


I didn't follow where to put the "%" sign, or a third statement (I tried an elseif, but didn't work).
PermalinkPermalink 02/05/09 @ 13:26
Comment from: valzubiri [Member] Email
Here are 2 examples. There are other ways to code, but below is a more simplified way to use the condition on mysql_fetch_object and counting the rows
// $link is your connection to the database
$query = "select * from table where truckcolor='red'";
$result = mysql_query($query, $link);
$numrows = mysql_num_rows($result);
$i=0;
if ($numrows==0)
   {
      //print nothing... you get the numrows so you
      //can either make a table for the results or not
      print("");
   }
else
   {
      print ("<table>");
      while ($row = mysql_fetch_object($result))
      {
         if ($i%3==0)
            {
               print ("row and cell code with the $first color ");
            }
         if ($i%3==1)
            {
               print ("row and cell code with the $second color ");
            }
         if ($i%3==2)
            {
               print ("row and cell code with the $third color ");
            }
         $i++
      }
      print ("</table>");
   }

Here's another way to use this, given an array

$count = count($thearray);
for ($i=0; $i<$count; $i++)
   {
      print ("<table>");
      if ($i%3==0)
         {
            print ("row and cell code with the $first color");
         }
      if ($i%3==1)
         {
            print ("row and cell code with the $second color");
         }
      if ($i%3==2)
         {
            print ("row and cell code with the $third color");
         }
      print ("</table>");
   }

If you get the hang of it, you can make more complicated returns.
For example, if you are returning pictures, and you want 5 pictures per row.
Start with $i=5 and increment from there... then divide $i by 5... so if you return $i % 5 == 0, print a tr followed by /tr. Otherwise print cells td /td only... Look at the html of a table and see where codes repeat, and then figure out what I'm talking about :-)
PermalinkPermalink 02/06/09 @ 21:07

This post has 20 feedbacks awaiting moderation...

Leave a comment:

Your email address will not be displayed on this site.
Your URL will be displayed.

Allowed XHTML tags: <p, ul, ol, li, dl, dt, dd, address, blockquote, ins, del, span, bdo, br, em, strong, dfn, code, samp, kdb, var, cite, abbr, acronym, q, sub, sup, tt, i, b, big, small>
(Line breaks become <br />)
(Set cookies for name, email and url)
(Allow users to contact you through a message form (your email will NOT be displayed.))

September 2010
Sun Mon Tue Wed Thu Fri Sat
 << <   > >>
      1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30    

PHP and MySQL

Tips on the use of PHP and MySQL for your website. If you need help with your site, ask and I'll see if I can help. If you need long-term help with your website, let me know as well.

Search

XML Feeds

What is this?

powered by b2evolution free blog software