Problem with page navigation

Thread Solved

Join Date: May 2008
Posts: 90
Reputation: Suhacini is an unknown quantity at this point 
Solved Threads: 0
Suhacini Suhacini is offline Offline
Junior Poster in Training

Problem with page navigation

 
0
  #1
May 19th, 2008
I have problem with page navigation.My problem is that am getting all the records in one page. $rowsPerPage = 10; I have 30 records, all are showed in one page...not the 10 records.Here is my code.

  1. <?php
  2. include ('conn.php');
  3.  
  4. // how many rows to show per page
  5. $rowsPerPage = 10;
  6.  
  7. // by default we show first page
  8. $pageNum = 1;
  9.  
  10. // if $_GET['page'] defined, use it as page number
  11. if(isset($_GET['page']))
  12. {
  13. $pageNum = $_GET['page'];
  14. }
  15.  
  16.  
  17. // counting the offset
  18. $offset = ($pageNum - 1) * $rowsPerPage;
  19.  
  20. $q = "show tables";
  21. $r = mysql_query($q);
  22. //$searchresult = array();
  23. $var = $_POST['keyword'] ;
  24. $trimmed = trim($var);
  25.  
  26. // rows to return
  27. $limit=10;
  28.  
  29. // check for an empty string and display a message.
  30. if ($trimmed == "")
  31. {
  32. echo "<p>Please enter a search...</p>";
  33. exit;
  34. }
  35.  
  36. // check for a search parameter
  37. if (!isset($var))
  38. {
  39. echo "<p>We dont seem to have a search parameter!</p>";
  40. exit;
  41. }
  42.  
  43. while($data = mysql_fetch_array($r)) {
  44. $table = $data[0]; // get the tablename
  45. $query = "select * from ".$table." where dispname like '%".$trimmed."%'";
  46. $result = mysql_query($query);
  47. while($data = mysql_fetch_array($result)) {
  48. // $searchresult[] = $rows['name'];
  49. //$var =$rows['dispname'];
  50.  
  51. echo ' <tr> <td> <a href="'.$data['MedName'].'" >' . $data['dispname'].' </a> </td>';
  52. echo'</tr>';
  53.  
  54. }
  55. }
  56. //$query = "SELECT val FROM randoms LIMIT $offset, $rowsPerPage";
  57. //$result = mysql_query($query) or die('Error, query failed');
  58.  
  59. while(list($val) = mysql_fetch_array($result))
  60. {
  61. echo "$val <br>";
  62. }
  63.  
  64. echo '<br>';
  65.  
  66.  
  67. $query = "select * from ".$table." where dispname like '%".$trimmed."%'";
  68. $result = mysql_query($query) or die('Error, query failed');
  69. $row = mysql_fetch_array($result, MYSQL_ASSOC);
  70. $numrows = $row['numrows'];
  71.  
  72. $maxPage = ceil($numrows/$rowsPerPage);
  73.  
  74. $self = $_SERVER['PHP_SELF'];
  75.  
  76.  
  77.  
  78.  
  79. if ($pageNum > 1)
  80. {
  81. $page = $pageNum - 1;
  82. $prev = " <a href=\"$self?page=$page\">[Prev]</a> ";
  83.  
  84. $first = " <a href=\"$self?page=1\">[First Page]</a> ";
  85. }
  86. else
  87. {
  88. $prev = ' [Prev] '; // we're on page one, don't enable 'previous' link
  89. $first = ' [First Page] '; // nor 'first page' link
  90. }
  91.  
  92. if ($pageNum < $maxPage)
  93. {
  94. $page = $pageNum + 1;
  95. $next = " <a href=\"$self?page=$page\">[Next]</a> ";
  96. $last = " <a href=\"$self?page=$maxPage\">[Last Page]</a> ";
  97. }
  98. else
  99. {
  100. $next = ' [Next] '; // we're on the last page, don't enable 'next' link
  101. $last = ' [Last Page] '; // nor 'last page' link
  102. }
  103.  
  104.  
  105. echo $first . $prev . " Showing page <strong>$pageNum</strong> of <strong>$maxPage</strong> pages " . $next . $last;
  106.  
  107.  
  108. ?>
