943,723 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Marked Solved
  • Views: 2340
  • PHP RSS
You are currently viewing page 1 of this multi-page discussion thread
May 19th, 2008
0

Problem with page navigation

Expand Post »
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.

PHP Syntax (Toggle Plain Text)
  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. ?>
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
Suhacini is offline Offline
90 posts
since May 2008
May 27th, 2008
0

Re: Problem with page navigation

I changed my code.Here is my new code:
PHP Syntax (Toggle Plain Text)
  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.
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
Suhacini is offline Offline
90 posts
since May 2008
May 27th, 2008
0

Re: Problem with page navigation

Actually in your code you have to put $offset instead of $start in your query

check once
Reputation Points: 10
Solved Threads: 2
Newbie Poster
hivenk is offline Offline
13 posts
since May 2008
May 27th, 2008
0

Re: Problem with page navigation

Actually I got this code from some where.I wanted to write my own code..Can anyone tell me how to start with that?
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
Suhacini is offline Offline
90 posts
since May 2008
May 27th, 2008
0

Re: Problem with page navigation

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.
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
Suhacini is offline Offline
90 posts
since May 2008
Jun 3rd, 2008
0

Re: Problem with page navigation

Changed the code again.
PHP Syntax (Toggle Plain Text)
  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?
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
Suhacini is offline Offline
90 posts
since May 2008
Jun 3rd, 2008
0

Re: Problem with page navigation

well, you are doing this
PHP Syntax (Toggle Plain Text)
  1. // counting the offset
  2. $offset = ($page - 1) * $rowsPerPage;

before you do this:
PHP Syntax (Toggle Plain Text)
  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.
Reputation Points: 10
Solved Threads: 1
Newbie Poster
Jorann is offline Offline
5 posts
since Jun 2008
Jun 3rd, 2008
0

Re: Problem with page navigation

This is what i once used in my code:


PHP Syntax (Toggle Plain Text)
  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());
Reputation Points: 10
Solved Threads: 1
Newbie Poster
Jorann is offline Offline
5 posts
since Jun 2008
Jun 3rd, 2008
0

Re: Problem with page navigation

Click to Expand / Collapse  Quote originally posted by Suhacini ...
Changed the code again.
PHP Syntax (Toggle Plain Text)
  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
Reputation Points: 11
Solved Threads: 3
Light Poster
helraizer is offline Offline
48 posts
since Apr 2008
Jun 3rd, 2008
0

Re: Problem with page navigation

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?
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
Suhacini is offline Offline
90 posts
since May 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: 2 tier & 3 tier architectures in php?
Next Thread in PHP Forum Timeline: Serious problem with attchments-please help





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC