problem with links in pagination

Reply

Join Date: Apr 2008
Posts: 35
Reputation: cali_dotcom is an unknown quantity at this point 
Solved Threads: 0
cali_dotcom cali_dotcom is offline Offline
Light Poster

problem with links in pagination

 
0
  #1
Oct 9th, 2008
hi, i just wrote a program for my website with a search function. everything works fine but i have a problem with the links to the other result pages. this is the code

  1.  
  2. <?php
  3.  
  4. $errors = array();
  5. if ($_POST)
  6. {
  7.  
  8. $mysql = mysql_connect("localhost", "root", "") or die("couldn't connect to server");
  9. $db = mysql_select_db("jobpage", $mysql) or die("couldn't connect to database");
  10.  
  11. $keyword = $_REQUEST["keyword"];
  12. $place = $_REQUEST["place"];
  13. $cat_id = $_REQUEST["cat_id"];
  14. $limit = $_REQUEST['limit'];
  15. $page = $_REQUEST['page'];
  16.  
  17. //set defaults for $limits, $page
  18. if (!($limit) || (is_numeric($limit) == "false") || ($limit < 10) || ($limit > 50)) {
  19. $limit = 10;
  20. }
  21.  
  22. if (!($page) || (is_numeric($page) == "false") || ($page > $numrows) || ($page <= 0)) {
  23. $page = 1;
  24. }
  25.  
  26. // set the lower limit where the records would start to display
  27.  
  28. $start_limit = $page * $limit - $limit;
  29. //build search query first against only the keyword--
  30. $search_db = "SELECT * FROM jobs WHERE job_desc like \"%$keyword%\"";
  31. $search_res = mysql_query($search_db) or die(mysql_error());
  32.  
  33. $numrows = mysql_num_rows($search_res);
  34. if ($numrows == 0) die ("no matches met your criteria");
  35.  
  36. // set the total number of pages required
  37. $total_pages = ceil($numrows/$limit);
  38.  
  39. $search_db = "SELECT * FROM jobs WHERE job_desc like \"%$keyword%\" LIMIT $start_limit, $limit";
  40. $search_res = mysql_query($search_db) or die(mysql_error());
  41.  
  42. // display what the person searched for
  43. $display .=" <p>You searched for: &quot;" . $keyword . "&quot;</p>";
  44.  
  45. // begin to show results set
  46. $b = $start_limit + $limit;
  47. $start_limit++;
  48. if ($numrows < $limit){
  49. $display .= "<p>Showing results $start_limit to $numrows of $numrows</p>";
  50. }
  51. else{
  52. $display .= "<p>Showing results $start_limit to $b of $numrows</p>";
  53.  
  54. }
  55. //first build the top part of the table
  56. $display .= "<TABLE BORDER = '1'>";
  57. $display .= "<TR>";
  58. $display .= "<TH>Job Title</TH><TH>Company</TH><TH>Description</TH><TH>Pay</TH><TH>Location</TH><TH>Date</TH>";
  59. $display .= "</TR>";
  60. // now you can display the results returned
  61. while ($row = mysql_fetch_array($search_res))
  62. {
  63. $title = $row["job_title"];
  64. $desc = $row["job_desc"];
  65. $qua = $row["job_qua"];
  66. $pay = $row["job_pay"];
  67. $req = $row["job_req"];
  68. $date = $row["post_date"];
  69. $comp = $row["comp_name"];
  70. $loc = $row["job_loc"];
  71.  
  72.  
  73. $display .= "<TR>";
  74. $display .= "<TD> $title</TD><TD> $comp </TD><TD> $desc </TD><TD> $pay </TD><TD> $loc </TD><TD> $date </TD>";
  75. $display .= "</TR>";
  76. //$count++;// total number of records shown which will be the set_limit, starting point of our limit clause
  77. }
  78. $display .= "</TABLE>";
  79.  
  80. $previous_page = $page -1;
  81. if ($previous_page >=1){
  82. $display .= "<b>&lt;&lt;</b> <a href=".$_SERVER["PHP_SELF"]."?keyword=$keyword&amp;limit=$limit&amp;page=$prev_page><b>Prev.</b></a>";
  83.  
  84. }
  85.  
  86. //loop through other pages
  87. for ($a = 1; $a <= $total_pages; $a++){
  88. if ($a == $page){
  89. $display .= "<b>$a | </b>";
  90. }
  91. else {
  92. $display .= "<a href='".$_SERVER["PHP_SELF"]."?keyword=$keyword&amp;limit=$limit&amp;page=$a'> $a </a> |";
  93. }
  94. }
  95.  
  96.  
  97.  
  98. $next_page = $page +1;
  99. if ($next_page <=$total_pages){
  100. $display .= "<a href=".$_SERVER["PHP_SELF"]."?keyword=$keyword&amp;limit=$limit&amp;page=$next_page><b>Next</b></a> &gt; &gt;";
  101. }
  102.  
  103. }
  104. else {
  105. header("location: sbd.html");
  106. }
  107.  
  108.  
  109. ?>

