I have been teaching myself php and doing ok at it. At least I am happy with my progress. I have a hard time understanding how pagination works no matter how much reading I do. I was hoping some of you might spit some thoughts at me that helped you or might help me understand how to get my mind around the idea. Lets start with , I run a query and echo a list.....how might I paginate that?
Hey, I wrote a guide for pagination a long time ago, it is geared towards novices so it should give you a good understanding. I cant post links as I am new so replace the DOT... http://www.devshed DOT com/c/a/PHP/Paginating-MySQL-Data-with-PHP/ If you have any questions with it, come back and I will gladly help.
OK that makes more sense. I would need to sit down and do it myself. One of the things I am dealing with is I am building a custom plugin for something in Joomla. There is an API I am using (below) I am trying to relate what I am seeing here to what I am reading in tutorials. 1.9.4.1 Pagination Part of the list framework, CB API supports a powerful, yet simple, paging API using this function: /** * Writes the html links for pages inside tabs, eg, previous 1 2 3 ... x next * @param array: paging parameters. ['limitstart'] is the record number to start dislpaying from will be ignored * @param string postfix for identifying multiple pagings/search/sorts (optional) * @param int Number of rows to display per page * @param int Total number of rows * @param string cb task to link to (default: userProfile) * @return string html text displaying paging */ function _writePaging($pagingParams, $postfix, $limit, $total, $task="userProfile")
Pagination at it's simplest is like this: define ('MAX_PAGES', 10); define ('ITEMS_PER_PAGE', 10); // validate input $page = intval (@$_GET['page']); $page = ($page > MAX_PAGES || $page < 1) ? 1 : $page; $result = mysql_query ('SELECT * FROM `table` WHERE 1 LIMIT '.($page - 1).', '.ITEMS_PER_PAGE); while ($item = mysql_fetch_assoc ($result)) { // output the items } // Pagination links if ($page > 1) { echo '<a href="?page='.($page-1).'">Prev page</a>'; } if ($page < 10) { echo '<a href="?page='.($page+1).'">Next page</a>'; } PHP:
cool i will definitly try this out. as far as the code above if someone has written this into a function how might i use the said function?
exam, sorry but your query is wrong... It should be $result = mysql_query ('SELECT * FROM `table` WHERE 1 LIMIT '. ($page - 1) * ITEMS_PER_PAGE . ', ' . ITEMS_PER_PAGE); (otherwise you would start from 0 for first page, 1 for second page, etc, while you want to start from 0 for first page, then 10 (for example) for second page, etc...)
mnymkr, It sounds like you already have an API with a paging function and your really just stuck with how to use that function. If thats the case, have you tried calling it to see what happens? To call the function you would just do: _writePaging($pagingParams,$postfix,20,1000,$task="userProfile"); PHP:
i guess there are already some answers in this thread, but you need the number of results from your query, the number of results you want to display per page and the offset - after which result you want to start. for example you have 85 results and want to show 30 results on each page, then you need a function pagination($total_results, $offset, $length) and call it like pagination(85, 0, 30) for page 1, pagination(85, 30, 30) for page2 etc. and you modify your sql query a bit, just add "LIMIT $offset, $length" at the end. then you pass $lenght and $offset to your result page in your url. and you have to mark the current page somehow (bold for example) and figure out how much pages you have with 85 results and 30/per page, guess you need php floor() here, quite simple math, nothing magic
this is a dumb question but above with the function i posted , does it spit out the list or do I still need to do a for each row as rows { echo .....
Hello, How would I alter this code for a script that is not using a database? I'm using an api feed, and an xml feed. How do I alter the results= line?