lu13cky Posted March 1, 2010 Share Posted March 1, 2010 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! Link to comment Share on other sites More sharing options...
ShadowMage Posted March 1, 2010 Share Posted March 1, 2010 <?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. Link to comment Share on other sites More sharing options...
justsomeguy Posted March 1, 2010 Share Posted March 1, 2010 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 comment Share on other sites More sharing options...
lu13cky Posted March 1, 2010 Author Share Posted March 1, 2010 awesome thanks guys - I will try this out and see what happens *crosses fingers* Link to comment Share on other sites More sharing options...
Mencarta Posted March 1, 2010 Share Posted March 1, 2010 I suggest you use the PHP GD Library: http://php.net/manual/en/book.image.php Link to comment Share on other sites More sharing options...
justsomeguy Posted March 1, 2010 Share Posted March 1, 2010 GD is for working with image data, this is just a counting issue. Link to comment Share on other sites More sharing options...
lu13cky Posted March 2, 2010 Author Share Posted March 2, 2010 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 comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.