473,373 Members | 1,407 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,373 software developers and data experts.

paging php mssql using next prev button

1
hei, anyone! i need help to paging php using mssql, and php script in the program to make a next and previous button.. please sending message for me if you can answer my question
Dec 13 '06 #1
5 14581
b1randon
171 Expert 100+
hei, anyone! i need help to paging php using mssql, and php script in the program to make a next and previous button.. please sending message for me if you can answer my question
Why don't you post what you have so far? It would help. To get you started, paging is really easy if you use mysql's LIMIT keyword. You can limit from row x to row y. A query is really easy b/c you can dynamically generate a LIMIT statement with something like LIMIT (50*x), (50*(x+1)) where x is your page number. That syntax isn't right for sure but you get the idea. It will give you 50 results per page ( page 0 has results 0-50, 1 has 50-100 etc). To find the total number of pages just do a query for count(*) /50. Post some code and I can help further.
Dec 14 '06 #2
ronverdonk
4,258 Expert 4TB
When you google for terms like 'pagination results display' you will get many replies pointing to code snippets that can perform what you are looking for.

Ronald :cool:
Dec 24 '06 #3
Atli
5,058 Expert 4TB
Just a note...

The MySQL LIMIT clause doesnt work exectly like you say. The first parameter is the start index, the second is the number of rows to return:

... LIMIT start, ammount

So dynamicly createing a limit clause in php would go something like this:

Expand|Select|Wrap|Line Numbers
  1. "... LIMIT ". $pageNr * $numRows .", {$numRows}"
wich would give you (e.g. 50 rows per page at page 5)

Expand|Select|Wrap|Line Numbers
  1. "... LIMIT 250, 50"
Dec 26 '06 #4
Hi There,

I in fact got a paging system ready to use now. it works great. Maybe it is something you are looking for.

But i also got a question to improve it.

As a result, when i got about 13 pages to show it will show you the following:

[First Page][Prev][1][2][3][4][5][6][7][8][9][10][11][12][13][Next][Last Page]

But i would realy like it to show a limit of 10 pages at a time, so when i get to page 7 or at least further then page 2 it shows

[First Page][Prev][3][4][5][6][7][8][9][10][11][12][Next][Last Page]

Does anyone know how to do that? I'm not really a very good with php (and dutch;) so please do not get to technical on this.


Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3. $pageNum = 1; 
  4. if (empty($page)){
  5.          $page=1;
  6.      }
  7.      else{
  8.          $page = (int) $page;
  9.      }
  10.  
  11. if(isset($_GET['page'])) 
  12.     $pageNum = $_GET['page']; 
  13.  
  14. if(!isset($_GET['category_id'])){
  15.  
  16. }
  17. else{
  18.     $id = (int) $_GET['category_id'];
  19. }
  20.  
  21. $offset = ($pageNum - 1) * $rowsPerPage; 
  22.  
  23.  
  24.  
  25. $query  = "SELECT id FROM fotos WHERE session_id='$id' LIMIT $offset, $rowsPerPage"; 
  26. $result = mysql_query($query) or die('Error, query failed'); 
  27.  
  28.  
  29.  
  30.  
  31. // how many rows we have in database 
  32. $query   = "SELECT COUNT(id) AS numrows FROM fotos WHERE session_id='$id'"; 
  33. $result  = mysql_query($query) or die('Error, query failed'); 
  34. $row     = mysql_fetch_array($result, MYSQL_ASSOC); 
  35. $numrows = $row['numrows']; 
  36.  
  37. // how many pages we have when using paging? 
  38. $maxPage = ceil($numrows/$rowsPerPage); 
  39. $offset_prev = (($pageNum - 1) * $rowsPerPage)- $rowsPerPage;
  40. $offset_next = (($pageNum + 1) * $rowsPerPage)- $rowsPerPage;  
  41.  
  42.  
  43.  
  44. // print the link to access each page 
  45. $self = $_SERVER['PHP_SELF']; 
  46. $nav = ''; 
  47. for($page = 1; $page <= $maxPage; $page++) 
  48.     if ($page == $pageNum) 
  49.     { 
  50.         $nav .= " $page ";   // no need to create a link to current page 
  51.     } 
  52.     else 
  53.     { 
  54.     $offset_nav = ($page * $rowsPerPage) - $rowsPerPage;
  55.         $nav .= " <a href=\"$self?page=$page&category_id=$id&offset=$offset_nav\">$page</a> "; 
  56.     }         
  57.  
  58. // creating previous and next link 
  59. // plus the link to go straight to 
  60. // the first and last page 
  61.  
  62. if ($pageNum > 1) 
  63.     $page = $pageNum - 1;
  64.     $prev = " <a href=\"$self?page=$page&category_id=$id&offset=$offset_prev\">[Prev]</a> "; 
  65.  
  66.     $first = " <a href=\"$self?page=1&category_id=$id&offset=0\">[First Page]</a> "; 
  67. }  
  68. else 
  69.     $prev  = '&nbsp;'; // we're on page one, don't print previous link 
  70.     $first = '&nbsp;'; // nor the first page link 
  71.  
  72. if ($pageNum < $maxPage) 
  73.     $offset_max = ($maxPage * $rowsPerPage) - $rowsPerPage; 
  74.     $page = $pageNum + 1; 
  75.     $next = " <a href=\"$self?page=$page&category_id=$id&offset=$offset_next\">[Next]</a> "; 
  76.  
  77.     $last = " <a href=\"$self?page=$maxPage&category_id=$id&offset=$offset_max\">[Last Page]</a> "; 
  78. }  
  79. else 
  80.     $next = '&nbsp;'; // we're on the last page, don't print next link 
  81.     $last = '&nbsp;'; // nor the last page link 
  82.  
  83. // print the navigation link 
  84. echo $first . $prev . $nav . $next . $last; 
  85.  
  86.  
  87. // and close the database connection 
  88. include '../library/closedb.php'; 
  89. ?>
