Hightower Posted February 11, 2009 Posted February 11, 2009 Can anybody point me in the direction of a VERY easy to implement PHP Paginating script? Many thanks
localzuk Posted February 11, 2009 Posted February 11, 2009 What are you paginating? MySQL? LDAP? MSSQL? etc...
lightinthedark Posted February 11, 2009 Posted February 11, 2009 If it's MySQL you're taking your data from, the LIMIT clause may be very useful to you. $perPage = 20; $pageNum = (int)$_GET['pagenum']; if( $pagenum < 0 ) { $pageNum = 0; } $from = $pageNum * $perPage; $query = 'SELECT * FROM my_table LIMIT '.$from.', '.$perPage; // mysql connection stuffs... $data = mysql_query($query); echo '</pre><table>'; while( $row = mysql_fetch_assoc($data) ) { echo ''; var_dump($row); echo ''; } echo '</table>';<br>?&g The while loop is pretty generic and won't make things nice, but you (hopefully) get the idea. Hello :-Dave As always, code is untested and has no guarantees of any kind.
Hightower Posted February 11, 2009 Author Posted February 11, 2009 PHP Pagination Now now webman, no need to be sarcy. Yes, I did try Google but was posting here to get recommendations
tsky Posted February 11, 2009 Posted February 11, 2009 If you're grabbing from MySQL or suchlike, use the limit function of SQL... easiest and simplest way!
Hightower Posted February 12, 2009 Author Posted February 12, 2009 If you're grabbing from MySQL or suchlike, use the limit function of SQL... easiest and simplest way! I am using MySQL - do you have this in an example.
powdarrmonkey Posted February 12, 2009 Posted February 12, 2009 SELECT * FROM table WHERE thing=true LIMIT 10; gets the first ten records returned only SELECT * FROM table WHERE thing=true LIMIT 10, 10; gets ten records from row eleven onwards (note: add an ORDER BY if you want to be able to predict what you'll be getting).
tsky Posted February 12, 2009 Posted February 12, 2009 I am using MySQL - do you have this in an example. you can always create a chunk of php code around your main loop to return pages of information - e.g. off the top of my head (from things I've done in the past): ---------------------------------- $sql = "SELECT * FROM mytable "; if(($pageStart == "")||($pageStart < 0)){ $pageStart = 0; } $sql .= "LIMIT $pageStart,50"; $result = @mysql_query($sql); while($resultArray = @mysql_fetch_array($result)){ // Grab a new row of data // Do your grab / display routine here $pageStart++; } // To create a google-esque page list $sql = "SELECT * FROM tablename"; $PageNum = 0; // Set First Page $numPages = 0; // Set zero number of pages to display $result = @mysql_query($sql); $numresults = @mysql_num_rows($result); while($numPages <= $numresults){ print " $PageNum "; $PageNum++; $numPages = $numPages + 50; } ------------------------------ This will create a page with fifty entries per page, if you enter the page with no starting point it will display from the beginning... hope that helps a bit - PM me if you need a bit more info.
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now