Have created php/mysql pagination but have minor issue:

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
tovia2000
Forum Newbie
Posts: 12
Joined: Sat Aug 25, 2007 7:45 am

Have created php/mysql pagination but have minor issue:

Post by tovia2000 »

scottayy | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hi Tovia here
I have three issues with my pagination, pls help:

1- The same records appear on every page, eg 123 on page 1, and 123 on page 2., which should have 456.
2- The current page isnt in bold in the number listing
3- The numbers to the right of the current page dont appear

My Code is as follows, really need to finish this soon:

Code: Select all

<?PHP
// user credentials
$host = "localhost";
$user = "root";
$pass = "police";
$data = "test";

// Establish connection, and select db
$conn = mysql_connect($host, $user, $pass) or die(mysql_error());
mysql_select_db($data, $conn) or die(mysql_error());

//create some variables
$pagenumber = (isset($GET["page"]) && is_numeric($GET["page"])) ? $GET["page"] : 1;

//establish number of resulsts per page
$perpage = 5;

//establish a padding value
$padding = 10;

// get start index of results
$startindex = ($pagenumber * $perpage) - $perpage;

// get total number of dbms entries
$totalcount = "SELECT COUNT(*) as 'Total' FROM test.articles";
$rscount = mysql_query($totalcount, $conn) or die(mysql_error());
$rowcount = mysql_fetch_object($rscount);

//Show page listings
print "
";

// get our total number of pages
$numofpages = ceil($rowcount->Total / $perpage);

// print link to first page
print "FIRST ";

// if our current page, minus our padding is greater than 1
if (($pagenumber - $padding) > 1) {
print "... ";
// set our lower limit
$lowerlimit = $pagenumber - $padding;
// print all padded numbers between lowerlimit and current page
for($i = $lowerlimit; $i < $pagenumber; $i++) {
print "".$i." ";

}

} else {
// print all numbers between current page, and first page
for($i = 2; $i < $pagenumber; $i++) {
print "".$i." ";
}
}

// if were not on the first page, or the last page, print current page
if(($pagenumber !=0) && ($pagenumber !=1) && ($pagenumber != $numofpages))
{ print " - " . $pagenumber . " - "; }

// if our current page, plus our padding , is less than the total number of pages
if (($pagenumber + $padding) < $numofpages) {
// set upper limit
$upperlimit = $pagenumber + $padding;
// print all numbers from padded pages above current page
for($i = ($pagenumber + 1); $i <= $upperlimit; $i++) {
print "".$i." ";
}
print "... ";
} else 
//print all page numbers between number of pages and current page
for($i = ($pagenumber +1); $i < $numofpages; $i++) {
print "".$i." ";
}
// print link to last page
print "LAST";

print "";

// get page results
$sql = "SELECT id,body FROM test.articles ORDER BY timestamp1
LIMIT $startindex, $perpage";

echo $startindex;

// get result set
$rs = mysql_query($sql,$conn) or die(mysql_error());

// do we have results
if (mysql_num_rows($rs) > 0) {
// show the results
while ($row = mysql_fetch_object($rs)) {
print "
";
print $row->id;
print ": ";
print $row->body;
print ": ";
print "";

}

} else {
print "Sorry, no results found.";

}

//close our dbms connection
mysql_close($conn);

?>

Thanks in Advance

Tovia


scottayy | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Re: Have created php/mysql pagination but have minor issue:

Post by superdezign »

I hope you aren't expecting us to just do it for you. I haven't read your code, but I'll try to help anyway.
tovia2000 wrote:1- The same records appear on every page, eg 123 on page 1, and 123 on page 2., which should have 456.
Do you make use of GET variables and MySQL LIMITs?
tovia2000 wrote:2- The current page isnt in bold in the number listing
Do you make it bold...?
tovia2000 wrote:3- The numbers to the right of the current page dont appear
... What?


I have two tutorials that may help you out.
Basic Pagination
Object-Oriented Pagination
tovia2000
Forum Newbie
Posts: 12
Joined: Sat Aug 25, 2007 7:45 am

Solved, thanks for help my friend:

Post by tovia2000 »

scottayy | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Problem1: Same records appeared on all pages, because i used $get instead of $_GET

Code: Select all

//create some variables
$pagenumber = (isset($_GET["page"]) && is_numeric($_GET["page"])) ? $_GET["page"] : 1;
Problem2: Current page(selected page) number not printing in bold in list
solved after fixing above first problem,

Problem3: Numbers not printing to right of current page in listing, because i missed { } brackets out, should have been:

Code: Select all

//Show page listings to right:---------------------------------------------------
// if our current page, plus our padding , is less than the total number of pages
if (($pagenumber + $padding) < $numofpages) {
// set upper limit
$upperlimit = $pagenumber + $padding;
// print all numbers from padded pages above current page
for($i = ($pagenumber + 1); $i <= $upperlimit; $i++) {
		print "<a href=\"index2.php?page=".$i."\">".$i."</a> ";
		}
		print "... ";
	} else {
//print all page numbers between number of pages and current page
	for($i = ($pagenumber +1); $i < $numofpages; $i++) {
		print "<a href=\"index2.php?page=".$i."\">".$i."</a> ";
	}
}


scottayy | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
tovia2000
Forum Newbie
Posts: 12
Joined: Sat Aug 25, 2007 7:45 am

everything works fine but i get one error at bottom of each

Post by tovia2000 »

Everything works fine but i get one error at bottom of each page:

PHP Warning: Unknown: failed to open stream: No such file or directory in Unknown on line 0 PHP Fatal error: Unknown: Failed opening required 'C:\Inetpub\wwwroot\document_root.php' (include_path='.;C:\Program Files\PHP\includes') in Unknown on line 0

Also any hhtp or href link lines have a background border of white, how do i chnage this?

kind regards

Tovia
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

Does C:\Inetpub\wwwroot\document_root.php exist?

Code: Select all

echo file_exists('C:\Inetpub\wwwroot\document_root.php') ? 'exists' : 'doesn\'t exist';
The white border on links is most likely a css issue.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
tovia2000
Forum Newbie
Posts: 12
Joined: Sat Aug 25, 2007 7:45 am

Hi, document_rrot doesnt exists your right,:

Post by tovia2000 »

Hi

Doesnt exist, how do i create this? or download this from somewhere,

Sorry for the late reply been bussy with trying to apply css to my pagination, problem is i have a pure php script as above and i get errors when i try to incl css in my script.

kind regards

Tovia Singer M
Post Reply