/    Sign up×
Community /Pin to ProfileBookmark

Multiple paged search results…

Hey.. i was wondering if someone could give me an example of how i would make it so that search results from a search query could be placed on multiple pages and when a next page is clicked it moves on to the next set of results.

I know that you have to use a LIMIT, but i dont knw how i would make it so that, when you clicked on the next page link, it would display the next set of results.

Thanks.

to post a comment
PHP

1 Comments(s)

Copy linkTweet thisAlerts:
@ShrineDesignsMay 24.2004 — here is a simple example:<?php
function pg()
{
return (isset($_GET['pg']) && !empty($_GET['pg'])) ? $_GET['pg'] : 1;
}
function ppg()
{
return (isset($_GET['ppg']) && !empty($_GET['ppg'])) ? $_GET['ppg'] : 10;
}

if($_GET['search'])
{
$keywords = explode(' ', $_GET['search']);
$where = "";

<i> </i>foreach($keywords as $key)
<i> </i>{
<i> </i> $where .= "<span><code>field_name</code></span> LIKE '%{$key}%'";
<i> </i> $where .= ($key != end($keywords)) ? " OR " : '';
<i> </i>}
<i> </i>$link_id = mysql_pconnect('localhost', $username, $password);
<i> </i>mysql_select_db($database, $link_id);
<i> </i>$result = mysql_query("SELECT * FROM <span><code>table_name</code></span> WHERE $where LIMIT ".(pg() - 1) * ppg().",".ppg(), $link_id);

<i> </i>while($row = mysql_fetch_array($result, MYSQL_NUM))
<i> </i>{
<i> </i> // output results
<i> </i>}
<i> </i>mysql_free_result($result);
<i> </i>$count = mysql_query("SELECT COUNT(*) FROM <span><code>table_name</code></span> WHERE $where", $link_id);
<i> </i>$rows = mysql_result($count, 0, 0);
<i> </i>$term = str_replace(' ', '+', $_GET['search']);

<i> </i>if($rows &gt; ppg())
<i> </i>{
<i> </i> echo "[";

<i> </i> for($i = 1; $i &lt;= ceil($rows / ppg()); $i++)
<i> </i> {
<i> </i> echo ($i == pg()) ? " $i " : " &lt;a href="{$_SERVER['PHP_SELF']}?search={$search}&amp;amp;pg=".pg()."&amp;amp;ppg=".ppg().""&gt;$i&lt;/a&gt; ";
<i> </i> echo ($i &lt; ceil($rows / ppg())) ? "|" : "]";
<i> </i> }
<i> </i>}
}
?&gt;
×

Success!

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