搜索查询会在每次加载时自动执行,而无需单击搜索按钮

时间:2009-10-27 15:42:01

标签: php mysql paging

我希望我的代码根据搜索关键字文本框中的值执行查询。但是,每次加载时,当前代码都会执行默认查询“帮助”。任何帮助将不胜感激。 Plz帮助

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  = '&nbsp;'; // we're on page one, don't print previous link
   $first = '&nbsp;'; // 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 = '&nbsp;'; // we're on the last page, don't print next link
   $last = '&nbsp;'; // 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()------

1 个答案:

答案 0 :(得分:1)

当您的表单方法为$_GET时,您的Pager课程中的POST内的值似乎正在寻找。您需要更改其中一个,以便他们使用相同的。

如果要在提交页面之前禁用搜索,可以尝试在执行搜索的代码周围添加if

if (isset($_POST['subm'])) {
  ...
}

编辑:解决OP的评论。