FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


alternate row color





dark_paladin
Is it possible to apply an alternate class for each row in a table using php? For example, I want the background of the first row of my table to be black and the background of the second row to the white, then repeat the same pattern over and over automatically. Does anyone know a simple way of doing that? Thanks.
Jack_Hammer
It depends what you want it for?, automated for say a script or just just doing it yourself?
sonam
I am not sure what you want but maybe this script can help you:

data.txt
Quote:
Code:
something|222222
something More|555555
something MORE More|666666
something MORE MORE MORE|999999


rows.php
Code:
<?php
$file = file_get_contents("data.txt");
$lines = explode("\n", $file);
$counting = count($lines);

for($x=0; $x<$counting; $x++) {   
list($some, $num) = explode("|", $lines[$x]);
$rows[] = $x;
$rows = range(1, $counting, 2);

if(in_array($x, $rows)) {
$n = 1;
} else {
$n = 2;
}
switch($n) {
   case "1": $row = "<tr>
      <td width='420' height='30' valign='top'  bgcolor='ffffff'>$some have $num</td>
      </tr>"; break;
   case "2": $row = "<tr>
      <td width='420' height='30' valign='top'  bgcolor='bb5599'>$some have $num</td>
      </tr>"; break;
}

echo "<table width='420' border='0' cellpadding='0' cellspacing='0' align='center'>
   $row
</table>";
}
?>


Sonam
dark_paladin
well, I want an automatic script that makes all odd row number one color and all even row number the other color...
kv
dark_paladin wrote:
well, I want an automatic script that makes all odd row number one color and all even row number the other color...


Hope this helps
Code:

if($row_num%2==1)
   echo "<tr><td width='420' height='30' valign='top'  bgcolor='ffffff'>data here</td></tr>";
else
   echo "<tr><td width='420' height='30' valign='top'  bgcolor='bb5599'>data here</td></tr>";


$row_num is the row number
dark_paladin
Thanks for all the scripts, but they are not what I have hoped for... I guess I'd just do them by hands then...
mathiaus
Hope one of these proves useful Smile

http://codewalkers.com/seecode/100.html
http://www.phpfreaks.com/tutorials/5/0.php
http://www.oxyscripts.com/item-401.html

If you mean you have a table already
http://forums.devarticles.com/archive/t-714/Alternate-row-colors-in-javascript-
post near bottom by wolverine
http://www.sitepoint.com/article/background-colors-javascript

notice the first uses php to create a table while the second set uses javascript as the tables already created Wink
Related topics
Nameservers
script backup database
Color Ranks [PL]
White font color
Okay, I'm done trying to use CSS for layout.
survey Insert in Php
Text color of main page
How To : Improve Your PHP Programming
751 Useful Windows XP Files
Tutorial: PHP Installed Modules Dynamic Reference Tool
"Five in a row" contest! [1500 frih$]
What Are Your Thoughts On The Death Penalty?
Expanding universe and gravity
Get alternate table color with php
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.