rashmi_k28 Posted December 5, 2008 Share Posted December 5, 2008 $rowsperpage = 3; // find out total pages $totalpages = ceil($cnt / $rowsperpage); //Pagination if (isset($_GET['currentpage']) && is_numeric($_GET['currentpage'])) { // cast var as int $currentpage = (int) $_GET['currentpage']; } else { $currentpage = 1; } // end if // if current page is greater than total pages... if ($currentpage > $totalpages) { // set current page to last page $currentpage = $totalpages; } // end if // if current page is less than first page... if ($currentpage < 1) { // set current page to first page $currentpage = 1; } // end if // the offset of the list, based on current page $offset = ($currentpage - 1) * $rowsperpage; if ($currentpage > 1) { // show << link to go back to page 1 echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=1'>First</a> "; // get previous page num $prevpage = $currentpage - 1; // show < link to go back to 1 page echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$prevpage'>Previous</a> "; } // end if else { echo "First Previous"; } echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$x'>$x</a> "; // if not on last page, show forward and last page links if ($currentpage != $totalpages) { // get next page $nextpage = $currentpage + 1; echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$nextpage'>Next</a> "; echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$totalpages'>Last</a> "; }else { echo "Next Last"; } The pagination code is working and how to display names 3 per page Here how to display 3 names per page. foreach($names as $name){ echo "<td colspan=\"3\"><div align=\"center\" class=\"tduser\">$name</div></td>"; } Quote Link to comment https://forums.phpfreaks.com/topic/135625-display-name/ Share on other sites More sharing options...
gevans Posted December 5, 2008 Share Posted December 5, 2008 What seems to be the problem? what names do you want to display? You're question wasn't very well explained Quote Link to comment https://forums.phpfreaks.com/topic/135625-display-name/#findComment-706719 Share on other sites More sharing options...
rashmi_k28 Posted December 8, 2008 Author Share Posted December 8, 2008 $names is the array. foreach($names as $name){ echo "<td colspan=\"3\"><div align=\"center\" class=\"tduser\">$name</div></td>"; } In the above code I have to display only 3 names per page. When Next link of pagination is clicked, next 3 names should be displayed. At present all the names are displayed in the same page. So only 3 names should be displayed Quote Link to comment https://forums.phpfreaks.com/topic/135625-display-name/#findComment-709171 Share on other sites More sharing options...
chronister Posted December 8, 2008 Share Posted December 8, 2008 You would do that with the query that you get your results from. You would use a LIMIT 0,3 clause in your query to only get 3 results per page. But you have to change the LIMIT on each query to only get 3 rows, but get the 3 that correspond to what you want to show. Here is a snippet from a page I have that uses pagination. <?php $setlimit = 100; // number of results to show per page if(isset($_GET['page'])){ // if page passed in URL $page = $_GET['page']; //set $page to that }else{ // else its not set, so set page to 1 $page = 1; } $pages = ceil($total_entries / $setlimit); // num of pages we are going to have $offset = ($page - 1) * $setlimit; // calculate which records we want to start showing from $result = mysql_query("SELECT * FROM tracks ORDER BY artist,track LIMIT $offset, $setlimit "); /* our query uses $offset & $setlimit to determine which rows to start at and how many rows to return*/ ?> Hope this helps you.... Nate Quote Link to comment https://forums.phpfreaks.com/topic/135625-display-name/#findComment-709174 Share on other sites More sharing options...
rashmi_k28 Posted December 8, 2008 Author Share Posted December 8, 2008 But all the mysql queries is wriiten in the separate function and called in the page. So offset and limit cannot be used. I have to find some way how to display the 3 names in single page Quote Link to comment https://forums.phpfreaks.com/topic/135625-display-name/#findComment-709208 Share on other sites More sharing options...
chronister Posted December 8, 2008 Share Posted December 8, 2008 But all the mysql queries is wriiten in the separate function and called in the page. So offset and limit cannot be used. I have to find some way how to display the 3 names in single page Well, then your going to have to grab all the results in the table, and with your code determine what needs to be shown and then limit it to that. Good luck, it will be challenging. Your code already has the 2 pieces, but 1 has a different name. $rowsperpage is the same thing as my $setlimit, and $offset is determined in the exact same manner as my $offset <?php $offset = ($page - 1) * $setlimit; // mine $offset = ($currentpage - 1) * $rowsperpage; // yours ?> The only difference is what we are calling the things, I call it $page where you call it $currentpage, I call it $setlimit where you call it $rowsperpage. Other than that, these 2 snippets do EXACTLY the same thing. There should be no reason at all you can't take the query that gets the data and add the LIMIT clause to it. You would simply add LIMIT $offset, $rowsperpage to the end of it. Post the code of the function that does the work here and gets the data. We can make it work for you. Post the part that gives you the $cnt variable too. Seriously, doing it in mysql is going to be a LOT simpler than trying to do it in the PHP. Plus, your page is already set up exactly how it should be for pagination. Nate Quote Link to comment https://forums.phpfreaks.com/topic/135625-display-name/#findComment-709245 Share on other sites More sharing options...
sasa Posted December 8, 2008 Share Posted December 8, 2008 try <?php $names = array_chunk($names, $rowsperpage); $names = $names[$currentpage]; //add your code here ?> but better is using limit in query Quote Link to comment https://forums.phpfreaks.com/topic/135625-display-name/#findComment-709394 Share on other sites More sharing options...
sasa Posted December 8, 2008 Share Posted December 8, 2008 ups <?php $names = array_chunk($names, $rowsperpage); $names = $names[$currentpage - 1]; //add your code here ?> Quote Link to comment https://forums.phpfreaks.com/topic/135625-display-name/#findComment-709444 Share on other sites More sharing options...
rashmi_k28 Posted December 12, 2008 Author Share Posted December 12, 2008 foreach($names as $name){ foreach($head as $heads){ echo "<td class=\"run\">".$details[$heads][$name]['R']."</td>"; } } How to display the array $details[$heads][$name]['R'] 3 per page. Quote Link to comment https://forums.phpfreaks.com/topic/135625-display-name/#findComment-713450 Share on other sites More sharing options...
chronister Posted December 12, 2008 Share Posted December 12, 2008 I already told you, you page is set up for pagination so all you need to do is pass in the $offset, $limit part into your query and things will be pretty well good to go. If you insist on doing it the hard way and try to keep track of what your supposed to display using the php, then have fun. Here is the basics of what you need to figure out. 1. determine what "page" is being displayed 2. determine what items need to be displayed on that "page" 3. get results, and set counter to keep track of which item is currently being looped through incrementing at the end each time 4. display or skip current item based on steps 1 & 2 5. learn how to do pagination through mysql because it is MUCH simpler. Those are the steps you will need to create code for. (minus number 5 of course). Have fun. If you wish to make your life much simpler, post the script that does the actual query and we can probably make it do pagination properly. Nate Quote Link to comment https://forums.phpfreaks.com/topic/135625-display-name/#findComment-713465 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.