|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
[SOLVED] Bug In Pagination Code
The following pagination and table display code works great except for one minor bug. The bug occurs when there are only ten records retrieved. When there are only ten records retrieved a page 2 hyperlink appears even when there are no records to be viewed on page two. Please feel free to change any comments because many comments reflect my ignorance at trying to decipher how the code works. Any reply would be appreciated. Thank you in advance.
</HEAD> <form action="Topic.php" method="get"> <font size="2" face="Arial"><b> Free Form Search Of Topics: <input name="SeeAlso" type="text" /> <input type="submit" /> </form> <BODY> <?php $username = " "; $password = " "; $dbname = " "; $global_db = mysql_connect('localhost', $username, $password); mysql_select_db($dbname, $global_db); $query = "SELECT * FROM `View1` WHERE `Topic` REGEXP '[[:<:]]($SeeAlso)[[:>:]]'"; $result = mysql_query($query) or die("ERROR63"); $num_record = mysql_num_rows($result); if($num_record > $display) { // Only show 1,2,3,etc. when //there are more records found that fit on 1 page if(empty($pagenr)) { $pagenr = 1; } // variables used in the code $display = 10; // number of records to display per page $max_pages_to_print = 7; // number of pages to show, if you change this you also have to change the variable 'middlenumber', for example:increase this one with two, increase middlenumber with one $startrecord = $pagenr * $display; // first record to show from the queryresult $num_pages = intval($num_record / $display) + 1; // total number of pages $loopcounter = 0; // counter for whileloop $currentpage = $pagenr; // Page where we are at the moment $middlenumber = 3; // Number will be decreased from variable currentpage in order to get the currentpage always in the middle $colourcounter = 0; // Variable to change the background-color of the <td> $i = 1; // variable that will print 1,2,3,etc.. $x = 0; // variable i use to put always the current, marked page in the middle //print a table of retrieval data echo "<div align=\"center\">\n"; echo " <center>\n"; echo " <table border=\"1\" width=\"700\" height=\"25\" cellspacing=\"1\" bgcolor=\"#D8E0F0\">\n"; echo " <tr>\n"; echo " <td colspan=\"2\" valign=\"middle\" align=\"center\" bgcolor=\"#0000c8\" width=\"690\">\n"; echo " <p align=\"center\"><font face=\"Arial\" color=\"#FFFFFF\">Retrieval Data</font></td>\n"; echo " </tr>\n"; echo " <tr>\n"; echo " <td width=\"248\"><font face=\"Arial\" size=\"2\">Search Term:</font></td>\n"; echo " <td width=\"436\"><font face=\"Arial\" size=\"2\">$SeeAlso</font></td>\n"; echo " </tr>\n"; echo " <tr>\n"; echo " <td width=\"248\"><font face=\"Arial\" size=\"2\">Current Page</font></td>\n"; echo " <td width=\"436\"><font face=\"Arial\" size=\"2\">$currentpage</font></td>\n"; echo " </tr>\n"; echo " <tr>\n"; echo " <td width=\"248\"><font face=\"Arial\" size=\"2\">Total Pages Retrieved:</font></td>\n"; echo " <td width=\"436\"><font face=\"Arial\" size=\"2\">$num_pages</font></td>\n"; echo " </tr>\n"; echo " <tr>\n"; echo " <td width=\"248\"><font face=\"Arial\" size=\"2\">Total Citations Retrieved:</font></td>\n"; echo " <td width=\"436\"><font face=\"Arial\" size=\"2\">$num_record</font></td>\n"; echo " </tr>\n"; echo " </table>\n"; echo " </center>\n"; echo "</div>\n"; // pageination code starts here print("<table border=0 align=center><tr>"); //if the current page number is greater than or = to the maximum pages to if($currentpage >= $max_pages_to_print) { //set up and print a table to display page numbers as hyperlinks //print a hyperlink page number equal to $i which is the variable that will print the //page number $i is preceded by the name of the current server [PHP_SELF] //****NOT SURE ?pagenr= is related to the page number //****NOT SURE &SeeAlso related to user input //$_GET['SeeAlso'] gets the user input print '<td><a href="' . $_SERVER['PHP_SELF'] . '?pagenr=' . $i . '&SeeAlso=' . $_GET['SeeAlso'] . '">' . $i . '</a>  </td>' ; // print pagenumbers } //BEGIN LOOP //while the loopcounter is less than the maximum pages to print then.. while($loopcounter < $max_pages_to_print) { // the current page is greater than or equql to the maximum pages to print if($currentpage >= $max_pages_to_print) { // If the current page absolutely equals the variable used to print the page numbers if($currentpage == $i) { //print another table of page numbers as hyperlinks print '<td><a href="' . $_SERVER['PHP_SELF'] . '?pagenr=' . $i . '&SeeAlso=' . $_GET['SeeAlso'] . '">' . $i . '</a>  </td>' ; // print pagenumbers //increase pagenr $i += 1; // increase loopcounter $loopcounter += 1; } // End marking // if last page has been printed, exit loop if($i > $num_pages) { break; } if($x == 0) { // current page will always be printed in the middle $i = $currentpage - $middlenumber; } print '<td><a href="' . $_SERVER['PHP_SELF'] . '?pagenr=' . $i . '&SeeAlso=' . $_GET['SeeAlso'] . '">' . $i . '</a>  </td>' ; // print pagenumbers $x = $x + 1; $i += 1; //increase pagenr $loopcounter += 1; // increase loopcounter } else { // Else user clicks on a pagenumber lower $max_pages_to_print // Mark current page if($currentpage == $i) { print '<td><a href="' . $_SERVER['PHP_SELF'] . '?pagenr=' . $i . '&SeeAlso=' . $_GET['SeeAlso'] . '">' . $i . '</a>  </td>' ; // print pagenumbers $i += 1; //increase pagenr $loopcounter += 1; // increase loopcounter } // End marking if($i > $num_pages) { // if less than $max_mages_to_print, exit loop break; } print '<td><a href="' . $_SERVER['PHP_SELF'] . '?pagenr=' . $i . '&SeeAlso=' . $_GET['SeeAlso'] . '">' . $i . '</a>  </td>' ; // print pagenumbers $i += 1; //increase pagenr $loopcounter += 1; // increase loopcounter } // End if } // END LOOP if(($num_pages > $max_pages_to_print AND // notice the user that there are more pages $i <= $num_pages)) { print '<td><a href="' . $_SERVER['PHP_SELF'] . '?pagenr=' . $i . '&SeeAlso=' . $_GET['SeeAlso'] . '">' . $i . '</a>  </td>' ; // print pagenumbers } print("</tr></table>"); $startrecord = $startrecord - $display; // Set startrecord to the right position // Some calculation for the lastrecord if($currentpage == $num_pages) { // Last page... $lastrecord = $num_record; // so $lastrecord = $num_record } else { $lastrecord = ($currentpage * $display); } } // End of the first if-statement // actual query, watch the end($startrecord, $display) //the variable $querystring is determined from the branching sequence at the beginning of the code //global $query2; //$query2 = "$querystring LIMIT $startrecord, $display"; $query2 = "SELECT * FROM `View1` WHERE `Topic` REGEXP '[[:<:]]($SeeAlso)[[:>:]]' LIMIT $startrecord, $display"; $result2 = mysql_query($query2) or die("ERROR148"); // print results on screen print("<table border=1 align=center width=700 bgcolor=#0000c8 cellpadding=\"2\" cellspacing=\"1\" font face=Arial><tr><td align=center><font color=#FFFFFF><b>Topic</b></font></td><td align=center><font color=#FFFFFF><b>Subtopic</b></font></td><td align=center><font color=#FFFFFF><b>References</b></font></td></tr>"); $Topic="Topic"; $Subtopic="Subtopic"; $References="References"; while(list($Topic,$Subtopic,$References)= mysql_fetch_row($result2)) { if ($colorcounter == 0) { $colorbg = "#d8e0f0"; } else { $colorbg = "#e6eaf4"; $colorcounter = $colorcounter - 2; } //<td><font size="1">cat</font></td> print("<tr><td bgcolor=$colorbg><font size=\"1\">$Topic</font></td><td align=left bgcolor=$colorbg><font size=\"1\">$Subtopic</td><td align=left bgcolor=$colorbg><font size=\"1\">$References</td></tr>"); $colorcounter = $colorcounter + 1; } print("</table>"); ?> </BODY> Last edited by HowdeeDoodee : February 18th, 2005 at 11:55 PM. Reason: Problem Solved |
|
#2
|
|||
|
|||
|
Upon further testing I found the problem exists not just for 10 pages but for multiplies of ten. Then I reset the $display = 10 variable so $display = 9. However, the problem still persisted when I called up 9 records or multiples of 9 records like 18. There is always an empty page showing with no records if the number of records is a multiple of $display. Any help would be appreciated. Thank you in advance.
|
|
#3
|
|||
|
|||
|
Solution found. A big yee haa.
$num_pages = intval($num_record / $display) + 1; // total number of pages should be changed to... $num_pages = (ceil($num_record/$display)); //total number of pages |
|
#4
|
||||
|
||||
|
in the future, try to put large blocks of code [actually, all code] with in [php][/php] tags... this will colour-code your tags, preserve your indentation, save screen real-estate, and give others an overall more pleasurable read through your code =)
Other then that, congratz on solving your problem =) "yee haa" |
![]() |
| Viewing: Dev Articles Community Forums > Programming > PHP Development > Bug In Pagination Code |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|