Suhasini
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 90
Reputation: Suhacini is an unknown quantity at this point 
Solved Threads: 0
Suhacini Suhacini is offline Offline
Junior Poster in Training

Re: Problem with page navigation

 
0
  #2
May 27th, 2008
I changed my code.Here is my new code:
  1. <?php
  2. include ('conn.php');
  3. // how many rows to show per page
  4. $rowsPerPage = 10;
  5. // by default we show first page
  6. $pageNum = 1;
  7. // if $_GET['page'] defined, use it as page number
  8. if(isset($_GET['page']))
  9. {
  10. $pageNum = $_GET['page'];
  11. }
  12. // counting the offset
  13. $offset = ($pageNum - 1) * $rowsPerPage;
  14. //$searchresult = array();
  15. $var = $_REQUEST['keyword'] ;
  16. $trimmed = trim($var);
  17. // rows to return
  18.  
  19.  
  20. // check for an empty string and display a message.
  21. if ($trimmed == "")
  22. {
  23. echo "<p>Please enter a search...</p>";
  24. exit;
  25. }
  26.  
  27. // check for a search parameter
  28. if (!isset($var))
  29. {
  30. echo "<p>We dont seem to have a search parameter!</p>";
  31. exit;
  32. }
  33.  
  34. //$table ="results";
  35. $query ="select count(*) from medlist where dispname like '%".$trimmed."%'";
  36. $rsid = mysql_query($query);
  37. if(!$rsid){die(mysql_error());}
  38. $resArr = mysql_fetch_row($rsid);
  39. $numrows = $resArr[0];
  40.  
  41. //$query ="select * from medlist where dispname like '%".$trimmed."%' limit $strpos, $rowsPerPage";
  42. $query = "select * from medlist where dispname like '%".$trimmed."%' LIMIT ". $start .", ".
  43. $rowsPerPage ;
  44. $result = mysql_query($query);
  45. if(!$result){die(mysql_error());}
  46. //$query = "SELECT val FROM randoms LIMIT $offset, $rowsPerPage";
  47. //$result = mysql_query($query) or die('Error, query failed');
  48.  
  49. while($val = mysql_fetch_row($result))
  50. {
  51. print_r($val);
  52. }
  53.  
  54. echo '<br>';
  55.  
  56.  
  57. $maxPage = ceil($numrows/$rowsPerPage);
  58.  
  59. $self = $_SERVER['PHP_SELF'];
  60. if ($pageNum > 1)
  61. {
  62. $page = $pageNum - 1;
  63. $prev = " <a href=\"$self?page=$page&keyword=$var\">[Prev]</a> ";
  64.  
  65. $first = " <a href=\"$self?page=1&keyword=$var\">[First Page]</a> ";
  66. }
  67. else
  68. {
  69. $prev = ' [Prev] '; // we're on page one, don't enable 'previous' link
  70. $first = ' [First Page] '; // nor 'first page' link
  71. }
  72.  
  73. if ($pageNum < $maxPage)
  74. {
  75. $page = $pageNum + 1;
  76. $next = " <a href=\"$self?page=$page&keyword=$var\">[Next]</a> ";
  77. $last = " <a href=\"$self?page=$maxPage&keyword=$var\">[Last Page]</a> ";
  78. }
  79. else
  80. {
  81. $next = ' [Next] '; // we're on the last page, don't enable 'next' link
  82. $last = ' [Last Page] '; // nor 'last page' link
  83. }
  84. echo $first . $prev . " Showing page <strong>$pageNum</strong> of <strong>$maxPage</strong> pages " . $next . $last;
  85. ?>
Error occured is :
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ' 10' at line 1.
This error is some where around this line:if(!$result){die(mysql_error());}
What is the solution?
Last edited by Suhacini; May 27th, 2008 at 2:54 am.
Suhasini
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 13
Reputation: hivenk is an unknown quantity at this point 
Solved Threads: 2
hivenk hivenk is offline Offline
Newbie Poster