note: one lines 82, 92 and 100 where i put the links to the other pages, i also tried: href='job_search.php'?keyword.......
the idea is that when one clicks on the links, a new query would be built so the variables required for the query are passed in the link.

ALSO: does anyone know how i can modify this code to show details when a user click on the results

if anybody knows what's wrong, please help me out
thanks
Last edited by cali_dotcom; Oct 9th, 2008 at 12:18 am.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 35
Reputation: cali_dotcom is an unknown quantity at this point 
Solved Threads: 0
cali_dotcom cali_dotcom is offline Offline
Light Poster

Re: problem with links in pagination

 
0
  #2
Oct 9th, 2008
the problem is with the links on lines 82, 92 and 100.
the links do appear but when they are clicked on, the variables are not passed on to the script, hence i get a "page not found" page instead of a new query to be built and the script be run again.
i really dont know what's wrong, i'm losing my mind on this.
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 849
Reputation: R0bb0b is on a distinguished road 
Solved Threads: 67
R0bb0b's Avatar
R0bb0b R0bb0b is offline Offline
Practically a Posting Shark

Re: problem with links in pagination

 
0
  #3
Oct 9th, 2008
Originally Posted by cali_dotcom View Post
the problem is with the links on lines 82, 92 and 100.
the links do appear but when they are clicked on, the variables are not passed on to the script, hence i get a "page not found" page instead of a new query to be built and the script be run again.
i really dont know what's wrong, i'm losing my mind on this.
I'm pretty sure you need to replace "&amp;" with "&" in the links
“Be who you are and say what you feel because those who mind don't matter and those who matter don't mind.” - Dr. Seuss

-- The documentation is inevitable, you may get away with it for a little while but eventually you too will have to do the deed.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 35
Reputation: cali_dotcom is an unknown quantity at this point 
Solved Threads: 0
cali_dotcom cali_dotcom is offline Offline
Light Poster

Re: problem with links in pagination

 
0
  #4
