/    Sign up×
Community /Pin to ProfileBookmark

Search + pagination

Hi all,

I have build up query through a search form and giving the desired results.The search is fine.The values are coming through a POST method from another page to search.php.

Now i want to make a pagination based on the search result in search.php.Also I have done pagination before based on url based value.I mean after passing a value through url I have grabbed that value and put it in the sql.

Now the problem is that i am getting values from a post method and when i am clicking the “next” link(in the pagination) the query is not getting the posted values further and thus showing a sql error.

How can I keep the posted values in that page i mean inside the query so that when i click next or previous link in the pagination the query get its value.

Please suggest me……….Thanks……….Raj

to post a comment
PHP

22 Comments(s)

Copy linkTweet thisAlerts:
@PineSolPirateJan 09.2007 — Well, I don't know of any way to do a POST using a link, but you could stuff the query information (search parameters) into some session variables and run it that way.
Copy linkTweet thisAlerts:
@raj_2006authorJan 09.2007 — Hi

Thanks for your post.Is there any other way besides keeping the posted values in a session.

I think (passing the params through the URL and then GET and then keep it in the pagination links) and (using sessions) there are no other way to keep the posted values on that page itself for doing pagination.

Please suggest if there is any other tricky/short cut way coz i am running about 32 queries...so now if i use sessions for 32 times then really it will be a hard job.....but if there is no other way then i have to do it.

Thanks

Raj
Copy linkTweet thisAlerts:
@bokehJan 09.2007 — Why are you using the POST method at all? Why do you need to do the search after a POST submission? Is it related somehow?
Copy linkTweet thisAlerts:
@raj_2006authorJan 09.2007 — If I dont use POST then how can i access the values in the search.php.I am submitting the form using POST method.

Is it possible to do search without POST submission.....Really i dont have this idea.

