Last comments

In response to: Alternate Colors on MySQL result rows

valzubiri [Member]
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

In response to: Alternate Colors on MySQL result rows

Jan [Visitor]
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
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    

All Blogs

All the Blog Entries appear here.

Search

XML Feeds

What is this?

powered by b2evolution free blog software