Connecting Tech Pros Worldwide Forums | Help | Site Map

ODBC SQL PHP pagination query

Newbie
 
Join Date: Nov 2009
Posts: 2
#1: 3 Weeks Ago
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. ?>

code green's Avatar
Expert
 
Join Date: Mar 2007
Location: England
Posts: 1,083
#2: 3 Weeks Ago

re: ODBC SQL PHP pagination query


Quote:
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.
Newbie
 
Join Date: Nov 2009
Posts: 2
#3: 3 Weeks Ago

re: ODBC SQL PHP pagination query


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. ?>
dlite922's Avatar
Expert
 
Join Date: Dec 2007
Location: Moon, Dark Side
Posts: 1,095
#4: 3 Weeks Ago

re: ODBC SQL PHP pagination query


Please use [code] tags around your code.
code green's Avatar
Expert
 
Join Date: Mar 2007
Location: England
Posts: 1,083
#5: 3 Weeks Ago

re: ODBC SQL PHP pagination query


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.
Reply