?
Solved

PHP Pagination and Page Count

Posted on 2009-04-25
7
Medium Priority
?
378 Views
Last Modified: 2013-12-12
Hey All,

I am using the below code to display 3 results per page which is working fine, but the problem is I cannot get a total page count.

This "$last=ceil($num/$howmany);" should display the $num which is 30 devided by $howmany which is 3; so it should be 1o pages, but "print($last);" is printing the number 1 rather than 10. This tells me that it is only doing 3/3 rather than 30/3. Any help on how to fix this would be greatly appreciated.

Thanks in advance.
<?php 
// WRITTEN BY Valley ENT \\
 
 
// CONNECT TO DATABASE \\
...........
 
// PAGINATION CONFIGURATION \\
$page = mysql_real_escape_string($_GET['page']);
$howmany = 3;
$start = $page * $howmany;
 
// QUERY CONFIGURATION \\
$query="SELECT * FROM ual ORDER BY cname LIMIT $start, $howmany";
$result=mysql_query($query);
$num=mysql_numrows($result);
$last=ceil($num/$howmany);
 
// CLOSE DATABASE CONNECTION \\
mysql_close();
 
// SEETING CSS STYLE TO PAGE \\
print("<link href=../../default.css rel=stylesheet type=text/css />");
 
// REQUESTING DATABASE INFO \\
$i=0;
while ($i < $num) {
$cname=mysql_result($result,$i,"cname");
$description=mysql_result($result,$i,"description");
$phone=mysql_result($result,$i,"phone");
$website=mysql_result($result,$i,"website");
 
// PRINTING DATABASE RESULTS \\
print("<div id=content>");
print("<b>" . "<a href=$website target=_blank>$cname</a>" . "</b><br />" . $description . "<br />Phone: " . $phone);
print("</div>");
 
$i++;
}
 
// NEXT AND PREVIOUS PAGE SETUP \\
 
$nextpage=$page+1;
$prevpage=$page-1;
 
if ($page == 1) {
print("<a href=view.php?page=$nextpage>Next Page</a>");
print("<br />");
}
 
?>

Open in new window

0
Comment
Question by:ValleyENT
7 Comments
 
LVL 14

Expert Comment

by:shobinsun
ID: 24234825
Hi,

Do you want this:

$query="SELECT * FROM pdf1 ORDER BY name LIMIT $start";

Try with it.

regards
0
 
LVL 20

Expert Comment

by:NerdsOfTech
ID: 24235161
Do a full count first :)
<?php 
// WRITTEN BY Valley ENT \\
 
 
// CONNECT TO DATABASE \\
...........
 
// PAGINATION CONFIGURATION \\
$page = mysql_real_escape_string($_GET['page']);
$howmany = 3;
$start = $page * $howmany;
 
// QUERY CONFIGURATION \\
$query="SELECT count(*) FROM ual;
$result=mysql_query($query);
$count=mysql_numrows($result);
$pagecount = ceil($count/3);
mysql_free_result ($result);
 
$query="SELECT * FROM ual ORDER BY cname LIMIT $start, $howmany";
$result=mysql_query($query);
$num=mysql_numrows($result);
$last=ceil($num/$howmany);
 
// CLOSE DATABASE CONNECTION \\
mysql_close();
 
// SEETING CSS STYLE TO PAGE \\
print("<link href=../../default.css rel=stylesheet type=text/css />");
 
// REQUESTING DATABASE INFO \\
$i=0;
while ($i < $num) {
$cname=mysql_result($result,$i,"cname");
$description=mysql_result($result,$i,"description");
$phone=mysql_result($result,$i,"phone");
$website=mysql_result($result,$i,"website");
 
// PRINTING DATABASE RESULTS \\
print("<div id=content>");
print("<b>" . "<a href=$website target=_blank>$cname</a>" . "</b><br />" . $description . "<br />Phone: " . $phone);
print("</div>");
 
$i++;
}
 
// NEXT AND PREVIOUS PAGE SETUP \\
 
$nextpage=$page+1;
$prevpage=$page-1;
 
if ($page == 1) {
 print("<a href=view.php?page=$nextpage>Next Page</a>");
 print("<br />");
}
 
?>

Open in new window

0
 
LVL 20

Expert Comment

by:NerdsOfTech
ID: 24235164
Typo! USE THIS INSTEAD
<?php 
// WRITTEN BY Valley ENT \\
 
 
// CONNECT TO DATABASE \\
...........
 