Oct 9th, 2008
Originally Posted by R0bb0b View Post
I'm pretty sure you need to replace "&amp;" with "&" in the links
i did just that and now i have a different problem. when you click on the prev, next or the other page links, nothing happens(the page still shows the first result page) although you can see that the url has changed and the page number in the url is now different. that means to me that a new query is not being run. do you know what the problem could be?
here is the new code:

  1. <?php
  2.  
  3.  
  4.  
  5. $mysql = mysql_connect("localhost", "root", "") or die("couldn't connect to server");
  6. $db = mysql_select_db("jobpage", $mysql) or die("couldn't connect to database");
  7.  
  8. $keyword = $_GET["keyword"];
  9. $place = $_REQUEST["place"];
  10. $cat_id = $_REQUEST["cat_id"];
  11. $limit = $_GET["limit"];
  12. $page = $_GET["page"];
  13.  
  14. //set defaults for $limits, $page
  15. if (!($limit) || (is_numeric($limit) == "false") || ($limit < 10) || ($limit > 50)) {
  16. $limit = 10;
  17. }
  18.  
  19. if (!($page) || (is_numeric($page) == "false") || ($page > $numrows) || ($page <= 0)) {
  20. $page = 1;
  21. }
  22.  
  23. // set the lower limit where the records would start to display
  24.  
  25. $start_limit = ($page * $limit) - $limit;
  26.  
  27.  
  28. //build search query first against only the keyword--
  29. $search_db = "SELECT * FROM jobs WHERE job_desc like \"%$keyword%\"";
  30. $search_res = mysql_query($search_db) or die(mysql_error());
  31.  
  32. //get all columns
  33.  
  34.  
  35.  
  36. $numrows = mysql_num_rows($search_res);
  37. if ($numrows == 0) die ("no matches met your criteria");
  38.  
  39. // set the total number of pages required
  40. $total_pages = ceil($numrows/$limit);
  41.  
  42.  
  43.  
  44. $db = "SELECT * FROM jobs WHERE job_desc like \"%$keyword%\" LIMIT $start_limit, $limit";
  45. $res = mysql_query($db) or die(mysql_error());
  46.  
  47. //get the number of rows to be displayed
  48. $numrows1 = mysql_num_rows($res);
  49.  
  50.  
  51. // display what the person searched for
  52. $display .=" <p>You searched for: &quot;" . $keyword . "&quot;</p>";
  53.  
  54. // begin to show results set
  55. $b = $start_limit + $limit;
  56. $start_limit= $start_limit + 1;
  57. if ($numrows1 < $limit){
  58. $display .= "<p>Showing results $start_limit to $numrows1 of $numrows</p>";
  59. }
  60. else{
  61. $display .= "<p>Showing results $start_limit to $b of $numrows</p>";
  62.  
  63. }
  64.  
  65.  
  66.  
  67.  
  68. //first build the top part of the table
  69. $display .= "<TABLE BORDER = '1'>";
  70. $display .= "<TR>";
  71. $display .= "<TH>Job Title</TH><TH>Company</TH><TH>Description</TH><TH>Pay</TH><TH>Location</TH><TH>Date</TH>";
  72. $display .= "</TR>";
  73. // now you can display the results returned
  74. while ($row = mysql_fetch_array($res))
  75. {
  76. $title = $row["job_title"];
  77. $desc = $row["job_desc"];
  78. $qua = $row["job_qua"];
  79. $pay = $row["job_pay"];
  80. $req = $row["job_req"];
  81. $date = $row["post_date"];
  82. $comp = $row["comp_name"];
  83. $loc = $row["job_loc"];
  84.  
  85.  
  86. $display .= "<TR>";
  87. $display .= "<TD> $title</TD><TD> $comp </TD><TD> $desc </TD><TD> $pay </TD><TD> $loc </TD><TD> $date </TD>";
  88. $display .= "</TR>";
  89. //$count++;// total number of records shown which will be the set_limit, starting point of our limit clause
  90. }
  91. $display .= "</TABLE>";
  92.  
  93. $prev_page = $page -1;
  94. if ($prev_page >=1){
  95. $display .= '<b>&laquo;</b> <a href="'.$_SERVER["PHP_SELF"].'?keyword='.$keyword.'&limit='.$limit.'&page='.$prev_page.'"><b>Prev.</b></a>';
  96.  
  97. }
  98.  
  99. //loop through other pages
  100. for ($a = 1; $a <= $total_pages; $a++){
  101. if ($a == $page){
  102. $display .= "<b>$a | </b>";
  103. }
  104. else {
  105. $display .= '<a href="'.$_SERVER["PHP_SELF"].'?keyword='.$keyword.'&limit='.$limit.'&page='.$a.'"><b>'.$a.'</b></a> |';
  106. }
  107. }
  108.  
  109.  
  110.  
  111. $next_page = $page +1;
  112. if ($next_page <=$total_pages){
  113. $display .= '<a href="'.$_SERVER["PHP_SELF"].'?keyword='.$keyword.'&limit='.$limit.'&page='.$next_page.'"><b>Next</b></a>&gt; &gt;';
  114.  
  115. }
  116.  
  117.  
  118.  
  119.  
  120.  
  121.  
  122.  
  123.  
  124. ?>
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 849
Reputation: R0bb0b is on a distinguished road 
Solved Threads: 67
R0bb0b's Avatar
R0bb0b R0bb0b is offline Offline
Practically a Posting Shark

Re: problem with links in pagination

 
0
  #5
Oct 9th, 2008
I think $numrows around line 22 when you are checking the value of $page is not defined so the value of $page will always be 1.
“Be who you are and say what you feel because those who mind don't matter and those who matter don't mind.” - Dr. Seuss

-- The documentation is inevitable, you may get away with it for a little while but eventually you too will have to do the deed.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 35
Reputation: cali_dotcom is an unknown quantity at this point 
Solved Threads: 0
cali_dotcom cali_dotcom is offline Offline
Light Poster

Re: problem with links in pagination

 
0
  #6
Oct 11th, 2008
Originally Posted by R0bb0b View Post
I think $numrows around line 22 when you are checking the value of $page is not defined so the value of $page will always be 1.

thanks rob,
the problem was $page and $limit were always set to the default of 1 an 10 respectively because the conditions is_numeric($page) and is_numeric($limit) was always returning false. i put this condition in just incase someone typed in the values in the url as security measure. as soon as i took them out all was fine. do you know any way i could block values that have been type directly into the url.

thanks..
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC