/    Sign up×
Community /Pin to ProfileBookmark

Restricting display of rows to 20 ?

With most db driven websites the output on the first page is limited to the first 15 or 20 records.
This obviously loads the page quicker and puts less strain on the server

I want to do the same, but am not sure how to do it.

The first page is easy of course just using a counter but for subsequent pages
how do I restart the reading rows from where it left off ?

If, for the sixth page, I have to read the first 100 rows, won’t it slow things down?
Do people use markers ? Or can you just tell mysql to start at row 100 and go for
another 20 rows i.e. put in a range ?

Notice that I have an ORDER which is selected y the user.

My coding looks like this:


—————————————————————

$sql = “SELECT * FROM stuff ORDER BY $sorter”;

$result = mysql_query($sql)
or die (“Could not 1st execute query.”);

$top = 190; // this is for positioning on the screen

while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
extract($row);
require(“stuff_list.php”); // this program displays the row
$top = $top + 30;
}


——————————————————————-

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
@jim_boJan 30.2005 — Hey,

Look into [URL=http://www.phpfreaks.com/tutorials/73/1.php]Pagination[/URL] ..
Copy linkTweet thisAlerts:
@ShrineDesignsJan 30.2005 — a simple equation,

starting row: (page_number - 1) * rows_per_page

number of rows: rows_per_page

example[code=php]<?php
$pg = (isset($_GET['pg']) && !empty($_GET['pg'])) ? $_GET['pg'] : 1;
$ppg = 20; // you can if you choose to set this in the url like above

$db = mysql_connect('localhost', 'root', '');
mysql_select_db('dbname', $db);

$result = mysql_query("SELECT * FROM stuff ORDER BY $sorter ASC LIMIT " . ($pg - 1) * $ppg . "," . $ppg);

while($row = mysql_fetch_array($result, 2))
{
// output results ...
}
mysql_free_result($result);

// propegate page navigation ...
?>[/code]
and if you are using this for a search page, you can use SQL_CALC_FOUND_ROWS and FOUND_ROWS(), for improved proformance, see: http://dev.mysql.com/doc/mysql/en/information-functions.html#id3139469
×

Success!

Help @DaveinLondon 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,
)...