PDA

View Full Version : using a loop to alternate colour of a row


sir pannels
05-18-2003, 02:57 PM
hey,
seen this down but i dont know how to do it, never been any good with loops, whie etc
so how to do make each bg color either, say black or white.. each cell with have a variable for each variable it makes anotehr row so i dont know the fixed number of rows.
so i need it to go black white black white and keep going til there are no more rows, or results whatever.
im completly useless dont have an idea of loop,s has anyone gott this code avalable please?
thanks loads:)
P:thumbsup:

scroots
05-18-2003, 03:08 PM
right i'm quite new to php but i know the process.
Sometihng like this?

$row =0;
if ($row == 1) {
echo "Row with coloured backgrouond";
}esle{
echo "Row with alternate coloured backgrouond";
}

you will just need a loop/while statment to go through it.
also replace the text with the html for the different row colours.

scrppts

stoodder
05-18-2003, 03:11 PM
very easy fist you make your lop then you do % thingy lol:

<?php
$number = NUMBER OF ROWS;
$color1 = First Color;
$color2 = Second Color;
echo<table>";
for($i=1;$i<=$number;$i++) {
if($i % 2) {
echo"<tr bgcolor=$color1>";
} else {
echo"<tr bgcolor=$color2>";
}
echo"
<td>
stuff
</td>
</tr>";
}
echo"</table>";
?>

Ökii
05-18-2003, 11:41 PM
or assuming class definitions
.dark_row { background-color:#777 }
.light_row { background-color:#AAA }

$row_count = 0;
// while(....whatever....) {
echo '<tr class="' .((++$row_count % 2 == 0) ? 'dark_row' : 'light_row'). '">';
// }

Ted Varnson
08-05-2004, 09:40 PM
How could I add that to this switching between two classes (alt1 and alt2)?

if (!$id) {

// print the list if there is not editing

$result = mysql_query("SELECT * FROM smallville_episode_guide ORDER BY `id` ASC",$db);

while ($myrow = mysql_fetch_array($result)) {

printf("%s - %s \n", $myrow["id"], $myrow["episode_name"]);

printf(" <a href=\"%s?id=%s\">[edit]</a> \n", $PHP_SELF, $myrow["id"]);

printf(" <a href=\"%s?id=%s&delete=yes\">[delete]</a><br>", $PHP_SELF, $myrow["id"]);

}

}

hemebond
08-05-2004, 10:14 PM
Client-side Zebra Tables (http://www.alistapart.com/articles/zebratables/).

soundboy
08-05-2004, 10:38 PM
How about the "ternary" operator?
($color='grey' ? 'white' : 'grey')
is equivalent to:
if ($color == 'black') {
$color = 'white';
} else {
$color = 'grey';
}

marek_mar
08-05-2004, 11:01 PM
How about the "ternary" operator?
($color='grey' ? 'white' : 'grey')
is equivalent to:
if ($color == 'black') {
$color = 'white';
} else {
$color = 'grey';
}
Shouldn't the "ternary" thingy be:

($color == 'grey') ? $color = 'white' : $color = 'grey';

Anyways I think using the "rest of division" (or whatever it's in english) operator is better.

dumpfi
08-05-2004, 11:07 PM
if (!$id) {

// print the list if there is not editing

$result = mysql_query('SELECT * FROM smallville_episode_guide ORDER BY `id`',$db);

$class = 'alt2';

while ($myrow = mysql_fetch_array($result)) {

$class = ($class == 'alt1') ? 'alt2' : 'alt1';

echo '<div class="'.$class.'">'.$myrow['id'].' - '.$myrow['episode_name']."\n".' <a href="'.$_SERVER['PHP_SELF'].'?id='.$myrow['id'].'">[edit]</a>'."\n".' <a href="'.$_SERVER['PHP_SELF'].'?id='.$myrow['id'].'&delete=yes">[delete]</a></div>';

}

}dumpfi

soundboy
08-05-2004, 11:11 PM
My bad. Actually it was:

$color = ($color='grey' ? 'white' : 'grey');