PDA

View Full Version : Alternating Row Colors


SDP2006
01-31-2004, 02:23 PM
I've always wanted to know how to alternate row colors, but I couldn't ever understand the tutorials. So, for once, can someone modify the code below to give me alternating row colors? Hopefully I will memorize it and finally understand it.
<html>
<head>
<title>Link Tracker Hits</title>
<style type="text/css">
body { font-family: Lucida Sans Unicode, sans-serif; }
h2 { margin: 0; }
td { padding : 4px; }
</style>
</head>
<body bgproperties="fixed" background="../styleimgs/bg.gif" vlink="#0000FF" alink="#0000FF">
<h2>Link Tracker : Net-Riches.com</h2>
<table border="1" cellpadding="0" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" width="100%" bgcolor="white">
<tr>
<td width="14%">
<b>Category</b>
</td>
<td width="36%">
<b>Site Name</b>
</td>
<td width="43%">
<b>Link to Site</b>
</td>
<td width="7%">
<b>Clicks</b>
</td>
</tr>
<?php
$link = mysql_connect("$host","$user","$pass") or die("Could not connect to database!");
mysql_select_db("$db","$link") or die("Could not select database!");
$query = "SELECT id,des,cat,href,hits FROM linktracker ORDER BY hits DESC";
$result = mysql_query($query, $link) or die(mysql_error());
if($result){
if(mysql_num_rows($result) == 0){
echo "No information in database table!";
}
else{
while($row = mysql_fetch_assoc($result)){
$id = $_row['id'];
echo "<tr>";
echo "<td width='14%'>";
echo($row['cat']);
echo "</td>";
echo "<td width='36%'>";
echo($row['des']);
echo "</td>";
echo "<td width='43%'>";
echo '<a href="'.$row['href'].'">';
echo($row['href']);
echo "</a>";
echo "</td>";
echo "<td width='7%'>";
echo($row['hits']);
echo "</td>";
echo "</tr>";
}
}
}
?>
</table>
</body>
</html>

Thanks to all

raf
01-31-2004, 02:50 PM
It is realy simple.

The CSS way

$i = 1 ;
while($row = mysql_fetch_assoc($result)){
$id = $_row['id']; // ???? what is thius used for ???
if ($i % 2 == 0) { // so if it is an even rownumber
echo ('<tr class="even">') ;
} else {
echo ('<tr class="noteven">') ;
}
$i ++ ;
...
}

the plain html way

$i = 1 ;
while($row = mysql_fetch_assoc($result)){
$id = $_row['id']; // ???? what is thius used for ???
if ($i % 2 == 0) {
echo ('<tr bgcolor="green">') ;
} else {
echo ('<tr bgcolor="red">') ;
}
$i ++ ;
...
}


I would need to dig deep to write up a tutorial about these 5 lines

SDP2006
01-31-2004, 03:12 PM
Thanks a million, but one problem

while($row = mysql_fetch_assoc($result)){
$i = 1 ;
if ($i % 2 == 0) {
echo ('<tr bgcolor="lightcyan">') ;
} else {
echo ('<tr bgcolor="white">') ;
}
$i ++ ;

It is only echoing white tr bgcolors.

You can see it here http://www.net-riches.com/newnr/linktracker/

Thanks

raf
01-31-2004, 03:28 PM
That is because you have

while($row = mysql_fetch_assoc($result)){
$i = 1 ;


Which sets the $i back to 1 for each iteration.

I posted

$i = 1 ;
while($row = mysql_fetch_assoc($result)){

so initally it's 1, but whith each iteration, the $i ++ a bit further down, increments the $i so that the modulus will go from 0 to 5 to 0 to 5

mordred
01-31-2004, 03:32 PM
If you set $i to 1 inside the while loop, the value will be 1 on each new iteration. Hence, your modulo operation evaluates always to 1, and that's why you always jump into the else-statement.

You need to use the code that raf gave you, $i needs to be defined outside the loop as a starting value.

EDIT: Posts crossed, raf 1 - mordred 0.

raf
01-31-2004, 03:45 PM
mordred : Hence, your modulo operation evaluates always to 1,
raf : so that the modulus will go from 0 to 5 to 0 to 5,
It of course goes from 0 to 1 and back like mordred wrote

mordred 1 - raf 1.

SDP2006
02-01-2004, 01:31 AM
Thanks, all it works great.

Now, I like to understand the code I use. I don't like using code that I don't understand so what does this mean?

if ($i % 2 == 0)

Thanks
- Stevie

Taylor_1978
02-01-2004, 03:28 AM
The % symbol is asking you how many remainders you have.

EG. If $i = 3.. how many remainders would you have if it was divided by 2... 1!

Hence, if you wanted 3 alternating color rows you would do:

[PHP]
if ($i % 3 = 0) {
// First row
}
if ($i % 3 = 1) {
// First row
}
if ($i % 3 = 2) {
// First row
}

Hope that clears it up for you.

Taylor :thumbsup:

Celtboy
02-02-2004, 11:01 PM
Oooh oooh! pick me! pick me!
It's called the "modulus operator"
hehe

purty good explanation. Syntax:
number % divisor = remainder

so if you had the fraction 21/7... that would translate into:

21 % 7 would give you 0
21 % 6 would give you 3 (because 21/6 is 3 (18) with a remainder of 3.

It's useful for stuff like this.

liorean
02-02-2004, 11:09 PM
Technically, there's a difference between remainder and modulus. For positive numbers they are the same, but modulus always returns a positive number, while remainder always returns what is left after you have removed the integer part of the division, that being negative or positive depending on situation.

JavaScript really has remainder, not modulus. I don't know which is the case for php.