Seaholme Posted July 27, 2010 Share Posted July 27, 2010 Hey all, I'm trying to get my site's Chat to show only the last 20 posts, but I'm not doing such a great job of it. It'll show only 20 posts, but it always shows from zero to 20, when I want it to show the most recent 20. Can anybody help me out? :/ <?php include "chatconnect.php"; $getnummessages="SELECT COUNT(*) as messagecount from chatmessages"; $getnummessages2=mysql_query($getnummessages) or die("blah"); $getnummessages3= mysql_result($getnummessages2, 0); if($getnummessages3 > '21') { $startrow= $getnummessages3 - '20'; } else { $startrow=0; } $getmsg="SELECT id, name, message, IP FROM chatmessages ORDER BY postime DESC limit $startrow,$getnummessages3"; $getmsg2=mysql_query($getmsg) or die(mysql_error()); while($row=mysql_fetch_array($getmsg2)) { $message=Smiley($message); //Smiley faces echo $row['name']; echo " (#"; echo $row['IP']; echo ") "; echo $row['message']; $result3 = mysql_query("SELECT moderator FROM players WHERE id=".$_SESSION['id']) or die(mysql_error()); while($row2 = mysql_fetch_array( $result3 )) { $mod2=$row2['moderator'];} if($mod2 == '1'){ ?> <form name="input" action="deletechatreply.php" method="post"> <input type="hidden" name="id" value="<? echo $rows['id']; ?>"> <input type="submit" value="Delete" /> </form> <?php } else{} echo '<br><hr><br>'; } function Smiley($texttoreplace) { $smilies=array( '' => "<img src='images/smile.gif'>", ':blush' =>"<img src='images/blush.gif'>", ':angry' =>"<img src='images/angry.gif'>", ''=> "<img src='images/shocked.gif'>", 'fuck' => "$#$%", 'Fuck' => "&$#@" ); $texttoreplace=str_replace(array_keys($smilies), array_values($smilies), $texttoreplace); return $texttoreplace; } ?> <script> setTimeout("window.location.replace('chatlog.php')",20000); </script> Thanks if you can! Quote Link to comment https://forums.phpfreaks.com/topic/209017-need-help-getting-a-limit-function-to-work/ Share on other sites More sharing options...
wildteen88 Posted July 27, 2010 Share Posted July 27, 2010 You'd use a single sql query, SELECT COUNT(*) AS messagecount FROM chatmessages ORDER BY postime DESC LIMIT 20 Quote Link to comment https://forums.phpfreaks.com/topic/209017-need-help-getting-a-limit-function-to-work/#findComment-1091787 Share on other sites More sharing options...
xcandiottix Posted July 27, 2010 Share Posted July 27, 2010 p.s. shouldn't if($getnummessages3 > '21') be greater then or equal to? Quote Link to comment https://forums.phpfreaks.com/topic/209017-need-help-getting-a-limit-function-to-work/#findComment-1091794 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.