currently I'm using pagination.but it's only have "Previous page | Next page" but i want to dispaly it like Page: 1,2,3,4,5,6,... I attahc the code currently i using.pls help me <?php $pagenext = $page+1; $result1 = $db->getnewsbycatid($pagenext,$front_latestoncatarecord,$catalogid); if ($page!=0) { $pagepre = $page-1; print "<a href=\"$PHP_SELF?page=$pagepre&catalogid=$catalogid\" class=\"en_b\">$front_previouspage</a> "; } if (!empty($result1)) { print "| <a href=\"$PHP_SELF?page=$pagenext&catalogid=$catalogid\" class=\"en_b\">$front_nextpage</a>"; } ?> PHP:
Something like this: Total pages = Total number of news stories / Results per page Loop through total pages and display links
Heres some code I made a while ago to view pages with virtually any query. $count_query = mysql_query("SELECT * FROM `images` ORDER BY `date` DESC"); //assign the pages $page = $data->get("page"); if($page == "") { $page = 0; } //set this to the number of items you want it to display per page $perpage = 30; if(mysql_num_rows($count_query) < $perpage) { $pages = 0; } else { $pages=ceil(mysql_num_rows($count_query) / $perpage); } $page_start = $perpage * $page; $page_end = $page_start + ($perpage); //last page if($page!=0) { $lastpage = $page-1; $display_pages .= "<a href=\"viewfiles.php?page=$lastpage\"><b>Last Page</b></a> "; } //page number $display_pages .= "Page "; $display_pages .= $page+1 . ""; //next page if($pages-1 > $page) { $nextpage = $page+1; $display_pages .= " <a href=\"viewfiles.php?page=$nextpage\"><b>Next Page</b></a>"; } PHP:
I can't be arsed to read through all that code, so here's what I do: // Parse variables $page_number = (integer) $_GET['page_number']; $newsId = (integer) $_GET['newsId']; $items_per_page = 5; $oNews = new News( $oDataLayer ); $news_count = $oNews->getNewsCount(); $page_count = ceil( $news_count / $items_per_page ); if ( ( $newsId == 0 ) && ( $page_number < 1 ) ) { $page_number = 1; } if ( $page_count > 1 ) { $delimiter = ' | '; for ( $i = 1; $i <= $page_count; $i++ ) { if ( $i == $page_number ) { // Same page, so no need to link $page_links .= $i . $delimiter; } else { $page_links .= '[url=' . ROOT_ABSOLUTE . "/news/$i/]$i" . "[/url]$delimiter"; } } $page_links = 'Page: ' . substr( $page_links, 0, ( strlen( $page_links ) - strlen( $delimiter ) ) ); // Trim trailing delimiter } Code (php): The above determines how many pages there are and generates the link code for each (bbcode, use actual html with yours). Using the above info, I get the required news items for that page with by messing round with an SQL SELECT with a LIMIT clause in there like so: "... LIMIT " . ( ( $page_number - 1 ) * $items_per_page ) . "," . $items_per_page . ";"; You can see it in action Here.
Ok, one thing I noticed from the one example. Grabbing all the data is a really bad idea depending on the size of the dataset. It's great one query you can count the rows and you can get the data. But what happens if you have 100,000 rows? It's going to get very slow due to the dataset size. You're best to do something $sql = "SELECT count(primary_key) as count from table"; PHP: Do the rest of your magic using mysql and then you have say $result['count'] for your number of results. From there you can just do $num_pages = ceil($result['count']/$rows_per_page); PHP: Now you got your number of pages then it's just a simple loop until you hit the number of pages to generate a list. With a bit of work you can create things like forums have pretty easily. With this you can also restrict your dataset query size to just the rows you need based on the page you're on. That way your dataset is small and quick no eating tons of memory with a large array.
check out the companion website for the book 'php and mysql for dynamic websites'. it has a whole section on pagination and the website even has the zip of all the scripts that are used in the book.