almightyegg Posted October 22, 2006 Share Posted October 22, 2006 when pulling data out of a table using a while loop, how do you make it separate the data into different pages??like the first 10 rows is one page then it goes to a second page for 11-20 then third etc... Quote Link to comment https://forums.phpfreaks.com/topic/24770-pages-of-data/ Share on other sites More sharing options...
redbullmarky Posted October 22, 2006 Share Posted October 22, 2006 the best place to start is the mysql LIMIT clause, which you tag onto the end of your SELECT. it takes either 1 or 2 parameters - 1 if you just want to pick a certain number of rows, 2 if you want to pick a certain number of rows starting from a particular record - which is what you need for pagination. there's a tutorial right here on phpfreaks that describes the basics well: http://www.phpfreaks.com/tutorials/43/0.phpthis, for example, will pick 5 records starting from the very first record (0 signifies the first record, not 1):SELECT * FROM mytable LIMIT 0, 10 this would pick 5 rows on "page" 3:SELECT * FROM mytable LIMIT 20, 10pagination generally involves passing the page number in the URL and passing the resulting $_GET['page'] into your LIMIT clause.http://php.about.com/od/mysqlcommands/g/Limit_sql.htmhere's a little example pseudocode to set you on your way:[code]<?php// how many results per page?$num_per_page = 10;// what record shall we start at?$offset = ($_GET['page'] - 1) * $num_per_page;$query = "SELECT * FROM mytable LIMIT $offset, $num_per_page";... etc ...?><a href="/mypage.php?page=1">Page 1</a><a href="/mypage.php?page=2">Page 2</a><a href="/mypage.php?page=3">Page 3</a>etcetc[/code]cheersMark Quote Link to comment https://forums.phpfreaks.com/topic/24770-pages-of-data/#findComment-112790 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.