I am trying to implement paging and i done that. The problem is when I click the search button the search results are displayed with no problem.
1st Snap http://www.flickr.com/photos/41695354@N08/4057149857/
2nd snap http://www.flickr.com/photos/41695354@N08/4057149861/
When i click the next button, it once again gets the value from all the textboxes which is empty by default and sets the query as select * from rentals_ads where city="" which is 0 result, and so it displaying 'No Results Found'.
3rd snap http://www.flickr.com/photos/41695354@N08/4057149863/
How can i make the query string to form only at the time of clicking the search button??.
Any help will be highly appreciated.
My code:
rentals_search.template.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<link rel="stylesheet" type="text/css" href="../css/style.css"> </link>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>RBlog - For the Consumers - By the Sellers</title>
<link href="css/Pager.css" rel="stylesheet" type="text/css" />
<script src="js/jquery-1.2.6.min.js" type="text/javascript"></script>
<script src="js/jquery.pager.js" type="text/javascript"></script>
<script type="text/javascript" language="javascript">
$(document).ready(function() {
$("#pager").pager({ pagenumber: 1, pagecount: 15, buttonClickCallback: PageClick });
});
PageClick = function(pageclickednumber) {
$("#pager").pager({ pagenumber: pageclickednumber, pagecount: 10, buttonClickCallback: PageClick });
//$("#result").html("Clicked Page " + pageclickednumber);
$("#result").html("Clicked Page " + pageclickednumber);
}
gen();
</script>
</head>
<body>
<?php include("includes/header.php"); ?>
<?php include("dbclass/paging_class.php"); ?>
<div class="afterpicer_total">
<?php include("includes/menu.php"); ?>
<div class="wrapper">
<div class="cont">
<?php include("includes/left_menu.php"); ?>
<div class="reg_cont">
<form action="search_results.php" method="GET">
<div class="reg_label">Looking for</div>
<div class="reg_tbox">
<select name="type" class="reg_combo_style">
<option>Living House</option>
<option>Office</option>
</select>
</div><br/>
<div class="reg_label">Rent/Month</div>
<div class="reg_tbox">
<select name="rent" class="reg_combo_sty开发者_JAVA百科le">
<option></option>
<option><2000</option>
<option>2000-4000</option>
<option>4000-6000</option>
<option>6000-10000</option>
<option>>10000</option>
</select>
</div><br/>
<div class="reg_label" >City</div>
<div class="reg_tbox">
<select name="city" class="reg_combo_style" >
<option></option>
<option>Chennai</option>
<option>Salem</option>
<option>Madurai</option>
<option>Trichy</option>
</select>
</div><br/>
<div class="reg_label" >Area</div>
<div class="reg_tbox"><input type="text" size="32" name="area" class="reg_tbox_style" value='<?php echo(htmlspecialchars($_SESSION["lastarea"], ENT_QUOTES)); ?>' ></input></div><br/><br/><br/>
<div class="reg_tbox"><input type="submit" name="subm" value="Search" class="reg_but_style"style="margin-left:155px;"></input></div><br/><br/>
<?php
//doPages(10, '/rentals_search.php', 'city=Madurai', 5);
/*if (isset($_GET['subm'])) {
$_SESSION['searchflag'] = 'on';
}
if(isset($_SESSION['searchflag']))
{
$newpage=new paging();
//$newpage->pager(" SELECT * FROM rentals_ads WHERE city='Salem'");
$type=$_GET[type];
$city=$_GET[city];
$newpage->pager("SELECT * FROM rentals_ads WHERE city='$city'");
} */
?>
<div class="reg_bl"></div>
</form>
</div>
</div>
<div class="adspace"> Advertisement Space</div>
</div>
</div>
<script language="javascript">
function gen()
{
for (var page = 0; page <= 10; page++) {
var currentButton = $('<li class="page-number">' + (page) + '</li>');
<a class="pager" href="/ajax_pages/get_results.php?page=3">page </a>
}
}
</script>
</body>
</html>
paging_class.php
<?php
ob_start();
require_once("common/class.Database.php");
require_once("common/varDeclare.php");
class paging extends Database
{
function pager()
{
$no_results = TRUE; // No results found yet
$howmany = 5; // Return 10 results per query
// Set default starting point of query to 0, or, if set, to $_GET['rs']
$row_start = (isset($_GET['rs'])) ? $_GET['rs'] : 0;
// Do our SQL query, with something like LIMIT 0, 10
$qcity=$_GET[city];
$sql="SELECT * FROM rentals_ads WHERE city='$qcity'";
$result=mysql_query($sql);
while($row = mysql_fetch_array($result))
{
$count=$count+1;
}
if($count>0)
{
echo "$count results found" ;
}
$sql = "SELECT * FROM rentals_ads WHERE city='$qcity' LIMIT ". $row_start .", ". $howmany ."";
$result = mysql_query($sql);
// Get the number of rows that would have been returned WITHOUT a limit clause, to be used later for paging.
$count_sql = "SELECT FOUND_ROWS() AS total";
$count_sql_result = mysql_query($count_sql);
$count_row = mysql_fetch_array($count_sql_result);
$count_result = $count_row['total'];
?>
<div class="paging_title">
<div class="paging_title_title"> Title</div>
<div class="paging_title_type">Room Type</div>
<div class="paging_title_city">City</div>
<div class="paging_title_rent">Rent</div>
</div>
<?
// Start looping through our result set
while($row = mysql_fetch_array($result)) {
$no_results = FALSE;
// Save results of query to $line_output
$line_output .= "
<div class=\"paging_ad\">
<div class=\"paging_ad_title\">". $row['rentals_title'] ."</div>
<div class=\"paging_ad_type\">". $row['rentals_type'] ."</div>
<div class=\"paging_ad_city\">". $row['city'] ."</div>
<div class=\"paging_ad_rent\">". $row['rent'] ."</div>
</div>";
}
// Don't bother building paging if we don't have records
if ($no_results) {
$line_output = "No records found...";
$page_output = "";
}
else {
// Build <prev> and <next> links and save to $page_output
$rs_prev = $row_start - $howmany; // where would prev page start, given current start less no. of records
$rs_next = $row_start + $howmany; // where would next page start, given current start plus no. of records
// If for some reason the next <prev> starting point is negative, do not display <prev>
// This happens when our current starting point is already 0
// This may happen if some smartass manually changes the rs= bit in the url
$page_output_prev = ($rs_prev < 0) ? "" : "<a href='?rs=".$rs_prev."'>Previous</a>";
// Will the next page jump start point exceed the number of records returned?
// If so, don't display <next>'
$page_output_next = ($rs_next <= $count_result) ? "<a href='?rs=".$rs_next."'>Next</a>": "";
// Just something to put between <prev> & <next>, IF they are both active
if (($page_output_prev == ""))
{
$page_output =" <div class=\"paging_nav_cont\">
<div class=\"paging_nav_but\">". $page_output_next ."</div>
</div>";
}
else if (($page_output_next == ""))
{
$page_output =" <div class=\"paging_nav_cont\">
<div class=\"paging_nav_but\">". $page_output_prev ."</div>
</div>";
}
else
{
$page_output =" <div class=\"paging_nav_cont\">
<div class=\"paging_nav_but\">". $page_output_prev ."</div>
<div class=\"paging_nav_but\">". $page_output_next ."</div>
</div>";
}
// Build final paging output
//$page_output = $page_output_prev . $page_output_breaker . $page_output_next;
}
// Write the outputs
echo $line_output;
echo $page_output;
}
}
?>
search_results.template.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>RBlog - For the Consumers - By the Sellers</title>
<?
require_once("common/class.Database.php");
require_once("dbclass/paging_class.php");
?>
</head>
<body>
<?php include("includes/header.php"); ?>
<div class="afterpicer_total">
<?php include("includes/menu.php"); ?>
<div class="wrapper">
<div class="cont">
<?
$newpage=new paging();
//$newpage->pager(" SELECT * FROM rentals_ads WHERE city='Salem'");
$type=$_GET[type];
$city=$_GET[city];
$newpage->pager();
?>
</div>
<div class="adspace"> Advertisement Space</div>
</div>
</div>
</body>
</html>
No, Scavenger is right, you have to use the $_SESSION variable. It is the only way to make PHP remember things from the previous page load.
The reason it was not working for you is probably because you haven't set it up correctly. You have to add
<?php session_start(); ?>
to the beginning of each PHP file in which you want to use the $_SESSION variable. Then, you have to code something like:
if (isset($_GET['city']) {
$qcity = $_GET['city'];
$_SESSION['city'] = $qcity;
} else {
$qcity = $_SESSION['city'];
}
to make it remember the city from last time.
This is one way to tackle the problem, I think you could also solve the problem with AJAX. If you do that, you have to make sure that the page is never reloaded completely. This keeps the input fields valid. Instead of reloading, you call your PHP files (containing database queries) from Javascript, and manipulate the page accordingly.
I'd go for PHP sessions (but I'm dying to learn more AJAX too, it's neat :-).
The $_SESSION PHP variable may be what you are looking for. It stores values between page loads. PHP manual
精彩评论