Re: Problem with page navigation

 
0
  #3
May 27th, 2008
Actually in your code you have to put $offset instead of $start in your query

check once
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 90
Reputation: Suhacini is an unknown quantity at this point 
Solved Threads: 0
Suhacini Suhacini is offline Offline
Junior Poster in Training

Re: Problem with page navigation

 
0
  #4
May 27th, 2008
Actually I got this code from some where.I wanted to write my own code..Can anyone tell me how to start with that?
Suhasini
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 90
Reputation: Suhacini is an unknown quantity at this point 
Solved Threads: 0
Suhacini Suhacini is offline Offline
Junior Poster in Training

Re: Problem with page navigation

 
0
  #5
May 27th, 2008
From the above code I am not getting any error but,the link to next page is not enabled.Even though $limit=10; & $rowsPerPage =10; both are declared as '10'.If I have 15 records all of them are displayed in the same page,but not in the 2 different pages.
Anyone to help regarding this??
Last edited by Suhacini; May 27th, 2008 at 10:00 am.
Suhasini
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 90
Reputation: Suhacini is an unknown quantity at this point 
Solved Threads: 0
Suhacini Suhacini is offline Offline
Junior Poster in Training

Re: Problem with page navigation

 
0
  #6
Jun 3rd, 2008
Changed the code again.
  1. <?php
  2. include ('conn.php');
  3.  
  4. // by default we show first page
  5. $page = 1;
  6.  
  7. // if $_GET['page'] defined, use it as page number
  8. if(isset($_GET['page']))
  9. {
  10. $page = $_GET['page'];
  11. }
  12. // counting the offset
  13. $offset = ($page - 1) * $rowsPerPage;
  14.  
  15.  
  16. //$searchresult = array();
  17. $var = $_POST['keyword'] ;
  18. $trimmed = trim($var);
  19.  
  20. // rows to return
  21. $limit=10;
  22.  
  23. $str = "select * from medlist where dispname like '%".$trimmed."%'";
  24. echo $str.'<br>';
  25. $res= mysql_query($str);
  26. if(!$res){die(mysql_error());}
  27. $num_rows= mysql_num_rows($res);
  28. echo $num_rows.'<br>';
  29.  
  30. // how many rows to show per page
  31. $rowsPerPage =10;
  32. $maxPage = ceil($num_rows/$rowsPerPage);
  33. //$page=(isset($_GET['page']))?$_GET['page']:1;
  34. //$offset=($page-1)*$rowsPerPage;
  35.  
  36. $query = "select * from medlist where dispname like '%".$trimmed."%' limit $offset,$rowsPerPage";
  37. $result = mysql_query($query);
  38. //<a href=page.php?search=$string&offset=$offset>
  39. if(!$result){die(mysql_error());}
  40. while($data = mysql_fetch_array($result)) {
  41.  
  42. echo ' <tr> <td colspan="3" > <a href="'.$data['MedName'].'" >' . $data['dispname'].' </a> </td>';
  43. echo'</tr>';
  44. }
  45. echo "<tr><td align='left' colspan='2'>Result Pages:";
  46. for($i=1;$i<=$maxPage;$i++)
  47. {
  48.  
  49. echo "<a href=pagno.php?rowsPerPage=$rowsPerPage&page=$i>".$i."&nbsp;|&nbsp;";
  50. }
  51. echo "</td>
  52. <td>Showing page <strong>".$page."</strong> of <strong>".$maxPage."</strong> pages </td>
  53. </tr>";
  54.  
  55. ?>
In Page1 data displayed is from A1 to A10. Now the problem is that if I go to Page 2 data displayed must be from A11 to A12 but in this case it displaying the data where Id=11 in my db.
What is wrong with this code?
Suhasini
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 5
Reputation: Jorann is an unknown quantity at this point 
Solved Threads: 1
Jorann Jorann is offline Offline
Newbie Poster

Re: Problem with page navigation

 
0
  #7
Jun 3rd, 2008
well, you are doing this
  1. // counting the offset
  2. $offset = ($page - 1) * $rowsPerPage;

