Jump to content

Alternating Row Colors (using 3 Colors?)


Recommended Posts

I'm trying to do alternating row colors for a table. Now - I can get two colors just fine and dandy...but because I feel like torturing myself, I made up a design that requires three. I've been fighting with the code for the past few days and I have no clue what to do anymore.If someone could help me I would be very thankful.(also - I am still very green to php..so if you could use small words - I'd love ya :))code from Header

<?phpfunction rowcolor($count){$class1 = "row-1";$class2 = "row-2";$class3 = "row-3";if($count % 1 != 1){return $class1;}elseif($count % 1 != 2){return $class2;}elseif($count % 1 != 3){return $class3;}} ?> <?phpfunction tableborder($count){$class1 = "table1";$class2 = "table2";$class3 = "table3";if($count % 3 != 1){return $class1;}elseif($count % 3 != 2){return $class2;}elseif($count % 3 != 3){return $class3;}} ?>

and then this is from the page where the table is:

$count = 0;while ($row = mysql_fetch_assoc($result)) {$rowcolor = rowcolor($count);$tableborder = tableborder($count);$id = $row['id'];?><table class="<? echo $tableborder; ?>" width="100%" cellpadding="0" cellspacing="0"><tr valign="top" class="<? echo $rowcolor; ?>"><td width="75" align="center" valign="middle"></td></tr></table><br /><?$count++;}?>

If I'm doing something wrong - well it wouldn't surprise me. But again - any help would be much appreciated :)Cheers!

Edited by lu13cky
Link to post
Share on other sites
<?phpfunction rowcolor($count){$class1 = "row-1";$class2 = "row-2";$class3 = "row-3";if($count % 1 != 1){return $class1;}elseif($count % 1 != 2){return $class2;}elseif($count % 1 != 3){return $class3;}}

You just need to change your conditional statements. For starters you have $count % 1 when it should be $count % 3 and you're checking for the wrong remainders. You should be checking for 0, 1, 2 not 1, 2, 3. Edited by jkloth
Link to post
Share on other sites

The result of any number mod 1 is always 0, btw. So this statement:if($count % 1 != 1)will always be true, and so it would always return the class 1. You could probably condense your function into this:

function rowcolor($count){  $nr = ($count % 3) + 1;  return 'row-' . $nr;}

Link to post
Share on other sites
The result of any number mod 1 is always 0, btw. So this statement:if($count % 1 != 1)will always be true, and so it would always return the class 1. You could probably condense your function into this:
function rowcolor($count){  $nr = ($count % 3) + 1;  return 'row-' . $nr;}

I love you. :)Thank you soooo much. It worked. *dances*
Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...