PHP Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingPHP Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old February 18th, 2005, 08:34 PM
HowdeeDoodee HowdeeDoodee is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2004
Posts: 14 HowdeeDoodee User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 50 m 18 sec
Reputation Power: 0
[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
//print

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> &nbsp</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> &nbsp</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> &nbsp</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> &nbsp</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> &nbsp</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> &nbsp</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

Reply With Quote
  #2  
Old February 18th, 2005, 10:55 PM
HowdeeDoodee HowdeeDoodee is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2004
Posts: 14 HowdeeDoodee User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 50 m 18 sec
Reputation Power: 0
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.

Reply With Quote
  #3  
Old February 18th, 2005, 11:53 PM
HowdeeDoodee HowdeeDoodee is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2004
Posts: 14 HowdeeDoodee User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 50 m 18 sec
Reputation Power: 0
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

Reply With Quote
  #4  
Old February 19th, 2005, 04:34 PM
MadCowDzz's Avatar
MadCowDzz MadCowDzz is offline
I'm Internet Famous
Dev Articles Frequenter (2500 - 2999 posts)
 
Join Date: Jan 2003
Location: Toronto, Canada
Posts: 2,890 MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level)MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level)MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 1 Week 16 h 14 m 9 sec
Reputation Power: 8
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"

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingPHP Development > Bug In Pagination Code


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway
Stay green...Green IT