before you do this:
  1. // how many rows to show per page
  2. $rowsPerPage =10;

i think it shoudl be other way around...
Last edited by Jorann; Jun 3rd, 2008 at 9:54 am.
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 5
Reputation: Jorann is an unknown quantity at this point 
Solved Threads: 1
Jorann Jorann is offline Offline
Newbie Poster

Re: Problem with page navigation

 
0
  #8
Jun 3rd, 2008
This is what i once used in my code:


  1. $showfromthisid = ceil($pagenumber*10) -10;
  2. $maxshownmessages = 10;
  3.  
  4. $result = mysql_query("SELECT * FROM table ORDER BY id DESC LIMIT $showfromthisid,$maxshownmessages") or die(mysql_error());
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 48
Reputation: helraizer is an unknown quantity at this point 
Solved Threads: 3
helraizer helraizer is offline Offline
Light Poster

Re: Problem with page navigation

 
0
  #9
Jun 3rd, 2008
Originally Posted by Suhacini View Post
Changed the code again.
  1. <?php
  2. include ('conn.php');
  3.  
  4. // by default we show first page
  5. $page = 1;
  6.  
  7. // if $_GET['page'] defined, use it as page number
  8. if(isset($_GET['page']))
  9. {
  10. $page = $_GET['page'];
  11. }
  12. // counting the offset
  13. $offset = ($page - 1) * $rowsPerPage;
  14.  
  15.  
  16. //$searchresult = array();
  17. $var = $_POST['keyword'] ;
  18. $trimmed = trim($var);
  19.  
  20. // rows to return
  21. $limit=10;
  22.  
  23. $str = "select * from medlist where dispname like '%".$trimmed."%'";
  24. echo $str.'<br>';
  25. $res= mysql_query($str);
  26. if(!$res){die(mysql_error());}
  27. $num_rows= mysql_num_rows($res);
  28. echo $num_rows.'<br>';
  29.  
  30. // how many rows to show per page
  31. $rowsPerPage =10;
  32. $maxPage = ceil($num_rows/$rowsPerPage);
  33. //$page=(isset($_GET['page']))?$_GET['page']:1;
  34. //$offset=($page-1)*$rowsPerPage;
  35.  
  36. $query = "select * from medlist where dispname like '%".$trimmed."%' limit $offset,$rowsPerPage";
  37. $result = mysql_query($query);
  38. //<a href=page.php?search=$string&offset=$offset>
  39. if(!$result){die(mysql_error());}
  40. while($data = mysql_fetch_array($result)) {
  41.  
  42. echo ' <tr> <td colspan="3" > <a href="'.$data['MedName'].'" >' . $data['dispname'].' </a> </td>';
  43. echo'</tr>';
  44. }
  45. echo "<tr><td align='left' colspan='2'>Result Pages:";
  46. for($i=1;$i<=$maxPage;$i++)
  47. {
  48.  
  49. echo "<a href=pagno.php?rowsPerPage=$rowsPerPage&page=$i>".$i."&nbsp;|&nbsp;";
  50. }
  51. echo "</td>
  52. <td>Showing page <strong>".$page."</strong> of <strong>".$maxPage."</strong> pages </td>
  53. </tr>";
  54.  
  55. ?>
In Page1 data displayed is from A1 to A10. Now the problem is that if I go to Page 2 data displayed must be from A11 to A12 but in this case it displaying the data where Id=11 in my db.
What is wrong with this code?
I don't really understand the question here. What is the difference between A11 and Id=11?

Sam
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 90
Reputation: Suhacini is an unknown quantity at this point 
Solved Threads: 0
Suhacini Suhacini is offline Offline
Junior Poster in Training

Re: Problem with page navigation

 
0
  #10
Jun 3rd, 2008
Actually am using this for search.In my db I am storing few words A-Z but not in alphbetic order.so when i wanted to search 'A',then all data from my db related to 'A' must display.Here am able to display A1 to A10 in 1st page.In 2nd page it should start with A11 but it is displaying the data from my db whose Id=11.Am I clear to you now?
Suhasini
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
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