MrXander Posted October 21, 2012 Share Posted October 21, 2012 Hi guys. I'm making a comment system for my website but I'm a bit stuck. I need to be able to alternate the style of every other comment with the stylesheet I have set, but how do I do this while fetching results from my database? I need to change the <li class="comment_even"> to <li class="comment_odd"> every other comment. <?php $sql2 = mysql_query("SELECT * FROM `comments` WHERE `relatingto`='".$_GET['id']."' AND `hidden`='No' AND `deletedbyadmin`='No'"); while($dat2 = mysql_fetch_array($sql)){ ?> <li class="comment_even"> <div class="author"><img class="avatar" src="images/avatar.gif" width="32" height="32" alt="" /><span class="name"><a href="#"><?php echo $dat2['poster']; ?></a></span> <span class="wrote">wrote:</span></div> <div class="submitdate"><a href="#"><?php echo $dat2['time']; ?></a></div> <p><?php echo $dat['comment']; ?></p> </li> <?php } ?> Quote Link to comment https://forums.phpfreaks.com/topic/269730-alternating-style-while-fetching-database-info/ Share on other sites More sharing options...
Pikachu2000 Posted October 21, 2012 Share Posted October 21, 2012 (edited) You just need to implement some logic like this. If you run it, you should be able to figure it out easily enough. <?php $i = 0; while( $i < 10 ) { $class = $i %2 == 0 ? 'even' : 'odd'; echo "$class<br>"; $i++; } Edited October 21, 2012 by Pikachu2000 Quote Link to comment https://forums.phpfreaks.com/topic/269730-alternating-style-while-fetching-database-info/#findComment-1386680 Share on other sites More sharing options...
MrXander Posted October 21, 2012 Author Share Posted October 21, 2012 Thanks for your reply. I have attempted to use this, but it just massively duplicates the comments that I am pulling (ten fold). Can you advise? Quote Link to comment https://forums.phpfreaks.com/topic/269730-alternating-style-while-fetching-database-info/#findComment-1386681 Share on other sites More sharing options...
Pikachu2000 Posted October 21, 2012 Share Posted October 21, 2012 It isn't meant to be cut and paste, it's an example. Post the new code. Quote Link to comment https://forums.phpfreaks.com/topic/269730-alternating-style-while-fetching-database-info/#findComment-1386682 Share on other sites More sharing options...
jcbones Posted October 21, 2012 Share Posted October 21, 2012 Try this: <?php $sql2 = mysql_query("SELECT * FROM `comments` WHERE `relatingto`='".$_GET['id']."' AND `hidden`='No' AND `deletedbyadmin`='No'"); while($dat2 = mysql_fetch_array($sql)){ $comment = ($comment == 'comment_odd') ? 'comment_even' : 'comment_odd'; //if $comment is 'comment_odd, change to comment_even, else change to comment_odd. Numbers start off odd right? ?> <li class="<?php echo $comment; ?>"> <div class="author"><img class="avatar" src="images/avatar.gif" width="32" height="32" alt="" /><span class="name"><a href="#"><?php echo $dat2['poster']; ?></a></span> <span class="wrote">wrote:</span></div> <div class="submitdate"><a href="#"><?php echo $dat2['time']; ?></a></div> <p><?php echo $dat['comment']; ?></p> </li> <?php } ?> Quote Link to comment https://forums.phpfreaks.com/topic/269730-alternating-style-while-fetching-database-info/#findComment-1386683 Share on other sites More sharing options...
JohnTipperton Posted October 21, 2012 Share Posted October 21, 2012 what about trying to use % 2 <?php $x=0; while($rowData=mysql_fetch_array($rs)) { $x=$x+1; if ($x % 2) { echo '<tr>'; }else{ echo '<tr class="alt">'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/269730-alternating-style-while-fetching-database-info/#findComment-1386722 Share on other sites More sharing options...
Barand Posted October 21, 2012 Share Posted October 21, 2012 what about trying to use % 2 Just what Pikachu suggested in the first reply to this post Quote Link to comment https://forums.phpfreaks.com/topic/269730-alternating-style-while-fetching-database-info/#findComment-1386730 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.