473,387 Members | 1,455 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,387 software developers and data experts.

ODBC SQL PHP pagination query

PAGINATION QUESTION
This code displays the same 5 results from the database. How do I get the page to reference the next or previous group of 5 results? I am using ODBC, SQL, and PHP. LIMIT won't work in ODBC. The table name is called plastic with two column names: ename and iname. I am also not sure about line 9. Your help would be greatly appreciated.

CODE:

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $db = odbc_connect('','','');  //your connection here
  3. function inv_rows($r1)  {
  4. ob_start(); 
  5. (int)$number=odbc_result_all($r1);
  6. ob_clean(); 
  7. return $number;
  8. }
  9. $page = isset($_GET["page"]) ? $_GET["page"] : 1;  //not sure of "page" text
  10. if(empty($page)){$page = 1; }               
  11. $query = "SELECT * FROM plastic"; // name of table is plastic with columns ename and iname.       
  12. $num_result = odbc_exec($db, $query);
  13. $numrows = inv_rows($num_result);               
  14. echo '<p>There are '.$numrows.' ideas.</p>';           
  15. $limit = 5;
  16. $limitvalue = $page * $limit - ($limit);
  17. $limitnew = $limitvalue + $limit;
  18. $sql = "SELECT * from (SELECT TOP 5 * FROM (SELECT TOP 5 * FROM plastic ORDER BY ename, iname DESC) as table1 ORDER BY ename, iname DESC) as table2 ORDER BY ename, iname ASC"; 
  19. $result = odbc_exec($db, $sql);
  20. while(odbc_fetch_row($result)){               
  21. ?>
  22. <table style="width: 600;">    
  23. <tr>
  24. <td style="width: 300; height: 25px;">Name:</td>
  25. <td style="width: 300; height: 25px;">Idea Name:</td>
  26. </tr>                 
  27. <tr>
  28. <td style="width: 300; height: 25px;"><?php echo odbc_result($result, "ename"); ?></td>
  29. <td style="width: 300; height: 25px;"><?php echo odbc_result($result, "iname"); ?></td>
  30. </tr>
  31. <tr>
  32. <td colspan="5" style="height: 25px"><hr/></td>
  33. </tr>
  34. </table>
  35. <?php //PREVIOUS AND NEXT
  36. }
  37. if($page !=1){
  38. $pageprev = $page - 1;
  39. echo "&nbsp;<strong><a href='?page=$pageprev'>PREV</a></strong>&nbsp;"; }
  40. else{ echo "&nbsp;PREV&nbsp;"; }
  41. $numofpages = $numrows / $limit;
  42. for($i = 1; $i <= $numofpages; ++$i){
  43. if($i == $page){ echo "&nbsp;[$i]&nbsp;"; }
  44. else{ echo "&nbsp;<strong><a href='?page=$i'>$i</a></strong>&nbsp;"; }
  45. }
  46. if(($numrows % $limit) != 0){
  47. if($i == $page){ echo "&nbsp;[$i]&nbsp;"; }
  48. else{ echo "&nbsp;<strong><a href='?page=$i'>$i</strong></b>&nbsp;"; }
  49. }
  50. if(($numrows - ($limit * $page)) > 0){
  51. $pagenext = $page + 1;
  52. echo "&nbsp;<strong><a href='?page=$pagenext'>NEXT</a></strong>&nbsp;"; }
  53. else{ echo "&nbsp;NEXT&nbsp;"; }
  54. odbc_free_result($result);            
  55. exit;
  56. ?>
Nov 3 '09 #1
4 9493
code green
1,726 Expert 1GB
I am using ODBC, SQL, and PHP. LIMIT won't work in ODBC.
ODBC is a connectivity layer between an application and a DBMS, not a language.
You are rightly using TOP if it is Access or T-SQL.

Your code seems to have lost its way a little, but the trick in pagination is in the query, the PHP and HTML
Your php needs to store the current highest id value (and lowest) in a variable say $highid and pass that as a URL variable via the NEXT or PREV hyperlink to the next page,
The next page runs the query based on the $highid or $lowid value
Expand|Select|Wrap|Line Numbers
  1. "SELECT TOP 5 * FROM tbl WHERE id> $highid
  2. ORDER BY id"
