Aureole Posted July 26, 2007 Share Posted July 26, 2007 Ok so here's the deal, basically the following code queries the database and shows all articles in the following format: ID Title Edit Delete This is all good but what if I had a thousand articles? It will show a thousand, how can I make it so say 20 are shown per page kind of like pagination I guess. <?php if (in_array($member['id'], $allowednewsmanage)) { $query = "SELECT id, title FROM news"; $result = @mysql_query($query); if ($result) { echo "ID Title Delete Edit<br />"; while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) { echo "{$row['id']} {$row['title']} <a href=\"news/delete.php?id={$row['id']}\">Delete</a> <a href=\"news/edit.php?id={$row['id']}\">Edit</a><br />"; } } else { echo "There are no news articles to display."; } } else { echo ""; } ?> Quote Link to comment Share on other sites More sharing options...
teng84 Posted July 26, 2007 Share Posted July 26, 2007 it a long process but heres the great start for you $query = "SELECT id, title FROM news limit 10,20"; that means you will query the record on the 10th up to the 20th record now your prob is the link on the bottom or top but theres allot of codes out there Quote Link to comment Share on other sites More sharing options...
Aureole Posted July 26, 2007 Author Share Posted July 26, 2007 it a long process but heres the great start for you $query = "SELECT id, title FROM news limit 10,20"; that means you will query the record on the 10th up to the 20th record Thanks for the start. now your prob is the link on the bottom or top but theres allot of codes out there I don't understand what you mean when you say the link on the bottom or top. Anyway I will try search Google.com for something but I don't know what to search for, what should I type? ??? Quote Link to comment Share on other sites More sharing options...
teng84 Posted July 26, 2007 Share Posted July 26, 2007 ok when you go to google and searh you will at the bottom of the page the number representing the page that when you click navigates to another page depending on the number you choose thats what im talking Quote Link to comment Share on other sites More sharing options...
Aureole Posted July 26, 2007 Author Share Posted July 26, 2007 I understand what pagination is. I just didn't know what to search for like "Paginate mysql results with php" or whatever. Found it now anyway, but thanks. Quote Link to comment Share on other sites More sharing options...
Aureole Posted July 26, 2007 Author Share Posted July 26, 2007 I got it working by using the following code, now I'm going to try find a way so you can click Next or Previous and it shows the results with ajax. Anyone got any ideas? <?php if (in_array($member['id'], $allowednewsmanage)) { if(!isset($_GET['page'])){ $page = 1; } else { $page = $_GET['page']; } // Define the number of results per page $max_results = 5; $from = (($page * $max_results) - $max_results); // Perform MySQL query on only the current page number's results $query = mysql_query("SELECT * FROM news LIMIT $from, $max_results"); echo "ID Title Delete Edit<br />"; while($row = mysql_fetch_array($query)){ echo "{$row['id']} {$row['title']} <a href=\"news/delete.php?id={$row['id']}\">Delete</a> <a href=\"news/edit.php?id={$row['id']}\">Edit</a><br />"; } $total_results = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM news"),0); $total_pages = ceil($total_results / $max_results); echo "<center>Select a Page<br />"; if($page > 1){ $prev = ($page - 1); echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$prev\"><<Previous</a> "; } for($i = 1; $i <= $total_pages; $i++){ if(($page) == $i){ echo "$i "; } else { echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$i\">$i</a> "; } } if($page < $total_pages){ $next = ($page + 1); echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$next\">Next>></a>"; } echo "</center>"; } else { echo ""; } ?> Quote Link to comment 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.