tags:

views:

86

answers:

1

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"&gt;
<html xmlns="http://www.w3.org/1999/xhtml"&gt;
<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  A: 

It looks like you're looking for values in $_GET in your Pager class when your form's method is POST. 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:

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

Edit: addressed OP's comment.

Lucas Oman
Thanks, for ur quick reply. My my requirement is, the default queries are executed first and the results are shown at the start up, how to stop this??
Rajasekar
Edited post to address your comment.
Lucas Oman
Its Working and i getting the results after clicking the search button. But the problem is when i click the second page its not showing the second page result...........
Rajasekar
That's probably because you haven't changed your form's method to GET instead of POST. If you change your code to use GET (including the above snippet in my post), it should work.
Lucas Oman
I changed it already. But i think because of the condition if (isset($_GET['subm'])) is there, the condition is valid only after clicking the search button. After the button is clicked, the results are displayed with paging numbers 1234.. After that when i click second page, it checks the above condition isset($_GET['subm']) and its false and so results are not shown. is there any other condtion to check whether the page is first loaded???
Rajasekar
Then I would add a check for the 'page' value: if (isset($_GET['subm']) || isset($_GET['page'])) { ... }
Lucas Oman
Millions of Thanks friend, its working fine. Once again thanks for your effort and help.... Vote ++
Rajasekar
You're quite welcome :) You can also "accept" the answer if you'd like.
Lucas Oman
But one problem friend, how to store the previous search value in the appropriate text box even after the other pages are loaded
Rajasekar