htetnainglynn Posted June 9, 2010 Share Posted June 9, 2010 Hi everyone, This time i'm really stucked. Please help me out. I have been doing Pagination for when user type in some keyword let's say "j" then I will list all username which includes "j". The following is my sql statement. It displays properly on the first page but not on the 2nd and the rest. SELECT * FROM users WHERE username LIKE '%$username%' if($pageno==1) { echo "<br />FIRST PREV"; } else { echo "<br /><a href='{$_SERVER['PHP_SELF']}?pageno=1'>FIRST</a>"; $prevpage = $pageno - 1; echo "<a href='{$_SERVER['PHP_SELF']}?pageno=$prevpage&searchThisUser=j'>PREV</a>"; echo "(Page $pageno of $lastpage)"; } if($pageno == $lastpage) { echo "<br /> NEXT LAST"; } else { $nextpage = $pageno + 1; echo "<a href='{$_SERVER['PHP_SELF']}?pageno=$nextpage&searchThisUser=j'>NEXT</a>"; echo "<a href='{$_SERVER['PHP_SELF']}?pageno=$lastpage&searchThisUser=j'>LAST</a>"; } } I copied above code on some tutorial and using straight in my source without any modification. I don't know why only page 1 can display correct result but not the rest. I thought it's something to do with GET method and I purposely handcode "searchThisUser=j" for testing purpose believing other pages would get the value "j". Any help would be appreciated. Output There are 6 users with their username includes "j" and I limit to 5 users per page. It displays properly 2 pages at the bottom and when I click next, it's not actually displaying the last one instead the first 5 users. Quote Link to comment Share on other sites More sharing options...
htetnainglynn Posted June 9, 2010 Author Share Posted June 9, 2010 I've found A solution for MY problem. I dunno what's wrong with my code so I get rid of function (that makes this pagination thing) and run the script from top to bottom without any function calls, and it works. I guess i was having some kinda problem with variable scope. If there is any common problems noob like me would encounter doing pagination, please share your experience here. I would love to hear from anyone who experienced problems doing pagination and come up with a solution. Meanwhile, allow me to leave this thread as "unsolved" and i'll mark it "solved" afterward. Looking forward to hearing more on this topic from exp programmers. cheers Quote Link to comment Share on other sites More sharing options...
JasonLewis Posted June 9, 2010 Share Posted June 9, 2010 You need to limit the amount of results being returned. Use MySQL LIMIT, you have from and length. Basically, you need to get the total results, calculate the total amount of pages there will be and then determine where to start from with your results. Here's some basic code with comments: $usersPerPage = 5; //The number of users to display per page. //Get the current page data. if(!isset($_GET['page'])){ $currentPage = 1; }else{ $currentPage = $_GET['page']; } $totalResults = mysql_num_rows(mysql_query("SELECT * FROM users WHERE username LIKE '%$username%'")); //We'll go with your number of results as 6 $totalPages = ceil($totalResults / $usersPerPage); //Get the total pages by dividing the total users by the users per page, then round up. //A little bit of error checking to make sure the current page isn't greater //then the totalPages and that is isn't less then 1. if($currentPage > $totalPages){ $currentPage = $totalPages; } if($currentPage < 1){ $currentPage = 1; } //Now figure out where to start the query from. //This is done by multiplying the usersPerPage by the currentPage //then subtracting the currentPage. $from = ($usersPerPage * $currentPage) - $usersPerPage; //Now we can run the query to limit the amount of users that show. $query = mysql_query("SELECT * FROM users WHERE username LIKE '%$username%' LIMIT $from, $usersPerPage"); Then you can build the previous and next buttons with the variables that you now have. Hope that makes sense. Quote Link to comment 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.