SelfObscurity Posted November 28, 2009 Share Posted November 28, 2009 Evening, It's been over a year since I've dealt with PHP...I actually stepped a way for a while. I'm working back into re-teaching, since I was learning when I was actively coding. I found a simple way to call for alternating rows via PHP based on the row #. The snippet of code works, but I don't know enough about the mathematics end of the coding to stop this duplicating. See code below. <?php $result = mysql_query("SELECT * FROM navigation"); while($row = mysql_fetch_array($result)) { for($i=0; $i<2; $i++) { $class = 'row' . ($i % 2); ?> And then the code that calls the row <td valign="middle" class="<?php echo "$class" ?>"><span class="red2">»</span> <a href="<?php echo $row['url'] ?>"><?php echo $row['text'] ?></a></td> </tr> <?php } } ?> The code works, but it is doubling each row. Please help? I assume it is the snippet: for($i=0; $i<2; $i++) ...but don't know enough and can't reallty find any resources to assist. Jon Link to comment https://forums.phpfreaks.com/topic/183194-simple-alternating-rows-gone-wrong/ Share on other sites More sharing options...
trq Posted November 28, 2009 Share Posted November 28, 2009 You don't need the for loop at all. Link to comment https://forums.phpfreaks.com/topic/183194-simple-alternating-rows-gone-wrong/#findComment-966798 Share on other sites More sharing options...
SelfObscurity Posted November 28, 2009 Author Share Posted November 28, 2009 Thanks for the post thorpe. I remove the loop and i get a basic syntax error...so maybe I am unsure how this is supposed to look. I've never dont this particular thing before, thus the use of a tutorial snippet. How should this look? Link to comment https://forums.phpfreaks.com/topic/183194-simple-alternating-rows-gone-wrong/#findComment-966803 Share on other sites More sharing options...
trq Posted November 28, 2009 Share Posted November 28, 2009 Its quite simple. <?php $i=0; while ($row = mysql_fetch_array($result)) { $class = ($i % 2) ? 'even' : 'odd'; ?> <td valign="middle" class="<?php echo $class ?>"><span class="red2">»</span> <a href="<?php echo $row['url'] ?>"><?php echo $row['text'] ?></a></td> <?php $i++; } ?> Link to comment https://forums.phpfreaks.com/topic/183194-simple-alternating-rows-gone-wrong/#findComment-966806 Share on other sites More sharing options...
SelfObscurity Posted November 28, 2009 Author Share Posted November 28, 2009 Interesting. I see it is not duplicating or anything, but it is omitting row1 of the table? Link to comment https://forums.phpfreaks.com/topic/183194-simple-alternating-rows-gone-wrong/#findComment-966808 Share on other sites More sharing options...
trq Posted November 28, 2009 Share Posted November 28, 2009 Post your code. Link to comment https://forums.phpfreaks.com/topic/183194-simple-alternating-rows-gone-wrong/#findComment-966809 Share on other sites More sharing options...
SelfObscurity Posted November 28, 2009 Author Share Posted November 28, 2009 Caught the problem, I doubled a line of code! Thanks for your help, I need to read up on this a bit more Link to comment https://forums.phpfreaks.com/topic/183194-simple-alternating-rows-gone-wrong/#findComment-966810 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.