Thauwa Posted April 8, 2009 Share Posted April 8, 2009 mysql_connect("host", "user", "pass") or die(mysql_error()) ; mysql_select_db("database") or die(mysql_error()) ; //Retrieves data from MySQL $data = mysql_query("SELECT * FROM table") or die(mysql_error()); //Puts it into an array while($info = mysql_fetch_array( $data )) { echo "<a href=\"/view.php?id={$info['id']}\">{$info["name"]}</a> <br>"; echo "Description: ".$info['description'] . " <br><hr>"; } Could any one paginate this for me. It is too complicated for me. Please don't ban me for asking such a direct question, moderators, for I've gone nuts trying to paginate this simple echo. Thanks in advance. P.S. I've read many tuts on pagination and understood none Quote Link to comment https://forums.phpfreaks.com/topic/153142-how-could-i-paginate-this/ Share on other sites More sharing options...
DeanWhitehouse Posted April 8, 2009 Share Posted April 8, 2009 Have you tried the phpfreaks tutorial on it? Quote Link to comment https://forums.phpfreaks.com/topic/153142-how-could-i-paginate-this/#findComment-804402 Share on other sites More sharing options...
Yesideez Posted April 8, 2009 Share Posted April 8, 2009 First you'll need to find out how many rows the query will return in total. Without this number you can't figure out how many pages you'll have. $numRows=mysql_num_rows($query); Next we need a page size $pageSize=10; Let's set it to 10 items per page Let's find out how many pages we're playing with $pageCount=floor($numRows/$pageSize) See if we've got any spilling over to the last page $lastCount=$numRows % $pageSize; Now let's have a starting page number: $pageNum=1; Now we can build the query $query=mysql_query("SELECT * FROM table LIMIT ".($pageNum-1).",".$pageSize); How's that for starters? Quote Link to comment https://forums.phpfreaks.com/topic/153142-how-could-i-paginate-this/#findComment-804404 Share on other sites More sharing options...
Thauwa Posted April 8, 2009 Author Share Posted April 8, 2009 THanks. Then I must start echoing, after the $query? Quote Link to comment https://forums.phpfreaks.com/topic/153142-how-could-i-paginate-this/#findComment-804409 Share on other sites More sharing options...
Yesideez Posted April 8, 2009 Share Posted April 8, 2009 Yes as it's a standard SELECT query and it'll be pulling data from the table(s). You'll also need to add a "next" and "previous" link to go back and forward a page and after that a sort of index allowing you to jump straight to a page. Quote Link to comment https://forums.phpfreaks.com/topic/153142-how-could-i-paginate-this/#findComment-804411 Share on other sites More sharing options...
Thauwa Posted April 8, 2009 Author Share Posted April 8, 2009 I appreciate you helping me in many of my posts. Thanks. In a tutorial, I got a code like this, $pages_query = ''; if ($sort != 0) { $pages_query .= '&sort='.$sort; } if ($artist) { $pages_query .= '&artist='.urlencode($artist); } if ($a_match) { $pages_query .= '&a_match='.urlencode($a_match); } // $pages_link is for page numbers only! $pages_link = 'index.php?pageno={page_link}'.$pages_query; // Fourth parameter is how many digits on each side of the current page number will be printed $page_numbers_formatted = quick_page_numbers(0, $pageno, $pages, 5, $pages_link)."\n"; but it was too complex. Ummm...I feel ashamed of myself........... **how do I create the next, prev, links.. :'( Answer it only if you want to. Quote Link to comment https://forums.phpfreaks.com/topic/153142-how-could-i-paginate-this/#findComment-804415 Share on other sites More sharing options...
Yesideez Posted April 8, 2009 Share Posted April 8, 2009 Why feel ashamed? This is the place to come for if you need help. My last question for help was last night with something REALLY simple. Let's say you're on page 1 - the first page. Now we can't have a previous link because that'll put us onto a non-existant page of 0. Only if we've got enough data returned from the table can we have a next link also. We've set the size of each page to 10 lines so if we've got more than 10 lines of data being returned we can use a next link. Quote Link to comment https://forums.phpfreaks.com/topic/153142-how-could-i-paginate-this/#findComment-804418 Share on other sites More sharing options...
Thauwa Posted April 8, 2009 Author Share Posted April 8, 2009 Thank you. You are encouraging! If I create a link for next saying $pageNum+1 for the URL and then have the data read out as usual, am I right? Quote Link to comment https://forums.phpfreaks.com/topic/153142-how-could-i-paginate-this/#findComment-804422 Share on other sites More sharing options...
Yesideez Posted April 8, 2009 Share Posted April 8, 2009 The links for next/previous would be pointing back to the page - remember we're on page 1. <a href="showdata.php?page=<?php echo $pageNum-1; ?>">Previous</a> Quote Link to comment https://forums.phpfreaks.com/topic/153142-how-could-i-paginate-this/#findComment-804426 Share on other sites More sharing options...
Thauwa Posted April 8, 2009 Author Share Posted April 8, 2009 Thanks. Pagination is now clearly more clearer to me than it was before. Quote Link to comment https://forums.phpfreaks.com/topic/153142-how-could-i-paginate-this/#findComment-804429 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.