maxudaskin Posted February 5, 2008 Share Posted February 5, 2008 How would I change the following to show only 30 topics then if there are more than thirty, add as many pages as needed? <table style="border-top:1px #FFFFFF solid" width="100%" border="0" cellspacing="0" cellpadding="0"> <tr> <td width="3%" height="20" align="center" valign="middle" bgcolor="#333355"><span class="style26"></span></td> <td width="54%" height="20" align="center" valign="middle" bgcolor="#333355"><span class="style26">Subject</span></td> <td width="7%" height="20" align="center" valign="middle" bgcolor="#333355"><span class="style26">Posts</span></td> <td width="16%" height="20" align="center" valign="middle" bgcolor="#333355"><span class="style26">Author</span></td> <td width="20%" height="20" align="center" valign="middle" bgcolor="#333355"><span class="style26">Last Post By </span></td> </tr> <?php $forumid = $_GET['f']; $topicssql = mysql_query("SELECT * FROM forum_posts WHERE topic = '0' && forumid = '{$forumid}'"); while($topics = mysql_fetch_array($topicssql)){ $topicid = $topics['topicid']; $type = $topics['type']; $type_img = constant($type); $postssql = mysql_query("SELECT * FROM forum_posts WHERE topicid = '{$topicid}'"); $posts = mysql_num_rows($postssql); echo "<tr onClick=\"MM_goToURL('parent','forum.php?t=".$topicid."');return document.MM_returnValue\" style=\"background-color:#EFF4FB;\" onMouseOver=\"this.style.backgroundColor='#E5EEF9';\" onMouseOut=\"this.style.backgroundColor='#EFF4FB';\">"; echo '<td class="cursor_link" align="center" valign="middle" style="border-bottom:1px #333355 solid" height="50px">'.$type_img.'</td>'; echo '<td class="cursor_link" align="left" valign="middle" style="border-bottom:1px #333355 solid" height="50px">'.$topics['title'].'</td>'; echo '<td class="cursor_link" align="center" valign="middle" style="border-bottom:1px #333355 solid" height="50px">'.$posts.'</td>'; echo '<td class="cursor_link" align="center" valign="middle" style="border-bottom:1px #333355 solid" height="50px">OOM'.$topics['pid'].' '.$topics['name'].'</td>'; echo '<td class="cursor_link" align="center" valign="middle" style="border-bottom:1px #333355 solid" height="50px"> </td>'; echo '</tr>'; } ?> <tr> <td height="45" colspan="2" align="left"><a href="forum.php?a=post" onmouseout="MM_swapImgRestore()" onmouseover="MM_swapImage('post','','../images/posthv.jpg',1)"></a><a href="forum.php?a=post"></a> <table width="156" border="0" cellspacing="0" cellpadding="2"> <tr> <td align="center"><a href="forum.php?a=post"><div class="status_message status_blue">Post New Thread</div></a></td> </tr> </table></td> <td align="left" colspan="4"> </td> </tr> </table> Quote Link to comment https://forums.phpfreaks.com/topic/89474-solved-forum-limit-30/ Share on other sites More sharing options...
pocobueno1388 Posted February 5, 2008 Share Posted February 5, 2008 You need to look into pagination, search Google for "PHP pagination tutorial". Quote Link to comment https://forums.phpfreaks.com/topic/89474-solved-forum-limit-30/#findComment-458222 Share on other sites More sharing options...
maxudaskin Posted February 5, 2008 Author Share Posted February 5, 2008 Kind of funny, the first result was PHP Freaks: http://www.phpfreaks.com/tutorials/43/0.php Quote Link to comment https://forums.phpfreaks.com/topic/89474-solved-forum-limit-30/#findComment-458225 Share on other sites More sharing options...
pocobueno1388 Posted February 5, 2008 Share Posted February 5, 2008 Kind of funny, the first result was PHP Freaks: http://www.phpfreaks.com/tutorials/43/0.php A lot of people seem to have trouble with that tutorial. I think there was an error in the code, they may have fixed it though, I'm not sure. Quote Link to comment https://forums.phpfreaks.com/topic/89474-solved-forum-limit-30/#findComment-458232 Share on other sites More sharing options...
pocobueno1388 Posted February 5, 2008 Share Posted February 5, 2008 What do you know, I have a fixed version of that tutorial saved on my computer from helping someone else with it. <?php $limit = 10; $query_count = "SELECT count(*) FROM table"; $result_count = mysql_query($query_count) or die("Error: " . mysql_error()); $totalrows = mysql_result($result_count, 0, 0); $numofpages = ceil($totalrows/$limit); if (isset($_GET['page'])) $page = $_GET['page']; else $page = 1; $offset = ($page - 1) * $limit; $query = "SELECT * FROM table LIMIT $offset, $limit"; echo $query .'<br>'; $result = mysql_query($query) or die("Error: " . mysql_error()); //Exit if no records to display if (mysql_num_rows($result) == 0) { exit("Nothing to Display!"); } while ($row = mysql_fetch_array($result)) { echo $row['col']."</br>"; } //Enable the Prev link if not first page if ($page > 1) { echo("<a href=\"index.php?page=".($page-1)."\">PREV</a> "); } else { echo("PREV "); } //Create links for each page in report for ($i=1; $i<=$numofpages; $i++) { if ($i == $page) { echo "$i "; } else { echo "<a href=\"index.php?page=$i\">$i</a> "; } } //Enable the Next link if not last page if ($page < $numofpages) { echo("<a href=\"index.php?page=".($page+1)."\">NEXT</a>"); } else { echo("NEXT"); } ?> So just replace everything in that code with your information. Quote Link to comment https://forums.phpfreaks.com/topic/89474-solved-forum-limit-30/#findComment-458233 Share on other sites More sharing options...
maxudaskin Posted February 5, 2008 Author Share Posted February 5, 2008 When I tested out the first part, it was waaaaaay off... <?php $query_count = "SELECT count(*) FROM forum_posts WHERE topic = '0'"; $result_count = mysql_query($query_count) or die("Error: " . mysql_error()); $totalrows = mysql_result($result_count, 0, 0); $numofpages = ceil($totalrows/$limit); echo $numofpages; ?> And it echoed 11, when the limit is 30 and there are 2 threads. Quote Link to comment https://forums.phpfreaks.com/topic/89474-solved-forum-limit-30/#findComment-458239 Share on other sites More sharing options...
pocobueno1388 Posted February 5, 2008 Share Posted February 5, 2008 So did it work? Quote Link to comment https://forums.phpfreaks.com/topic/89474-solved-forum-limit-30/#findComment-458246 Share on other sites More sharing options...
maxudaskin Posted February 5, 2008 Author Share Posted February 5, 2008 I am working on implementing it now... Quote Link to comment https://forums.phpfreaks.com/topic/89474-solved-forum-limit-30/#findComment-458252 Share on other sites More sharing options...
maxudaskin Posted February 5, 2008 Author Share Posted February 5, 2008 I get the following error... 1Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Resource id #6' at line 1 <table style="border-top:1px #FFFFFF solid" width="100%" border="0" cellspacing="0" cellpadding="0"> <tr> <td width="3%" height="20" align="center" valign="middle" bgcolor="#333355"><span class="style26"></span></td> <td width="54%" height="20" align="center" valign="middle" bgcolor="#333355"><span class="style26">Subject</span></td> <td width="7%" height="20" align="center" valign="middle" bgcolor="#333355"><span class="style26">Posts</span></td> <td width="16%" height="20" align="center" valign="middle" bgcolor="#333355"><span class="style26">Author</span></td> <td width="20%" height="20" align="center" valign="middle" bgcolor="#333355"><span class="style26">Last Post By </span></td> </tr> <?php $forumid = $_GET['f']; $topicssql = mysql_query("SELECT * FROM forum_posts WHERE topic = '0' && forumid = '{$forumid}'"); while($topics = mysql_fetch_array($topicssql)){ $topicid = $topics['topicid']; $type = $topics['type']; $type_img = constant($type); $query_count = "SELECT count(*) FROM forum_posts WHERE topic = '0'"; $result_count = mysql_query($query_count) or die("Error: " . mysql_error()); $totalrows = mysql_result($result_count, 0, 0); $numofpages = ceil($totalrows/$limit); echo $numofpages; if (isset($_GET['page'])){ $page = $_GET['page'];} else{ $page = 1;} $offset = ($page - 1) * $limit; $query = mysql_query("SELECT * FROM forum_posts WHERE topicid = '{$topicid}' LIMIT $offset, $limit"); $result = mysql_query($query) or die("Error: " . mysql_error()); if (mysql_num_rows($result) == 0) { exit("Nothing to Display!"); } $postssql = mysql_query("SELECT * FROM forum_posts WHERE topicid = '{$topicid}'"); $posts = mysql_num_rows($query); echo "<tr onClick=\"MM_goToURL('parent','forum.php?t=".$topicid."');return document.MM_returnValue\" style=\"background-color:#EFF4FB;\" onMouseOver=\"this.style.backgroundColor='#E5EEF9';\" onMouseOut=\"this.style.backgroundColor='#EFF4FB';\">"; echo '<td class="cursor_link" align="center" valign="middle" style="border-bottom:1px #333355 solid" height="50px">'.$type_img.'</td>'; echo '<td class="cursor_link" align="left" valign="middle" style="border-bottom:1px #333355 solid" height="50px">'.$topics['title'].'</td>'; echo '<td class="cursor_link" align="center" valign="middle" style="border-bottom:1px #333355 solid" height="50px">'.$posts.'</td>'; echo '<td class="cursor_link" align="center" valign="middle" style="border-bottom:1px #333355 solid" height="50px">OOM'.$topics['pid'].' '.$topics['name'].'</td>'; echo '<td class="cursor_link" align="center" valign="middle" style="border-bottom:1px #333355 solid" height="50px"> </td>'; echo '</tr>'; } if ($page > 1) { echo("<a href=\"forum.php?page=".($page-1)."\">PREV</a> "); } else { echo("PREV "); } for ($i=1; $i<=$numofpages; $i++) { if ($i == $page) { echo "$i "; } else { echo "<a href=\"forum.php?page=$i\">$i</a> "; } } if ($page < $numofpages) { echo("<a href=\"forum.php?page=".($page+1)."\">NEXT</a>"); } else { echo("NEXT"); } ?> <tr> <td height="45" colspan="2" align="left"><a href="forum.php?a=post" onmouseout="MM_swapImgRestore()" onmouseover="MM_swapImage('post','','../images/posthv.jpg',1)"></a><a href="forum.php?a=post"></a> <table width="156" border="0" cellspacing="0" cellpadding="2"> <tr> <td align="center"><a href="forum.php?a=post"><div class="status_message status_blue">Post New Thread</div></a></td> </tr> </table></td> <td align="left" colspan="4"> </td> </tr> </table> Quote Link to comment https://forums.phpfreaks.com/topic/89474-solved-forum-limit-30/#findComment-458257 Share on other sites More sharing options...
maxudaskin Posted February 5, 2008 Author Share Posted February 5, 2008 http://www.virtualzoom.net/comm/forum.php?f=3 Quote Link to comment https://forums.phpfreaks.com/topic/89474-solved-forum-limit-30/#findComment-458260 Share on other sites More sharing options...
pocobueno1388 Posted February 5, 2008 Share Posted February 5, 2008 Okay, well you have a ton of queries in there and we have no idea which query is getting that error. In the error message for all your queries you need to number them. Instead of just saying "Error", you should put "Error 1" for the first query and so on. Quote Link to comment https://forums.phpfreaks.com/topic/89474-solved-forum-limit-30/#findComment-458263 Share on other sites More sharing options...
maxudaskin Posted February 5, 2008 Author Share Posted February 5, 2008 $query = mysql_query("SELECT * FROM forum_posts WHERE topicid = '{$topicid}' LIMIT $offset, $limit"); $result = mysql_query($query) or die("Error (QUERY): " . mysql_error()); Error (QUERY): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Resource id #6' at line 1 Quote Link to comment https://forums.phpfreaks.com/topic/89474-solved-forum-limit-30/#findComment-458267 Share on other sites More sharing options...
maxudaskin Posted February 5, 2008 Author Share Posted February 5, 2008 I changed it to: <?php $query = mysql_query("SELECT * FROM forum_posts WHERE topicid = '{$topicid}' LIMIT '{$offset}', '{$limit}'"); $result = mysql_query($query) or die("Error (QUERY): " . mysql_error()); ?> and it gives the error: Error (QUERY): Query was empty Quote Link to comment https://forums.phpfreaks.com/topic/89474-solved-forum-limit-30/#findComment-458269 Share on other sites More sharing options...
maxudaskin Posted February 5, 2008 Author Share Posted February 5, 2008 Theoretically it should echo the results as there is an EXIT function: <?php if (mysql_num_rows($result) == 0) { exit("Nothing to Display!"); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/89474-solved-forum-limit-30/#findComment-458275 Share on other sites More sharing options...
pocobueno1388 Posted February 5, 2008 Share Posted February 5, 2008 Your running mysql_query() twice. It should be <?php $query = "SELECT * FROM forum_posts WHERE topicid = '$topicid' LIMIT '$offset', '$limit'"; $result = mysql_query($query) or die("Error (QUERY): " . mysql_error()); ?> I also don't see a need for the brackets around all the variables. Quote Link to comment https://forums.phpfreaks.com/topic/89474-solved-forum-limit-30/#findComment-458278 Share on other sites More sharing options...
maxudaskin Posted February 5, 2008 Author Share Posted February 5, 2008 Well... the script is 4 characters shorter, but not working any better. I didn't know I didn't need the curly brackets. Quote Link to comment https://forums.phpfreaks.com/topic/89474-solved-forum-limit-30/#findComment-458282 Share on other sites More sharing options...
pocobueno1388 Posted February 5, 2008 Share Posted February 5, 2008 It should be a lot more than 4 characters shorter if you changed everything I did. Did you actually copy and paste the code? Even better, copy and paste this then copy and paste what it says: <?php $query = "SELECT * FROM forum_posts WHERE topicid = '$topicid' LIMIT '$offset', '$limit'"; $result = mysql_query($query) or die("Error (QUERY): " . mysql_error() . "<p>With Query:<br>$query<p>"); ?> Quote Link to comment https://forums.phpfreaks.com/topic/89474-solved-forum-limit-30/#findComment-458288 Share on other sites More sharing options...
maxudaskin Posted February 5, 2008 Author Share Posted February 5, 2008 Well, That helped a lot (no sarcasm)... It is now giving a detailed error: Error (QUERY): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''0', '30'' at line 1 With Query: SELECT * FROM forum_posts WHERE topicid = '1' LIMIT '0', '30' Quote Link to comment https://forums.phpfreaks.com/topic/89474-solved-forum-limit-30/#findComment-458291 Share on other sites More sharing options...
pocobueno1388 Posted February 5, 2008 Share Posted February 5, 2008 Oh, duh...you need to get rid of the single quotes in the LIMIT part. <?php $query = "SELECT * FROM forum_posts WHERE topicid = '$topicid' LIMIT $offset, $limit"; $result = mysql_query($query) or die("Error (QUERY): " . mysql_error() . "<p>With Query:<br>$query<p>"); ?> Quote Link to comment https://forums.phpfreaks.com/topic/89474-solved-forum-limit-30/#findComment-458294 Share on other sites More sharing options...
maxudaskin Posted February 5, 2008 Author Share Posted February 5, 2008 And now it echo's 'Nothing to Display' <?php $query_count = "SELECT count(*) FROM forum_posts WHERE topic = '0'"; $result_count = mysql_query($query_count) or die("Error: (result_count) " . mysql_error()); $totalrows = mysql_result($result_count, 0, 0); $numofpages = ceil($totalrows/$limit); if (isset($_GET['page'])){ $page = $_GET['page'];} else{ $page = 1;} $offset = ($page - 1) * $limit; $query = "SELECT * FROM forum_posts WHERE topicid = '$topicid' LIMIT $offset,$limit"; $result = mysql_query($query) or die("Error (QUERY): " . mysql_error() . "<p>With Query:<br>$query<p>"); if (mysql_num_rows($result) == 0) { exit("Nothing to Display!"); } ?> <table style="border-top:1px #FFFFFF solid" width="100%" border="0" cellspacing="0" cellpadding="0"> <tr> <td width="3%" height="20" align="center" valign="middle" bgcolor="#333355"><span class="style26"></span></td> <td width="54%" height="20" align="center" valign="middle" bgcolor="#333355"><span class="style26">Subject</span></td> <td width="7%" height="20" align="center" valign="middle" bgcolor="#333355"><span class="style26">Posts</span></td> <td width="16%" height="20" align="center" valign="middle" bgcolor="#333355"><span class="style26">Author</span></td> <td width="20%" height="20" align="center" valign="middle" bgcolor="#333355"><span class="style26">Last Post By </span></td> </tr> <?php $forumid = $_GET['f']; $topicssql = mysql_query("SELECT * FROM forum_posts WHERE topic = '0' && forumid = '{$forumid}'") or die("Error (TOPICSSQL): " . mysql_error());; while($topics = mysql_fetch_array($result)){ $topicid = $topics['topicid']; $type = $topics['type']; $type_img = constant($type); $postssql = mysql_query("SELECT * FROM forum_posts WHERE topicid = '{$topicid}'") or die("Error ($POSTSQL): " . mysql_error());; $posts = mysql_num_rows($query); echo "<tr onClick=\"MM_goToURL('parent','forum.php?t=".$topicid."');return document.MM_returnValue\" style=\"background-color:#EFF4FB;\" onMouseOver=\"this.style.backgroundColor='#E5EEF9';\" onMouseOut=\"this.style.backgroundColor='#EFF4FB';\">"; echo '<td class="cursor_link" align="center" valign="middle" style="border-bottom:1px #333355 solid" height="50px">'.$type_img.'</td>'; echo '<td class="cursor_link" align="left" valign="middle" style="border-bottom:1px #333355 solid" height="50px">'.$topics['title'].'</td>'; echo '<td class="cursor_link" align="center" valign="middle" style="border-bottom:1px #333355 solid" height="50px">'.$posts.'</td>'; echo '<td class="cursor_link" align="center" valign="middle" style="border-bottom:1px #333355 solid" height="50px">OOM'.$topics['pid'].' '.$topics['name'].'</td>'; echo '<td class="cursor_link" align="center" valign="middle" style="border-bottom:1px #333355 solid" height="50px"> </td>'; echo '</tr>'; } if ($page > 1) { echo("<a href=\"forum.php?f=".$_GET['f']."&page=".($page-1)."\">PREV</a> "); } else { echo("PREV "); } for ($i=1; $i<=$numofpages; $i++) { if ($i == $page) { echo "$i "; } else { echo "<a href=\"forum.php?f=".$_GET['f']."&page=$i\">$i</a> "; } } if ($page < $numofpages) { echo("<a href=\"forum.php?f=".$_GET['f']."&page=".($page+1)."\">NEXT</a>"); } else { echo("NEXT"); } ?> <tr> <td height="45" colspan="2" align="left"> <?php if($logged_in){ ?> <table width="156" border="0" cellspacing="0" cellpadding="2"> <tr> <td align="center"><a href="forum.php?a=post"><div class="status_message status_blue">Post New Thread</div></a></td> </tr> </table> <?php } ?></td> <td align="left" colspan="4"> </td> </tr> </table> Quote Link to comment https://forums.phpfreaks.com/topic/89474-solved-forum-limit-30/#findComment-458302 Share on other sites More sharing options...
pocobueno1388 Posted February 5, 2008 Share Posted February 5, 2008 Which is...good? That just means it didn't find any results in the database that match your query. Quote Link to comment https://forums.phpfreaks.com/topic/89474-solved-forum-limit-30/#findComment-458308 Share on other sites More sharing options...
maxudaskin Posted February 5, 2008 Author Share Posted February 5, 2008 The problem is, is that there should be two matches... Quote Link to comment https://forums.phpfreaks.com/topic/89474-solved-forum-limit-30/#findComment-458314 Share on other sites More sharing options...
pocobueno1388 Posted February 5, 2008 Share Posted February 5, 2008 Type this query into your SQL in your database and make sure you get two results SELECT * FROM forum_posts WHERE topicid = '1' LIMIT 0, 30 Quote Link to comment https://forums.phpfreaks.com/topic/89474-solved-forum-limit-30/#findComment-458317 Share on other sites More sharing options...
maxudaskin Posted February 5, 2008 Author Share Posted February 5, 2008 $query = "SELECT * FROM forum_posts WHERE topicid = '0' && forumid = '{$forumid}' LIMIT $offset,$limit"; $result = mysql_query($query) or die("Error (QUERY): " . mysql_error() . "<p>With Query:<br>$query<p>"); But nothing displays... Quote Link to comment https://forums.phpfreaks.com/topic/89474-solved-forum-limit-30/#findComment-458324 Share on other sites More sharing options...
maxudaskin Posted February 5, 2008 Author Share Posted February 5, 2008 PID is Pilot ID. Quote Link to comment https://forums.phpfreaks.com/topic/89474-solved-forum-limit-30/#findComment-458328 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.