Dec 27 '06 #5
@cecep
okay just to add some note.. here

some dude from code igniter forum create a simple of function to make pagination....

i think this function will work great with all database. and you dont havta use code igniter framework.

im currently using php and mssql server 2005 with Code Igniter Framework
and it works well with the function...

just view it here http://codeigniter.com/forums/viewthread/64287/
and this is the query that i used :
Expand|Select|Wrap|Line Numbers
  1. function get_list($offset){
  2. $sql = "
  3. select top (20) from (
  4. select id,name, ROW_NUMBER() over (order by id) as Result_Number from tm_person) innerSel Where Result_Number >(($offset - 1) *20) Order by Result_Number
  5. )
  6. ";
  7. $results = $this->db->query($sql);
  8. return $results->result_array();
  9. }
Feb 19 '09 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: CharitiesOnline | last post by:
Hello, I have set this script up to add paging to a search results page. Which on the first page works fine. I calculates how many pages there should be depending on the number of results returned...
1
by: Dan Nash | last post by:
Hi guys Just developing a cool list that pulls data from an SQL database. I've got a nice header, a footer, borders down the side, but no top/bottom borders for the rows, and onmouseover /...
3
by: Darren | last post by:
Dear Groups I've used paging with a datagrid in C# at least a hundred times, but for whatever reason, I cannot get the prev /next to appear as links. It is absolutely certain that the number of...
1
by: ratnakarp | last post by:
Hi, I have a search text box. The user enters the value in the text box and click on enter button. In code behind on button click i'm writing the code to get the values from the database and...
4
by: karunakar rao | last post by:
Hi All I need some help. I have been populated 50,000 Records in asp.net C# datagrid. Ever page has 10 records Example "<< Prev 1 2 3 4 5 6 7 8 9 10 Next>> Iam showing like that in my...
2
by: saviomf | last post by:
Hi all, I have a problem with my paging script. The first time it executes, (say it gives 5 pages of records), it will display 1 of 5 but when i click on the next button, it gives 1 of 1 . Please can...
3
osward
by: osward | last post by:
Hi, everyone, I had managed to make use of the date link from a simple calendar script to my query table. When I click on the date's link or Prev and Next Month link, The table first row will be...
2
by: nse111 | last post by:
I am trying to get some data and show it usig paging. my coding displays and also the paging is also visible on my web page. but it doesnt work. as in when i click next the next set of results aren't...
16
by: pupilstuff | last post by:
hi guys i just want to perform custom paging in which at the footer of the grid view ,there must be a pager 'pervious/next with numeric' this is what i did in aspx page <asp:GridView...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.