The next page can of course be the same PHP file but the results will be different.
Nov 4 '09 #2
Thank you for your response.
My table name is plastic, with columns plastic_ID, ename, and iname.
With the code below, the result is seeing all entries (not 5 at a time as desired) and the pagination links do not link to the NEXT or PREV pages.
I appreciate your response and help.
Here is my updated code.
Expand|Select|Wrap|Line Numbers
  1. <?php 
  2. $db = odbc_connect('','','');  //put your connection here 
  3. function inv_rows($r1)  { 
  4. ob_start(); 
  5. (int)$number=odbc_result_all($r1); 
  6. ob_clean(); 
  7. return $number; 
  8. $page = isset($_POST["page"]) ? $_POST["page"] : 1;
  9. if(empty($page)){$page = 1; } 
  10. $query = "SELECT * FROM plastic"; // name of table is plastic with columns plastic_ID, ename, and iname.            
  11. $num_result = odbc_exec($db, $query); 
  12. $numrows = inv_rows($num_result);                    
  13. echo '<p>There are '.$numrows.' ideas.</p>'; 
  14. $limit = 5; 
  15. $limitvalue = $page * $limit - ($limit); 
  16. $limitnew = $limitvalue + $limit;
  17. if (isset($_POST['submit'])) {
  18. switch ($_POST['direction']) {
  19. case 'next':
  20. $query = "SELECT TOP 5 * FROM plastic WHERE plastic_ID > " . $_SESSION['lastid'] . " ORDER BY plastic_ID";
  21. break;
  22. case 'prev':
  23. $query = "SELECT * FROM (SELECT TOP 5 * FROM plastic WHERE plastic_ID < " . $_SESSION['firstid'] . " ORDER BY plastic_ID DESC) AS t ORDER BY plastic_ID ASC";
  24. break;
  25. default:
  26. $query = "SELECT TOP 5 * FROM plastic ORDER BY plastic_ID";
  27. }     
  28. }
  29. $result = odbc_exec($db, $query); 
  30. while(odbc_fetch_row($result)){                
  31. ?> 
  32. <table style="width: 600;">     
  33. <tr> 
  34. <td style="width: 300; height: 25px;">Name:</td> 
  35. <td style="width: 300; height: 25px;">Idea Name:</td> 
  36. </tr>                 
  37. <tr> 
  38. <td style="width: 300; height: 25px;"><?php echo odbc_result($result, "ename"); ?></td> 
  39. <td style="width: 300; height: 25px;"><?php echo odbc_result($result, "iname"); ?></td> 
  40. </tr> 
  41. <tr> 
  42. <td colspan="5" style="height: 25px"><hr/></td> 
  43. </tr> 
  44. </table> 
  45. <?php //PREVIOUS AND NEXT BUTTONS 
  46. if($page !=1){ 
  47. $pageprev = $page - 1; 
  48. echo "&nbsp;<strong><a href='?page=$pageprev'>PREV</a></strong>&nbsp;"; } 
  49. else{ echo "&nbsp;PREV&nbsp;"; } 
  50. $numofpages = $numrows / $limit; 
  51. for($i = 1; $i <= $numofpages; ++$i){ 
  52. if($i == $page){ echo "&nbsp;[$i]&nbsp;"; } 
  53. else{ echo "&nbsp;<strong><a href='?page=$i'>$i</a></strong>&nbsp;"; } 
  54. if(($numrows % $limit) != 0){ 
  55. if($i == $page){ echo "&nbsp;[$i]&nbsp;"; } 
  56. else{ echo "&nbsp;<strong><a href='?page=$i'>$i</strong></b>&nbsp;"; } 
  57. if(($numrows - ($limit * $page)) > 0){ 
  58. $pagenext = $page + 1; 
  59. echo "&nbsp;<strong><a href='?page=$pagenext'>NEXT</a></strong>&nbsp;"; } 
  60. else{ echo "&nbsp;NEXT&nbsp;"; } 
  61. odbc_free_result($result);             
  62. exit; 
  63. ?>
Nov 4 '09 #3
dlite922
1,584 Expert 1GB
Please use [code] tags around your code.
Nov 4 '09 #4
code green
1,726 Expert 1GB
I looks like you are not passing the variables via the href.
A bit of debugging code would be helpful (simply echo out variables to see what they contain) But
Expand|Select|Wrap|Line Numbers
  1. echo "&nbsp;<strong><a href='?page=$pageprev'>PREV</a></strong>&nbsp;"; } 
  2. else{ echo "&nbsp;PREV&nbsp;"; } 
  3.  
It looks like you have wrapped php variables in single quotes so they won't be parsed.
When echoing HTML I find it better to concatenate strings in single quotes and variables.
Expand|Select|Wrap|Line Numbers
  1. echo '&nbsp;<strong>
  2. <a href="?page='.$pageprev.'">PREV</a>
  3. </strong>&nbsp;' 
Looking again there is no URL at all, only a page variable.
You need a page for the link to go.
Nov 4 '09 #5

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

Similar topics

5
by: leegold2 | last post by:
Hi, I have been looking around for a way to paginate php/mysql table output. There are a lot of hits when I google it. But unfortunately what I have tried either does not work or is imho just...
11
by: ste | last post by:
Hi there, Further to my recent posts where I've received excellent help from Rik and Jerry, I've ended up with an image gallery on my website that displays images in a table, 3 images per row. ...
0
by: Sheau Wei | last post by:
I am using php version 4.3.3. The below was my pagination code. My problem is that when i run this code in my computer, i wall fail because of the hyperlink of the pagination not function when i...
3
by: assgar | last post by:
Hello I have a search page where you select the letter of the alphabet and the person's information who's name beginning with that letter is displayed. This function is placed on the web page...
1
by: assgar | last post by:
Hi I was using a schroll bar to display multiple rows of dynamically created from database records. The scrolling was not displaying the data properly so I have decided to use pagination. The...
3
sharijl
by: sharijl | last post by:
I see tutorials all over the web with pagination and MySQl but not much out there (that is updated using PHP 5 without global variables) on doing this using ODBC connection with MSSQL. This is...
1
by: Alec | last post by:
Newbie question. I have recently tried a php tutorial on pagination, but I just cannot get the links to work. See www.freeweekends.co.uk/pagtest2.php. The search finds over 60 results, but only...
4
by: ArizonaJohn | last post by:
Hello, The code below works great. The user enters a name into an HTML form, the code looks up a table with that name, and then that table is displayed. I am trying to use pagination with it,...
2
by: kkshansid | last post by:
this is my search page on which i am getting two parameters from previous page but the problem is that as soon as i click any other next pages my sql query fails as it doesnt get these two parameters...
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: 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: 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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.