I would like to pull data from a table and limit the amount of data shown to 10 per page. Now running this query: SELECT * FROM blog_main ORDER BY date LIMIT 10 Will only allow 10 to show up, I would like the overflow meaning the others to also be displayed but on other pages such as: blog.php?page=2 This page would display the rest and if there was still more a page 3 would be added. How can this be achieved?
The main idea is this: - count how many records you have - divide by 10 to find out how many pages to generate - generate the links Below you'll find an implementation for this (is used to better paginate the WordPress) http://www.seoegghead.com/blog/seo/updated-pagerfix-plugin-code-for-wp-21-p202.html The source file is here: http://www.seoegghead.com/blog/wp-content/plugins/PagerFix.php.txt
<?php $var = @$_GET['q'] ; $trimmed = (trim($var)); // $var2 = @$_GET['Submit'] ; // $trim = (trim($var2)); //\"$targetpage?page=$lpm1&s=$trimmed&Submit=$trim\" $tbl_name="get"; //your table name // How many adjacent pages should be shown on each side? $adjacents = 1; /* First get total number of rows in data table. If you have a WHERE clause in your query, make sure you mirror it here. */ if(empty($var)){ $query = "SELECT COUNT(*) as num FROM $tbl_name"; }else{ $query = "SELECT COUNT(*) as num FROM $tbl_name where `name` like \"%$trimmed%\""; } $total_pages = mysql_fetch_array(mysql_query($query)); $total_pages = $total_pages[num]; /* Setup vars for query. */ $targetpage = "index.php"; //your file name (the name of this file) $limit = 30; //how many items to show per page $page = $_GET['page']; if($page) $start = ($page - 1) * $limit; //first item to display on this page else $start = 0; //if no page var is given, set start to 0 /* Get data. */ if(strlen($var) <= 3 && isset($var) && strlen($var) != 0){ echo "SEARCH MUST BE 3 OR MORE"; $sql = "select * from $tbl_name LIMIT $start, $limit"; }else{ $sql = "select * from $tbl_name where `name` like \"%$trimmed%\" LIMIT $start, $limit"; } $result = mysql_query($sql); $num_rows = mysql_num_rows($result); // showing amount on page /* Setup page vars for display. */ if ($page == 0) $page = 1; //if no page var is given, default to 1. $prev = $page - 1; //previous page is page - 1 $next = $page + 1; $lastpage = ceil($total_pages/$limit); //lastpage is = total pages / items per page, rounded up. $lpm1 = $lastpage - 1; //last page minus 1 /* Now we apply our rules and draw the pagination object. We're actually saving the code to a variable in case we want to draw it more than once. */ $pagination = ""; if($lastpage > 1) { $pagination .= "<div id=\"pages\">"; //previous button if ($page > 1) $pagination.= "<li><a href=\"$targetpage?page=$prev&q=$trimmed\">« previous - </a></li>"; else $pagination.= "<li class=\"nolink\">« previous - </li>"; //DONE //pages if ($lastpage < 7 + ($adjacents * 2)) //not enough pages to bother breaking it up { for ($counter = 1; $counter <= $lastpage; $counter++) { if ($counter == $page) $pagination.= "<li class=\"nolink\">$counter - </li>"; else $pagination.= "<li><a href=\"$targetpage?page=$counter&q=$trimmed&Submit=$trim\">$counter - </a></a>"; //DONE } } elseif($lastpage > 5 + ($adjacents * 2)) //enough pages to hide some { //close to beginning; only hide later pages if($page < 1 + ($adjacents * 2)) { for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++) { if ($counter == $page) $pagination.= "<li class=\"current\">$counter - </li>"; else $pagination.= "<li><a href=\"$targetpage?page=$counter&q=$trimmed&Submit=$trim\">$counter</a></li>"; } $pagination.= "<li class=\"nolink\">...</li>"; $pagination.= "<li><a href=\"$targetpage?page=$lpm1&q=$trimmed&Submit=$trim\">$lpm1</a></li>"; $pagination.= "<li><a href=\"$targetpage?page=$lastpage&q=$trimmed&Submit=$trim\">$lastpage</a></li>"; } //in middle; hide some front and some back elseif($lastpage - ($adjacents * 2) > $page && $page > ($adjacents * 2)) { $pagination.= "<li><a href=\"$targetpage?page=1&q=$trimmed&Submit=$trim\">1</a></li>"; $pagination.= "<li><a href=\"$targetpage?page=2&q=$trimmed&Submit=$trim\">2</a></li>"; $pagination.= "<li class=\"nolink\">...</li>"; for ($counter = $page - $adjacents; $counter <= $page + $adjacents; $counter++) { if ($counter == $page) $pagination.= "<li class=\"current\">$counter - </li>"; else $pagination.= "<li><a href=\"$targetpage?page=$counter&q=$trimmed&Submit=$trim\">$counter</a></li>"; } $pagination.= "<li class=\"nolink\">...</li>"; $pagination.= "<li><a href=\"$targetpage?page=$lpm1&q=$trimmed&Submit=$trim\">$lpm1</a></li>"; $pagination.= "<li><a href=\"$targetpage?page=$lastpage&q=$trimmed&Submit=$trim\">$lastpage</a></li>"; } //close to end; only hide early pages else { $pagination.= "<li><a href=\"$targetpage?page=1&q=$trimmed&Submit=$trim\">1</a></li>"; $pagination.= "<li><a href=\"$targetpage?page=2&q=$trimmed&Submit=$trim\">2</a></li>"; $pagination.= "<li class=\"nolink\">...</li>"; for ($counter = $lastpage - (2 + ($adjacents * 2)); $counter <= $lastpage; $counter++) { if ($counter == $page) $pagination.= "<li class=\"current\">$counter</li>"; else $pagination.= "<li><a href=\"$targetpage?page=$counter&q=$trimmed&Submit=$trim\">$counter</a></li>"; } } } //next button if ($page < $counter - 1) $pagination.= "<li><a href=\"$targetpage?page=$next&q=$trimmed&Submit=$trim\">next »</a></li>"; else $pagination.= "<li class=\"nolink\">next »</li>"; $pagination.= "</div>\n"; } ?> PHP: <?= $pagination // where you want the pages to be printed ?> PHP: This code works well but its been modified for some of my scripts and sites... it will give you an example thos of how to do this..