/    Sign up×
Community /Pin to ProfileBookmark

generating pages and page numbers

i got a db that i want outputted in a webpage, after every 5 rows i want the next 5 rows outputted on the next page.
what it should look like is this:

on the first page, u got 5 rows of data from the db and at the bottom u have a horizontal list of page numbers from 1 to n. n being the number of pages it takes to display all the elements in the db’s table.

how do i get my php/MySQL script to do this?

to post a comment
PHP

6 Comments(s)

Copy linkTweet thisAlerts:
@chazzyJan 26.2008 — I'm going to say that this is more of a php question...
Copy linkTweet thisAlerts:
@rozillaauthorJan 29.2008 — help?
Copy linkTweet thisAlerts:
@SheldonJan 29.2008 — This is called [url=http://www.google.com/search?hl=en&client=safari&rls=en-us&sa=X&oi=spell&resnum=0&ct=result&cd=1&q=php+pagination&spell=1]Pagination[/url].

You need to query using a start value and the LIMIT SQL function

Here is a VERY Basic example
[code=php]
$start = mysql_real_escape_string($_GET['start']);
if(empty($start)){ $start = 1; }
$limit = 5;
$sql = "SELECT * FROM database ORDER BY id ASC LIMIT {$start}, {$limit}";
$qry = @mysql_query($sql);
$num = @mysql_num_rows($qry);
if($num >= 1){
while($row = mysql_num_rows($qry)){
var_dump($row);
}
echo("<a href="".htmlentities(basename($_SERVER['PHP_SELF']))."?start=".($start+$limit)."">Next {$limit}</a>");
}[/code]

(Not Tested)
Copy linkTweet thisAlerts:
@NogDogJan 29.2008 — There's the [url=http://pear.php.net/package/Pager]PEAR Pager class[/url]. If that is not practical, try a search on "PHP pagination".
Copy linkTweet thisAlerts:
@felgallJan 29.2008 — Printing happens client side and so you would want to insert the appropriate stylesheet commands in order to achieve the page splitting that you want.

Most browsers are already set to number the pages when printing so adding your own numbers is redundant.

The alternative where you do get full control over printing from PHP is to generate a PDF instead of HTML. The easiest way to do that is to use the library at www.fpdf.org
Copy linkTweet thisAlerts:
@SheldonJan 29.2008 — Here is a version that will add Pageination with links in increment of 5's

[code=php]$start = mysql_real_escape_string($_GET['start']);
if(empty($start)){ $start = 1; }
$limit = 5;
$total = @mysql_num_rows(@mysql_query("SELECT * FROM database"));
if($total > 1){
$sql = "SELECT * FROM database ORDER BY id ASC LIMIT {$start}, {$limit}";
$qry = @mysql_query($sql);
$num = @mysql_num_rows($qry);
if($num >= 1){
while($row = mysql_num_rows($qry)){
var_dump($row);
}
}
for($i=1;$i<=$total;($i+$limit)){
echo("[<a href="".htmlentities(basename($_SERVER['PHP_SELF']))."?start={$i}">{$i}</a>]&nbsp;");
}
}[/code]
again, not tested.
×

Success!

Help @rozilla spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.4,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...