Currently what I am doing is running query based on the values I am getting after POST submission and then running the queries.
Copy linkTweet thisAlerts:
@bokehJan 09.2007 — If I dont use POST then how can i access the values in the search.php.[/QUOTE]According to [URL=http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html]RFC2616[/URL] when the side-effects of N > 0 identical requests is the same as for a single request (A.K.A an [URL=http://en.wikipedia.org/wiki/Idempotence_%28computer_science%29]idempotent[/URL] request) the GET method should be used.
Copy linkTweet thisAlerts:
@NightShift58Jan 09.2007 — Let me see if I understand.

(1) You have a page with a form where the user enters different search items.

(2) The user clicks on the "submit" button and goes to "search.php".

(3) In "search.php", you run one or more a queries based on the search items that were POSTed on the form.

(4) Depending on the number of records returned by the query, you want to paginate through the list using links or buttons like "Next" and "Previous".

If 1-4 are correct, then you could do something like:[code=php]<?php
// search.php
session_start();
if (!isset($_POST['submit']) AND !isset($_GET['page'])) {
print "Where are you coming from...";
exit;
}

if (isset($_POST['submit'])) {
$_SESSION['search1'] = isset($_POST['search1']) ? $_POST['search1'] : "";
$_SESSION['search2'] = isset($_POST['search2']) ? $_POST['search2'] : "";
...
$thisPAGE = 1;
} else {
$thisPAGE = isset($_GET['page'] ? $_GET['page'] : 1;
}

$thisSEARCH1 = $_SESSION['search1'];
$thisSEARCH2 = $_SESSION['search2'];
...
$begREC = ...
$endREC = ...

$sql = "SELECT * FROM mytable WHERE search1 = '$thisSEARCH1' and search2 = '$thisSEARCH2' LIMIT $begREC, $endREC";
...
[/code]
You'll have to modify to fit your data and script flow, but something like would work. The idea is to transfer $_POST to $_SESSION and "remember" what the user is searching for over more than one page.
Copy linkTweet thisAlerts:
@bokehJan 09.2007 — [QUOTE=NightShift58](3) In "search.php", you run one or more a queries based on the search items that were POSTed on the form.[/QUOTE]NightShift, what I was trying to express with my previous post is that it is incorrect to be using POST or starting a session if the only purpose of the request is data retrieval. What needs to be answered by the OP is why he is using the POST method in the first place.
Copy linkTweet thisAlerts:
@NightShift58Jan 09.2007 — I have no disagreement with you or the RFC on that matter.

I was still writing my post as you posted yours, so it may seem that my reply is to you. I was addressing the previous one. I just write slower...

I'm not sure I understand what Raj wants. I posted 4 assumptions and based on those, a basic script flow.
Copy linkTweet thisAlerts:
@bokehJan 09.2007 — I just write slower...[/QUOTE]Code always takes longer to write than a quick answer but... can you imagine if Google started a session for each new search?
Copy linkTweet thisAlerts:
@NightShift58Jan 09.2007 — can you imagine if Google[/quote]Well, there is search and then there is search... He mentions 32 queries...

For Gsearch, GET is sufficient; in other areas - Adwords, AdSense, Gmail, Gsheets, etc. - I'm being sessioned.

As far as Raj is concerned, I don't even know what data he's pushing through. Is it less than 2K? Don't know.

All I know is that he's got a <form>, from which initial data comes - just like Google: <form action="/search" name=f>, which defaults to GET. If he's sure he'll stay under the 2K horizon, he can push via GET. If not...

I don't have too many qualms, either way.
Copy linkTweet thisAlerts:
@bokehJan 09.2007 — For Gsearch, GET is sufficient; in other areas - Adwords, AdSense, Gmail, Gsheets, etc. - I'm being sessioned.[/QUOTE]That's because the latter ones are non-idempotent requests.
Copy linkTweet thisAlerts:
@NightShift58Jan 09.2007 — Ummm.... Sounds pretty bad. Don't they make Vi*gr* for this kind of stuff? Just kidding.

Raj will have to address that issue with his data. I have a gut feeling, though, that what he refers to as "search" is not necessarily a search in the Google sense.
Copy linkTweet thisAlerts:
@raj_2006authorJan 11.2007 — Hi

Sorry to reply in late.

Well all at last i have used sessioned to keep the posted values in that same page.....and its working....But this is not the thing i wanted...I wanted to keep the values there without using session...

Now what i want to know is if i write in index.php

<form action="search.php" method="get">

......................

.......................

</form>

then in search.php what i will do

just $a=$_GET['b']; and it will keep the value of $a in that page if i click the next page in the pagination link.

Please give me an tiny example of GET.I used to do GET through URL but have not used GET while using form.

also thanks to both of you for your valuable opinion.Just learning a lot.

Thanks

Raj
Copy linkTweet thisAlerts:
@raj_2006authorJan 11.2007 — also i have not understood difference between

1.What is non-idempotent requests

2.what is idempotent requests

Can i have a general example plz.
Copy linkTweet thisAlerts:
@bokehJan 11.2007 — $a=$_GET['b']; and it will keep the value of $a[/QUOTE]You just add that query string to your pagination links. So your initial URL looks something like:[CODE]http://www.domain.com/search.php?search_terms=term1+term2[/CODE]
and the subsequent URLs would look like this:[CODE]http://www.domain.com/search.php?search_terms=term1+term2&page=2[/CODE]
also i have not understood difference between

1.What is non-idempotent requests

2.what is idempotent requests

Can i have a general example plz.[/QUOTE]
Did you not follow the link I posted above?

Idempotent: is a request that does not cause a lasting change. It doesn't matter how many times the request is sent the result will be the same. This is ideal for search engines (data retrieval).

Non-idempotent: this is a request where there is a lasting change and it matters how many times the request is sent. For example if one is booking an airline ticket and the request is sent zero times zero tickets will be booked, if the request is sent ten times ten tickets will be booked.
Copy linkTweet thisAlerts:
@raj_2006authorJan 11.2007 — nice........ i have understood almost.Let me clear it plz.

[CODE]http://www.domain.com/search.php?search_items=salary+age[/CODE]

<form method="get" action="search.php">

......................//salary input

........................//age input

.......................//input type submit name submit

</form>

in search.php

if(isset($_GET['submit']))

{

$salary=$_
GET['salary'];

$age=$_GET['age'];

}

Now i add this with the pagination link

CODE]http://www.domain.com/search.php?search_items=salary+age[/CODE]

so if i do $_GET['salary'] and $_GET['age'] from the above url(though this is done inside the above if statement) then I will get the same value i got from the form.

or i have to do it in this way

CODE]http://www.domain.com/search.php?salary=$salary&age=$age[/CODE] add to the pagination link.
Copy linkTweet thisAlerts:
@bokehJan 11.2007 — Post your SQL query and I'll post you a simple search script with the pagination links done.
Copy linkTweet thisAlerts:
@raj_2006authorJan 11.2007 — I will post it asap....just doing a small stuff here...once it is done I will concentrate in search.

Thanks

Raj
Copy linkTweet thisAlerts:
@raj_2006authorJan 12.2007 — Hi

The SQL query is something like this.My original sql query is a long list.about 32 queries.I just want to take the idea.

[code=php]if($salary!="")
{
$sql="select * from job_list where salary>='$salary' and salary<='$salary'";
...................
...................
}

if($age!="")
{
$sql="select * from job_list where age>='$age' and age<='$age'";
...................
...................
}[/code]
Copy linkTweet thisAlerts:
@NightShift58Jan 12.2007 — Assume [B]$salary='1000'[/B]. What does this mean:[code=php]$sql="select * from job_list where salary>='$salary' and salary<='$salary'";[/code]My interpretation is that your query will only return records where salary='1000' - not a penny more, not a penny less. Same problem with "age".

32 queries? Are you doing one query per <form> field? Can't you combine - if not all - at least some of them? i.e.[code=php]$sql="SELECT * FROM job_list
WHERE (salary BETWEEN '$salary1' AND '$salary2')
AND (age BETWEEN '$age1' AND '$age2')
AND .........";[/code]
Copy linkTweet thisAlerts:
@bokehJan 12.2007 — So as not to complicate this too much for the moment the following code just uses a simple query and form based on [I]age[/I] which can be expanded once you get it working. The printout presentation is just for demonstration purposes. Just fill in the MySQL connection and table details at the top and it's ready to run.[code=php]<?php

$mysql = mysql_connect('localhost','root','');
mysql_select_db('test');

#start: validate inputs
$page = (!empty($_GET['page']) and (is_numeric($_GET['page'])) and ($_GET['page'] > 1)) ? (int)$_GET['page'] : 1 ;
$age = isset($_GET['age']) ? preg_replace('/[^0-9]/', '', $_GET['age']) : null;
#end: validate inputs

$results_per_page = 10;

$content = '';

if($age)
{
$query = "SELECT COUNT(*) as count ".
"FROM job_list ".
"WHERE (age ".
"BETWEEN '".($age - 5)."' ".
"AND '".($age + 5)."')";

$result = mysql_query($query);
$row = mysql_fetch_assoc($result);
$num_rows = $row['count'];
$total_pages = $num_rows ? ceil($num_rows / $results_per_page) : 1 ;
$page = ($page <= $total_pages) ? (int)$page : 1 ;

$query = "SELECT * ".
"FROM job_list ".
"WHERE (age ".
"BETWEEN '".($age - 5)."' ".
"AND '".($age + 5)."')".
"ORDER BY age ASC ".
"LIMIT $results_per_page ".
"OFFSET ".(($results_per_page * $page) - $results_per_page);

$content = printout($query);
$content .= pagination_links($page, $num_rows, $results_per_page);

}

?><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">

<html lang="en">
<head>

<meta http-equiv="content-type" content="text/html; charset=iso-8859-1">

<style type="text/css">
table.mysql_table caption{
color: #008;
font-size: 9pt;
font-family: verdana, sans-serif;
margin: 0 0 5px 0;
white-space: nowrap;
}

table.mysql_table th{
color: #008;
font-size: 8pt;
font-family: verdana, sans-serif;

white-space: nowrap;
}

table.mysql_table td{
color: #008;
font-size: 8pt;
font-family: verdana, sans-serif;
}

p.pagination-links{
clear:both;
margin:0;
padding:2em 0 1em 0;
text-align:center;
}
</style>

<title>Search</title>

</head>

<body>

<form action="" method="get">
<p>
Age:
<input type="text" name="age" value="<?php echo(isset($_GET['age'])?$_GET['age']:'') ?>">
<input type="submit" value="Search">
</p>
</form>

<?php echo $content ?>

</body>
</html><?php

function pagination_links($page, $num_rows, $results_per_page, $each_direction = 5)
{
$word_for_previous = 'prev';
$word_for_next = 'next';
$total_pages = $num_rows ? ceil($num_rows / $results_per_page) : 1 ;
if($total_pages < 2)
{
return null;
}
$page = ((is_numeric($page)) and ($page >= 1) and ($page <= $total_pages)) ? (int)$page : 1 ;
$output = null;
$query_string = '';
foreach($_GET as $k => $v)
{
if($k != 'page')
{
$query_string .= get_magic_quotes_gpc() ?
urlencode(stripslashes($k)).'='.urlencode(stripslashes($v)).'&amp;':
urlencode($k).'='.urlencode($v).'&amp;';
}
}
if($page > 1)
{
$output .= '<a href="'.htmlentities($_SERVER['PHP_SELF']).'?'.$query_string.'page='.($page - 1).'">'.$word_for_previous.'</a> | '."n";
}
for($i = $page - $each_direction; $i <= $page + $each_direction; $i++)
{
if(($i > 0) and ($i <= $total_pages))
{
$output .= isset($spacer) ? $spacer : null ;
$spacer = ' | '."n";
if($page != $i)
{
$output .= '<a href="'.htmlentities($_SERVER['PHP_SELF']).'?'.$query_string.'page='.$i.'">'.$i.'</a>'."n";
}
else
{
$output .= $i."n";
}
}
}
if($page < $total_pages)
{
$output .= ' | <a href="'.htmlentities($_SERVER['PHP_SELF']).'?'.$query_string.'page='.($page + 1).'">'.$word_for_next.'</a>'."n";
}
return '<p class="pagination-links">'."n".$output."n".'</p>';
}

function printout($query)
{
$query_result = mysql_query($query) or die (mysql_error());
if(mysql_num_rows($query_result) > 0)
{
$return = '<table class="mysql_table" border="1" cellspacing="0" cellpadding="6">'."n".
'<caption>"Search results"</caption>'."n";
$first_time = true;
while($row = mysql_fetch_assoc($query_result))
{
if($first_time)
{
$first_time = false;
$headings = array_keys($row);
$return .= '<tr>';
foreach($headings as $heading)
{
$return .= '<th>'.$heading.'</th>';
}
$return .= '</tr>'."n";
}
$return .= '<tr>';
foreach($headings as $heading)
{
$return .= '<td>';
$return .= ($row[$heading]) ? $row[$heading] : '&nbsp;';
$return .= '</td>';
}
$return .= '</tr>'."n";
}
$return .= '</table>'."n";
return $return;
}
else
{
return 'No results for that search<br>'."n";
}
}

?>[/code]
Copy linkTweet thisAlerts:
@raj_2006authorJan 12.2007 — Hi nightshift

Sorry its my mistake...it will be salary1 and salary2 2 different variables containing 2 different value

Hi Bokeh

THanks for the code sample.Let me understand this first.Then i will try to apply it.

I will post here if anyline is uncomprehensive to me.
×

Success!

Help @raj_2006 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 6.16,
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: @nearjob,
tipped: article
amount: 1000 SATS,

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

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