/    Sign up×
Community /Pin to ProfileBookmark

How can I make new pages for database queries? (Like a search engine)

On my website I have Sockigami models, and for [URL=http://sockigami.com/models/category/entertainment]this[/URL] page (displaying all of the models), I would like to make the user click “next page” for every 10 models or so (it’s not done yet, the list will have more information in it). Can someone give me some example code for how to do this?

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
@TaschenSep 22.2006 — Yep! It's called pagination.

First: decide how many results you want per page and asign a $limit with the value

Then: using the count() function to count the total number of records limit the results using the $limit variable.

Finally: print results to page using the count() and $limit values to move forwards and backwards.

[code=php]
$limit = 10;
$start = $_GET['start']; // one way or another get a start position
//query the db to get total records
$query = "SELECT COUNT(*) FROM table WHERE published = 1 [etc]";
$result = mysql_query($query);
$row = mysql_fetch_row($result);
$total = $row[0];
//Don't forget some error control and flush results around here

//New query to get the actual records
$query = "SELECT something FROM somewher WHERE condition LIMIT $start, $limit";

//execute query etc ($result = mysql_query($query);) and iterate over your //result set (if any!)
[/code]

Next you will need a forward and back link. No problem.
[code=php]
if($start >= $limit){
echo "<a href="mypage.php?start=".($start-$limit."">previous</a>";
}
//next link
if($start + $limit < $total && $start >= 0 ){
echo "<a href="mypage.php?start=".($start+$limit."">next</a>";
}
[/code]


Fill yer boots on that!
×

Success!

Help @chesemonkyloma 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.18,
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: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

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