Thread: Paging Problem
View Single Post
Join Date: Jun 2007
Posts: 1,225
Reputation: kkeith29 has a spectacular aura about kkeith29 has a spectacular aura about kkeith29 has a spectacular aura about 
Solved Threads: 166
kkeith29's Avatar
kkeith29 kkeith29 is offline Offline
Nearly a Posting Virtuoso

Re: Paging Problem

 
0
  #10
Sep 30th, 2007
i set up a database to check the code and i think i fixed the problem.

  1. <?php
  2. //****************************************************************************
  3. ////////////////////////Downloaded from www.plus2net.com //////////////////////////////////////////
  4. /////////////////////// Visit www.plus2net.com for more such script and codes.
  5. //////// Read the readme file before using /////////////////////
  6. //////////////////////// You can distribute this code with the link to www.plus2net.com ///
  7. ///////////////////////// Please don't remove the link to www.plus2net.com ///
  8. //////////////////////////
  9. //*****************************************************************************
  10. ?>
  11. <!doctype html public "-//w3c//dtd html 3.2//en">
  12.  
  13. <html>
  14.  
  15. <head>
  16. <title>Plus2net.com paging script in PHP</title>
  17. <meta name="GENERATOR" content="Arachnophilia 4.0">
  18. <meta name="FORMATTER" content="Arachnophilia 4.0">
  19. </head>
  20.  
  21. <body bgcolor="#ffffff" text="#000000" link="#0000ff" vlink="#800080" alink="#ff0000">
  22. <?php
  23. require "mysql.php"; // All database details will be included here
  24.  
  25. $page_name="paging.php"; // If you use this code with a different page ( or file ) name then change this
  26.  
  27. if(!isset($start)) { // This variable is set to zero for the first page
  28. $start = 0;
  29. }
  30.  
  31. $eu = ($start -0);
  32. $limit = 1; // No of records to be shown per page.
  33. $this = $eu;
  34. $number = 2;
  35. $back = $eu - $limit;
  36. $next = $eu + $limit;
  37.  
  38.  
  39. /////////////// WE have to find out the number of records in our table. We will use this to break the pages///////
  40. $query2=" SELECT * FROM student_adv ";
  41. $result2=mysql_query($query2);
  42. echo mysql_error();
  43. $nume=mysql_num_rows($result2);
  44. /////// The variable nume above will store the total number of records in the table////
  45.  
  46. /////////// Now let us print the table headers ////////////////
  47. $bgcolor="#f1f1f1";
  48. echo "<TABLE width=50% align=center cellpadding=0 cellspacing=0> <tr>";
  49. echo "<td bgcolor='dfdfdf' >&nbsp;<font face='arial,verdana,helvetica' color='#000000' size='4'>ID</font></td>";
  50.  
  51. echo "<td bgcolor='dfdfdf' >&nbsp;<font face='arial,verdana,helvetica' color='#000000' size='4'>Name</font></td>";
  52. echo "<td bgcolor='dfdfdf' >&nbsp;<font face='arial,verdana,helvetica' color='#000000' size='4'>Class</font></td>";
  53. echo "<td bgcolor='dfdfdf'>&nbsp;<font face='arial,verdana,helvetica' color='#000000' size='4'>Mark</font></td></tr>";
  54.  
  55. ////////////// Now let us start executing the query with variables $eu and $limit set at the top of the page///////////
  56. $query=" SELECT * FROM student_adv limit $eu, $number ";
  57. $result=mysql_query($query);
  58. echo mysql_error();
  59.  
  60. //////////////// Now we will display the returned records in side the rows of the table/////////
  61. while($noticia = mysql_fetch_array($result))
  62. {
  63. if($bgcolor=='#f1f1f1'){$bgcolor='#ffffff';}
  64. else{$bgcolor='#f1f1f1';}
  65.  
  66. echo "<tr >";
  67. echo "<td align=left bgcolor=$bgcolor id='title'>&nbsp;<font face='Verdana' size='2'>$noticia[id]</font></td>";
  68.  
  69. echo "<td align=left bgcolor=$bgcolor id='title'>&nbsp;<font face='Verdana' size='2'>$noticia[name]</font></td>";
  70. echo "<td align=left bgcolor=$bgcolor id='title'>&nbsp;<font face='Verdana' size='2'>$noticia[class]</font></td>";
  71. echo "<td align=left bgcolor=$bgcolor id='title'>&nbsp;<font face='Verdana' size='2'>$noticia[mark]</font></td>";
  72.  
  73. echo "</tr>";
  74. }
  75. echo "</table>";
  76. ////////////////////////////// End of displaying the table with records ////////////////////////
  77.  
  78. ///// Variables set for advance paging///////////
  79. $p_limit=8; // This should be more than $limit and set to a value for whick links to be breaked
  80. if(!isset($p_f)){$p_f=0;}
  81. $p_fwd=$p_f+$p_limit;
  82. $p_back=$p_f-$p_limit;
  83. //////////// End of variables for advance paging ///////////////
  84. /////////////// Start the buttom links with Prev and next link with page numbers /////////////////
  85. echo "<table align = 'center' width='50%'><tr><td align='left' width='20%'>";
  86. if($p_f<>0){print "<a href='$page_name?start=$p_back&p_f=$p_back'><font face='Verdana' size='2'>PREV $p_limit</font></a>"; }
  87. echo "</td><td align='left' width='10%'>";
  88. //// if our variable $back is equal to 0 or more then only we will display the link to move back ////////
  89. if($back >=0 and ($back >=$p_f)) {
  90. print "<a href='$page_name?start=$back&p_f=$p_f'><font face='Verdana' size='2'>PREV</font></a>";
  91. }
  92. //////////////// Let us display the page links at center. We will not display the current page as a link ///////////
  93. echo "</td><td align=center width='30%'>";
  94. for($i=$p_f;$i < $nume and $i<($p_f+$p_limit);$i=$i+$limit){
  95. if($i <> $eu){
  96. $i2=$i+$p_f;
  97. echo " <a href='$page_name?start=$i&p_f=$p_f'><font face='Verdana' size='2'>$i</font></a> ";
  98. }
  99. else { echo "<font face='Verdana' size='4' color=red>$i</font>";} /// Current page is not displayed as link and given font color red
  100.  
  101. }
  102.  
  103.  
  104. echo "</td><td align='right' width='10%'>";
  105. ///////////// If we are not in the last page then Next link will be displayed. Here we check that /////
  106. if($this < $nume and $this <($p_f+$p_limit)) {
  107. print "<a href='$page_name?start=$next&p_f=$p_f'><font face='Verdana' size='2'>NEXT</font></a>";}
  108. echo "</td><td align='right' width='20%'>";
  109. if($p_fwd < $nume){
  110. print "<a href='$page_name?start=$p_fwd&p_f=$p_fwd'><font face='Verdana' size='2'>NEXT $p_limit</font></a>";
  111. }
  112. echo "</td></tr></table>";
  113.  
  114.  
  115. ?>
  116. <center><a href='http://www.plus2net.com'>PHP SQL HTML free tutorials and scripts</a></center>
  117. </body>
  118.  
  119. </html>
Reply With Quote