每次加载时自动执行搜索查询,无需点击搜索按钮
我希望我的代码根据搜索关键字文本框中的值执行查询。但当前代码每次加载时都会执行默认查询Help。任何帮助将不胜感激。请帮助
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>
<?
require_once("common/class.Database.php");
?>
</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="<? echo $_SERVER['PHP_SELF']; ?>" method="post">
<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_style">
<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" ></input></div><br/><br/>
<?php
//doPages(10, '/rentals_search.php', 'city=Madurai', 5);
$newpage=new paging();
$newpage->pager();
?>
<div class="reg_tbox"><input type="submit" name="subm" value="Search" class="reg_but_style"style="margin-left:155px;"></input></div>
<div class="reg_bl"></div>
</form>
</div>
</div>
<div class="adspace"> Advertisement Space</div>
</div>
</div>
</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()
{
$rowsPerPage = 3;
// by default we show first page
$pageNum = 1;
// if $_GET['page'] defined, use it as page number
if(isset($_GET['page']))
{
$pageNum = $_GET['page'];
}
// counting the offset
$offset = ($pageNum - 1) * $rowsPerPage;
$query = " SELECT * FROM rentals_ads" ." LIMIT $offset, $rowsPerPage";
//$query="SELECT * FROM ads WHERE rentals_type = 'Living House' AND rent > 50 AND rent < 300 AND city = 'London' AND area LIKE '%westminster%'"." LIMIT $offset, $rowsPerPage";
$result = mysql_query($query) or die('Error, query1 failed');
// print the random numbers
while($row = mysql_fetch_array($result))
{
echo $row['rentals_title'] . '';
echo $row['rentals_type'].' ';
echo $row['city'].'<br>';
}
// ... more code here
// ... the previous code
// how many rows we have in database
$query = "SELECT COUNT(rentals_title) AS numrows FROM rentals_ads";
$result = mysql_query($query) or die('Error, query2 failed');
$row = mysql_fetch_array($result, MYSQL_ASSOC);
$numrows = $row['numrows'];
// how many pages we have when using paging?
$maxPage = ceil($numrows/$rowsPerPage);
// print the link to access each page
$self = $_SERVER['PHP_SELF'];
$nav = '';
for($page = 1; $page <= $maxPage; $page++)
{
if ($page == $pageNum)
{
$nav .= " $page "; // no need to create a link to current page
}
else
{
$nav .= " <a href=\"$self?page=$page\">$page</a> ";
}
}
// ... still more code coming
// ... the previous code
// creating previous and next link
// plus the link to go straight to
// the first and last page
if ($pageNum > 1)
{
$page = $pageNum - 1;
$prev = " <a href=\"$self?page=$page\">[Prev]</a> ";
$first = " <a href=\"$self?page=1\">[First Page]</a> ";
}
else
{
$prev = ' '; // we're on page one, don't print previous link
$first = ' '; // nor the first page link
}
if ($pageNum < $maxPage)
{
$page = $pageNum + 1;
$next = " <a href=\"$self?page=$page\">[Next]</a> ";
$last = " <a href=\"$self?page=$maxPage\">[Last Page]</a> ";
}
else
{
$next = ' '; // we're on the last page, don't print next link
$last = ' '; // nor the last page link
}
// print the navigation link
echo $first . $prev . $nav . $next . $last;
// and close the database connection
// ... and we're done!
}
}
?>
// ---- End of addDB()------
I want my code to execute a query based on the values in the search keyword text boxes. But the current code executes the default query each time at the time of loading, Help. Any help will be appreciated. Plz help
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>
<?
require_once("common/class.Database.php");
?>
</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="<? echo $_SERVER['PHP_SELF']; ?>" method="post">
<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_style">
<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" ></input></div><br/><br/>
<?php
//doPages(10, '/rentals_search.php', 'city=Madurai', 5);
$newpage=new paging();
$newpage->pager();
?>
<div class="reg_tbox"><input type="submit" name="subm" value="Search" class="reg_but_style"style="margin-left:155px;"></input></div>
<div class="reg_bl"></div>
</form>
</div>
</div>
<div class="adspace"> Advertisement Space</div>
</div>
</div>
</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()
{
$rowsPerPage = 3;
// by default we show first page
$pageNum = 1;
// if $_GET['page'] defined, use it as page number
if(isset($_GET['page']))
{
$pageNum = $_GET['page'];
}
// counting the offset
$offset = ($pageNum - 1) * $rowsPerPage;
$query = " SELECT * FROM rentals_ads" ." LIMIT $offset, $rowsPerPage";
//$query="SELECT * FROM ads WHERE rentals_type = 'Living House' AND rent > 50 AND rent < 300 AND city = 'London' AND area LIKE '%westminster%'"." LIMIT $offset, $rowsPerPage";
$result = mysql_query($query) or die('Error, query1 failed');
// print the random numbers
while($row = mysql_fetch_array($result))
{
echo $row['rentals_title'] . '';
echo $row['rentals_type'].' ';
echo $row['city'].'<br>';
}
// ... more code here
// ... the previous code
// how many rows we have in database
$query = "SELECT COUNT(rentals_title) AS numrows FROM rentals_ads";
$result = mysql_query($query) or die('Error, query2 failed');
$row = mysql_fetch_array($result, MYSQL_ASSOC);
$numrows = $row['numrows'];
// how many pages we have when using paging?
$maxPage = ceil($numrows/$rowsPerPage);
// print the link to access each page
$self = $_SERVER['PHP_SELF'];
$nav = '';
for($page = 1; $page <= $maxPage; $page++)
{
if ($page == $pageNum)
{
$nav .= " $page "; // no need to create a link to current page
}
else
{
$nav .= " <a href=\"$self?page=$page\">$page</a> ";
}
}
// ... still more code coming
// ... the previous code
// creating previous and next link
// plus the link to go straight to
// the first and last page
if ($pageNum > 1)
{
$page = $pageNum - 1;
$prev = " <a href=\"$self?page=$page\">[Prev]</a> ";
$first = " <a href=\"$self?page=1\">[First Page]</a> ";
}
else
{
$prev = ' '; // we're on page one, don't print previous link
$first = ' '; // nor the first page link
}
if ($pageNum < $maxPage)
{
$page = $pageNum + 1;
$next = " <a href=\"$self?page=$page\">[Next]</a> ";
$last = " <a href=\"$self?page=$maxPage\">[Last Page]</a> ";
}
else
{
$next = ' '; // we're on the last page, don't print next link
$last = ' '; // nor the last page link
}
// print the navigation link
echo $first . $prev . $nav . $next . $last;
// and close the database connection
// ... and we're done!
}
}
?>
// ---- End of addDB()------
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当表单的方法是
POST
时,您似乎正在Pager
类的$_GET
中查找值。您需要更改其中之一,以便他们使用相同的。如果您想在页面提交之前禁用搜索,您可以尝试在执行搜索的代码周围添加
if
:编辑:解决 OP 的注释。
It looks like you're looking for values in
$_GET
in yourPager
class when your form's method isPOST
. You'll need to change one or the other so that they're using the same.If you want to disable the search until the page has been submitted, you can try adding an
if
around the code that executes the search:Edit: addressed OP's comment.