By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,190 Members | 1,469 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,190 IT Pros & Developers. It's quick & easy.

Paging isnt working

P: 21
I am trying to get some data and show it usig paging. my coding displays and also the paging is also visible on my web page. but it doesnt work. as in when i click next the next set of results aren't shown. Pls tell me wats wron wit my coding.

pls not that this is a jst a snippet from my coding.so ignore unclosed tags n all.
Expand|Select|Wrap|Line Numbers
  1. $db_host = 'localhost';
  2. $db_user = 'root';
  3. $db_pwd = 'dba';
  4.  
  5. $database = 'mydb';
  6. $table = 'user_groups';
  7.  
  8. $rowsPerPage = 3;
  9. $pageNum = 1;
  10. $offset = ($pageNum - 1) * $rowsPerPage;
  11.  
  12. if (!mysql_connect($db_host, $db_user, $db_pwd))
  13. die("Can't connect to database");
  14.  
  15. if (!mysql_select_db($database))
  16. die("Can't select database");
  17.  
  18. $result1 = mysql_query(" SELECT ug_name FROM user_groups "." LIMIT $offset, $rowsPerPage");
  19. if (!$result1) {
  20. die("Query to show fields from table failed");
  21. }
  22. if(isset($_GET['page']))
  23. {
  24. $pageNum = $_GET['page'];
  25. }
  26.  
  27. ?>
  28. <table width="348" border="1" cellpadding="1" cellspacing="1" bordercolor="#1648AB" bgcolor="#EBE9ED">
  29. <tr>
  30. <td width="159" class="right"><strong class="right">User Group</strong></td>
  31. <td width="63" class="right"><strong>Edit</strong></td>
  32. <td width="108" class="right"><strong>Remove</strong></td>
  33. </tr>
  34. <?php while($row1 = mysql_fetch_row($result1)) { ?>
  35. <tr><?php foreach($row1 as $cell){?>
  36. <td><?php echo "$cell"; ?></td>
  37. <td><a href="editusergroups.php?x=<?php echo"$cell";?>">Edit</a></td>
  38. <td><a href="deleteusergroups.php?x=<?php echo"$cell";?>">Remove</a></td>
  39. </tr>
  40. <?php } }
  41. $query = "SELECT COUNT(ug_name) AS numrows FROM user_groups";
  42. $result = mysql_query($query) or die('Error, query failed');
  43. $row = mysql_fetch_array($result, MYSQL_ASSOC);
  44. $numrows = $row['numrows'];
  45. $maxPage = ceil($numrows/$rowsPerPage);
  46. $self = $_SERVER['PHP_SELF'];
  47. $nav = '';
  48. $fields_num = mysql_num_fields($result1);
  49.  
  50. for($page = 1; $page <= $maxPage; $page++)
  51. {
  52. if ($page == $pageNum)
  53. {
  54. $nav .= " $page "; // no need to create a link to current page
  55. }
  56. else
  57. {
  58. $nav .= " <a href=\"$self?page=$page\">$page</a> ";
  59. }
  60. }
  61.  
  62. if ($pageNum > 1)
  63. {
  64. $page = $pageNum - 1;
  65. $prev = " <a href=\"$self?page=$page\">[Prev]</a> ";
  66. $first = " <a href=\"$self?page=1\">[First Page]</a> ";
  67. }
  68. else
  69. {
  70. $prev = '&nbsp;'; // we're on page one, don't print previous link
  71. $first = '&nbsp;'; // nor the first page link
  72. }
  73.  
  74. if ($pageNum < $maxPage)
  75. {
  76. $page = $pageNum + 1;
  77. $next = " <a href=\"$self?page=$page\">[Next]</a> ";
  78. $last = " <a href=\"$self?page=$maxPage\">[Last Page]</a> ";
  79. }
  80. else
  81. {
  82. $next = '&nbsp;'; // we're on the last page, don't print next link
  83. $last = '&nbsp;'; // nor the last page link
  84. }
  85.  
  86. // print the navigation link
  87. echo $first . $prev . $nav . $next . $last;
  88.  
  89. mysql_free_result($result1);
  90. ?>
  91. </table>
  92. </div>
  93. <br class="clearfloat" />
  94. <?php @include_once("ui_footer.php");?>
  95. </div>
  96. </body>
  97. </html>
Jul 18 '08 #1
Share this Question
Share on Google+
2 Replies


rohypnol
P: 54
First, replace:
Expand|Select|Wrap|Line Numbers
  1. $rowsPerPage = 3;
  2. $pageNum = 1;
  3. $offset = ($pageNum - 1) * $rowsPerPage;
with
Expand|Select|Wrap|Line Numbers
  1. $rowsPerPage = 3;
  2. if(isset($_GET['page']))
  3. {
  4.     $pageNum = mysql_real_escape_string($_GET['page']);
  5. }
  6. else
  7. {
  8.     $pageNum = 1;
  9. }
  10. $offset = ($pageNum - 1) * $rowsPerPage;
Second, remove:
Expand|Select|Wrap|Line Numbers
  1. if(isset($_GET['page']))
  2. {
  3. $pageNum = $_GET['page'];
  4. }
And here are some tips:
- ALWAYS sanitize user input before you put it into MYSQL. You are connecting to MYSQL using the root account and you are putting user strings into queries. A user could easily do something like www.site.com/page.php?page='; delete from user_groups; which would get you fired :( Use mysql_real_escape_string before pasting strings into queries.
- Use the [ code ] tags you have in the bytes.com message editor to paste code, that way it's easier to read your code and it's easier to tell you where the problem is because that tag will add line numbers.
- Break PHP from HTML because if in a month you need to change something in your page, you won't even know where to start! Use a templating system such as Smarty or at least don't output HTML using PHP variables.
- While in production, you should ALWAYS set PHP to report all errors, by setting error_reporting to "E_ALL | E_STRICT" and display_errors to "on" in your php.ini file (don't forget to restart APACHE after this). If you would have done that, you would have easily seen your error! It's on the first post in the PHP section of this website, it's a sticky post telling you to turn on error reporting. If you don't have access to php.ini check out this: http://bytes.com/forum/thread629295.html
Jul 18 '08 #2

P: 25

Post your reply

Sign in to post your reply or Sign up for a free account.