// PAGINATION CONFIGURATION \\
$page = mysql_real_escape_string($_GET['page']);
$howmany = 3;
$start = $page * $howmany;
 
// QUERY CONFIGURATION \\
$query="SELECT count(*) FROM ual";
$result=mysql_query($query);
$count=mysql_numrows($result);
$pagecount = ceil($count/3);
mysql_free_result ($result);
 
$query="SELECT * FROM ual ORDER BY cname LIMIT $start, $howmany";
$result=mysql_query($query);
$num=mysql_numrows($result);
$last=ceil($num/$howmany);
 
// CLOSE DATABASE CONNECTION \\
mysql_close();
 
// SEETING CSS STYLE TO PAGE \\
print("<link href=../../default.css rel=stylesheet type=text/css />");
 
// REQUESTING DATABASE INFO \\
$i=0;
while ($i < $num) {
$cname=mysql_result($result,$i,"cname");
$description=mysql_result($result,$i,"description");
$phone=mysql_result($result,$i,"phone");
$website=mysql_result($result,$i,"website");
 
// PRINTING DATABASE RESULTS \\
print("<div id=content>");
print("<b>" . "<a href=$website target=_blank>$cname</a>" . "</b><br />" . $description . "<br />Phone: " . $phone);
print("</div>");
 
$i++;
}
 
// NEXT AND PREVIOUS PAGE SETUP \\
 
$nextpage=$page+1;
$prevpage=$page-1;
 
if ($page == 1) {
 print("<a href=view.php?page=$nextpage>Next Page</a>");
 print("<br />");
}
 
?>

Open in new window

0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 20

Accepted Solution

by:
NerdsOfTech earned 1500 total points
ID: 24235194
LOGIC ERROR

USE THIS INSTEAD:

=NerdsOfTech
<?php 
// WRITTEN BY Valley ENT \\
 
 
// CONNECT TO DATABASE \\
...........
 
// PAGINATION CONFIGURATION \\
$page = mysql_real_escape_string($_GET['page']);
$howmany = 3;
$start = $page * $howmany;
 
// QUERY CONFIGURATION \\
$query="SELECT count(*) as rows FROM ual";
$result=mysql_query($query);
$row = mysql_fetch_row($result);
$count = $row[0];
$pagecount = ceil($count/3);
mysql_free_result ($result);
 
$query="SELECT * FROM ual ORDER BY cname LIMIT $start, $howmany";
$result=mysql_query($query);
$num=mysql_numrows($result);
$last=ceil($num/$howmany);
 
// CLOSE DATABASE CONNECTION \\
mysql_close();
 
// SEETING CSS STYLE TO PAGE \\
print("<link href=../../default.css rel=stylesheet type=text/css />");
 
// REQUESTING DATABASE INFO \\
$i=0;
while ($i < $num) {
$cname=mysql_result($result,$i,"cname");
$description=mysql_result($result,$i,"description");
$phone=mysql_result($result,$i,"phone");
$website=mysql_result($result,$i,"website");
 
// PRINTING DATABASE RESULTS \\
print("<div id=content>");
print("<b>" . "<a href=$website target=_blank>$cname</a>" . "</b><br />" . $description . "<br />Phone: " . $phone);
print("</div>");
 
$i++;
}
 
// NEXT AND PREVIOUS PAGE SETUP \\
 
$nextpage=$page+1;
$prevpage=$page-1;
 
if ($page == 1) {
 print("<a href=view.php?page=$nextpage>Next Page</a>");
 print("<br />");
}
 
?>

Open in new window

0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 24236446
Hey, NerdsOfTech: I thought I was the only one who did that! ;-)

There is a good article on pagination over at SitePoint
http://www.sitepoint.com/article/perfect-php-pagination/

Best to all, ~Ray
0
 
LVL 20

Expert Comment

by:NerdsOfTech
ID: 24237631
Hi Ray! I really like your style. Your answers are really descriptive and I enjoy when you are on the thread. Hope to see your post more often. You are very valuable to Experts Exchange!

Your colleague,
NerdsOfTech
0
 
LVL 4

Author Closing Comment

by:ValleyENT
ID: 31574612
This worked!

0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The title says it all. Writing any type of PHP Application or API code that provides high throughput, while under a heavy load, seems to be an arcane art form (Black Magic). This article aims to provide some general guidelines for producing this typ…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses
Course of the Month14 days, 2 hours left to enroll

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question