PHP MYSQL Paging Problem

Reply

Join Date: Jan 2008
Posts: 16
Reputation: BillyMako is an unknown quantity at this point 
Solved Threads: 0
BillyMako BillyMako is offline Offline
Newbie Poster

PHP MYSQL Paging Problem

 
0
  #1
Jan 31st, 2008
Hi, i am having problems displaying the second page of search results. I have applied similar code to a view all logs page which works perfectly but i am having problems making it work with search results. I think it might be a problem with $searchString, not sure???
Please help me!!!

Here is the code --------->

  1. //save keyword from html search form to variable
  2. $searchString2 = addslashes($_POST['search2']);
  3.  
  4. if(!empty($searchString2)) {
  5. // how many rows to show per page
  6. $rowsPerPage = 20;
  7.  
  8. // by default we show first page
  9. $pageNum = 1;
  10.  
  11. // if $_GET['page'] defined, use it as page number
  12. if(isset($_GET['page']))
  13. {
  14. $pageNum = $_GET['page'];
  15. }
  16.  
  17. // counting the offset
  18. $offset = ($pageNum - 1) * $rowsPerPage;
  19. $query = "SELECT * FROM fishinglog WHERE username LIKE '$searchString2' AND private = 'no' ORDER BY date11_date DESC LIMIT $offset, $rowsPerPage ";
  20. $result = mysql_query($query);
  21. Print "<h3>Search Results for Username: <font color='#76b8f2'>'$searchString2' </font></h3><br />";
  22. Print "<BR>Submit a <a href=newlog.php>Fishing Log</a><BR>";
  23. Print "<BR><a href=viewyourlogs.php>View</a> your Fishing Logs<BR>";
  24. Print "<BR><a href=searchlogs.php>Search</a> members Fishing Logs<BR><BR>";
  25. Print"<hr>";
  26. Print"<table>";
  27. $anymatches01=mysql_num_rows($result);
  28. if ($anymatches01 != 0) {
  29. Print"<tr><td><font color=#00CCff>Fishing Location</font></td><td><font color=#00CCff>Date</font></td><td><font color=#00CCff>Action</font></td><td><font color=#00CCff>Created by</font></td>";
  30. }else{
  31. Print"";
  32. }
  33. while ($i = mysql_fetch_array($result)){
  34. print <<<END
  35. <tr><td width=200><a href="displaylog.php?fishing_location=$i[fishing_location]&&id=$i[id]">$i[fishing_location]</a></td><td width=100>$i[date11_date] $i[date11_month] $i[date11_year]</td><td width=70>$i[action]</td><td><a href="userinfo.php?user=$i[username]">$i[username]</a></td>
  36.  
  37. END;
  38. }
  39. Print"</table>";
  40. // how many rows we have in database
  41. $query = "SELECT COUNT(*) AS numrows FROM fishinglog WHERE username LIKE '$searchString2' AND private = 'no'";
  42. $result = mysql_query($query) or die('Error, query failed');
  43. $row = mysql_fetch_array($result, MYSQL_ASSOC);
  44. $numrows = $row['numrows'];
  45.  
  46. // how many pages we have when using paging?
  47. $maxPage = ceil($numrows/$rowsPerPage);
  48.  
  49. // print the link to access each page
  50. $self = $_SERVER['PHP_SELF'];
  51. $nav = '';
  52.  
  53. for($page = 1; $page <= $maxPage; $page++)
  54. {
  55. if ($page == $pageNum)
  56. {
  57. $nav .= " $page "; // no need to create a link to current page
  58. }
  59. else
  60. {
  61. $nav .= " <a href=\"$self?page=$page\">$page</a> ";
  62. }
  63. }
  64. // creating previous and next link
  65. // plus the link to go straight to
  66. // the first and last page
  67.  
  68. if ($pageNum > 1)
  69. {
  70. $page = $pageNum - 1;
  71. $prev = " <a href=\"$self?page=$page\">[Prev]</a> ";
  72.  
  73. $first = " <a href=\"$self?page=1\">[First Page]</a> ";
  74. }
  75. else
  76. {
  77. $prev = '&nbsp;'; // we're on page one, don't print previous link
  78. $first = '&nbsp;'; // nor the first page link
  79. }
  80.  
  81. if ($pageNum < $maxPage)
  82. {
  83. $page = $pageNum + 1;
  84. $next = " <a href=\"$self?page=$page\">[Next]</a> ";
  85.  
  86. $last = " <a href=\"$self?page=$maxPage\">[Last Page]</a> ";
  87. }
  88. else
  89. {
  90. $next = '&nbsp;'; // we're on the last page, don't print next link
  91. $last = '&nbsp;'; // nor the last page link
  92. }
  93.  
  94. // print the navigation link
  95. echo"<br>";
  96. echo "<center>";
  97. echo $first . $prev . $nav . $next . $last;
  98. echo "</center>";
  99. if ($anymatches01 == 0) {
  100. echo "Sorry, but your search criteria matched no results<br><br>";
  101. //And we remind them what they searched for
  102. echo "<b><a href=searchlogs.php>Search again</a><br><br>";
  103. }
  104. }
Last edited by digital-ether; Jan 31st, 2008 at 5:49 pm. Reason: Please highlight your code
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 16
Reputation: BillyMako is an unknown quantity at this point 
Solved Threads: 0
BillyMako BillyMako is offline Offline
Newbie Poster

Re: PHP MYSQL Paging Problem

 
0
  #2
Feb 25th, 2008
i still have this problem p please help!
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 1
Reputation: ambrose is an unknown quantity at this point 
Solved Threads: 0
ambrose ambrose is offline Offline
Newbie Poster

you have register_globals in php off in new version

 
0
  #3
Jun 28th, 2008
hai billy

look into this thread here the same problem you are having is solved.

http://www.webmasterworld.com/forum88/9860.htm

you have to change the start page code?.

if you are not sure use that code given in that site. and change the portion

if(!isset($_GET['start'])) { // This variable is set to zero for the first page
$start = 0;
} else {
$start = $_GET['start'];
}
Reply With Quote Quick reply to this message  
Reply

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



Similar Threads
Other Threads in the PHP Forum
Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC