It's so much easier to use the mod (%) operator:

using the mod operator, you can check if a variable is divisible by some other number without leaving a remainder.
For this example, we want to change every other row, so we would compare our $count against 2 to see if it leaves a remainder:
$bg = ($count%2==0) ? "#000000" : "FF0000";


What this line does is FIRST, it checks to see if $count/2 leaves no remainder. If this is true, it sets the $bg var to "#000000"
If it is false, it sets $bg to "#FF0000"


It only requires the addition of one line of code, and to replace your row background color with a php variable.
Also, you don't need to use printf(), since you aren't specifying any formatting.


See code below:

code:---------------------------------------------------------------------------------------
$result = mysql_query("SELECT * FROM albums where id <15");

$Count = @mysql_num_rows($result);

echo "<table border=1 cellpadding=3 cellspacing=0 bordercolor='#000000'>\n";
echo "<tr><td bgcolor='#666666'>ID</td><td bgcolor='#666666'>ARTIST</td><td bgcolor='#666666'>TITLE</td><td bgcolor='#666666'>LABEL</td><td bgcolor='#666666'>PRICE</td></tr>\n";


 for ($count = 0; $count < $Count; $count++) {
   // Extract post details from database
    $myrow = mysql_fetch_array($result);
   $id = $myrow ['id'];
   $artist = $myrow ['artist'];
 $title = $myrow ['title'];
 $label = $myrow ['label'];
 $price = $myrow ['price'];

$bg = ($count%2==0) ? "#000000" : "FF0000";
echo "<tr bgcolor='".$bg."'><td>$id</td><td>$artist</td><td>$title</td><td>$label</td><td>£$price</tr>\n";
}
echo "</table>\n";





At 01:08 PM 11/15/2003, you wrote:
Well, first of all Ill just scrap you script since this one is so easy
its better to do it from scratch.

OK, somewhere in your script you have the code that accually
aoutputs the tables you are working with. Im refferring to lines
here, and Im meaning the bottom of this document which is
the scipt you posted, and I have numbered the lines.

Overview of your script.
Line 5 - we print out the table header
Line 11 - 23 is the loop which prints out all the lines, or rows.
Line 24 closes the table.

So what we have to do?

First we need to declare the values we want to use as backround
colours, lets use logical names :

(fig a)
    $backcolor1="#fafafa";
    $backcolor2="#c0c0c0";
    $backcolor=$backcolor1;    // we assign color 1

This code has to be written before the loop starts, so somewhere
before line 11.

In the loop (11-23) we need to switch between the colours where
we write the colour of the <tr>. So we write something like :

(fig b)
    echo '<tr style="background-color:' . $backcolor . ';">';
    // continue with the rest of <td....... </td></tr> here
    // which is -> your code.

This will print out the first background color, nice. Now we need it
to switch color, so we need to add a little logic. This will be inserted
right before the loop ends (infact, you can put it where ever you like
aslong as its in the loop).

(fig c)
    if($backcolor=backcolor1)
    $backcolor=$backcolor2;
    else
    $backcolor=$backcolor1;

As you see above the logic is quite simple, if the color is 1 -> we set it
to 2,
else we set it to 1. If you think of it, if you process this logic over and
over again
you will infact get 1, 2, 1, 2, 1, 2, 1, 2 all the time, :) Nice!

There you have it, and I hope you got the hang of it.

To take your code and implement my colorswither all you need to do is,

1. On line 21 replace #000000 width $backcolor
2. Insert the logic (figc), all lines, into line 19
3. Place fig a in line 4.

--
Kim Steinhaug
---------------------------------------------------------------
There are 10 types of people when it comes to binary numbers:
those who understand them, and those who don't.
---------------------------------------------------------------


The code for return the top ten result is : 1 $result = mysql_query("SELECT * FROM albums where id <15"); 2 3 $Count = @mysql_num_rows($result); 4 5 echo "<table border=1 cellpadding=3 cellspacing=0 6 bordercolor='#000000'>\n"; 7 echo "<tr><td bgcolor='#666666'>ID</td><td 8 bgcolor='#666666'>ARTIST</td><td bgcolor='#666666'>TITLE</td><td 9 bgcolor='#666666'>LABEL</td><td bgcolor='#666666'>PRICE</td></tr>\n"; 10 11 for ($count = 0; $count < $Count; $count++) { 12 // Extract post details from database 13 $myrow = mysql_fetch_array($result); 14 $id = $myrow ['id']; 15 $artist = $myrow ['artist']; 16 $title = $myrow ['title']; 17 $label = $myrow ['label']; 18 $price = $myrow ['price']; 19 20 printf("<tr 21bgcolor='#000000'><td>$id</td><td>$artist</td><td>$title</td><td>$label</t d> 22<td>£$price</tr>\n"); 23} 24 echo "</table>\n";

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

-- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php



Reply via email to