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
If I dont use POST then how can i access the values in the search.php.[/QUOTE]According to
[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.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?
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.[/QUOTE]That's because the latter ones are non-idempotent requests.
$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]
[CODE]http://www.domain.com/search.php?search_terms=term1+term2&page=2[/CODE]
also i have not understood difference betweenDid you not follow the link I posted above?
1.What is non-idempotent requests
2.what is idempotent requests
Can i have a general example plz.[/QUOTE]
[CODE]http://www.domain.com/search.php?search_items=salary+age[/CODE]
[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]
[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".[code=php]$sql="SELECT * FROM job_list
WHERE (salary BETWEEN '$salary1' AND '$salary2')
AND (age BETWEEN '$age1' AND '$age2')
AND .........";[/code]
[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)).'&':
urlencode($k).'='.urlencode($v).'&';
}
}
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] : ' ';
$return .= '</td>';
}
$return .= '</tr>'."n";
}
$return .= '</table>'."n";
return $return;
}
else
{
return 'No results for that search<br>'."n";
}
}
?>[/code]
0.1.9 — BETA 6.16