Jump to content

PHP MYSQL Advanced search


tagtekin

Recommended Posts

I am trying to create search page where visitor picks the zip, city, min max price and min beds and max beds for their listings to choose from. I am getting stuck where if the visitor does not select any vlaues. Here is what igot . Thanks
<?php
 
$city = $_GET['city'];
$zip = $_GET['zip'];
$minprice = $_GET['minprice'];
$maxprice = $_GET['maxprice'];
$minbaths = $_GET['maxbaths'];
$minbeds = $_GET['minbeds'];



  $db=@mysql_connect("localhost", "", "") or die("ERROR--CAN'T CONNECT TO SERVER");
    @mysql_select_db("listings") or die("ERROR--CAN'T CONNECT TO DB");

$table = 'listings_tbl';

if (isset($_GET['pageno'])) {
  $pageno = $_GET['pageno'];
} else {
  $pageno = 1;
} // if

//$q = "SELECT * FROM $table WHERE city IN (" . $city . ") AND zip IN (" . $zip . ") AND price BETWEEN " . $_POST['minprice'] . " AND " . $_POST['maxprice'];
//$query = "SELECT count(*) FROM $table WHERE City like '$city' and Zip_Code like '$zip' AND Beds < '$minbeds' and Full_Baths < '$minbaths' and List_Price between '$minprice' and '$maxprice'";
  //AND Zip Code IN '$zip'
//AND price BETWEEN " . $_POST['minprice'] . " AND " . $_POST['maxprice']; 
//if ($city =="" and $zip==""){
//echo "PLease select zip or city";
//}else if  ($city ==""){
//echo "PLease select zip or city";
$query = "SELECT count(*) FROM $table WHERE Zip_Code like '$zip' AND Beds < '$minbeds' and Full_Baths < '$minbaths' and List_Price between '$minprice' and '$maxprice'";
// }else if  ($zip ==""){
//echo "PLease select zip or city";
//$query = "SELECT count(*) FROM $table WHERE City like '$city' AND Beds < '$minbeds' and Full_Baths < '$minbaths' and List_Price between '$minprice' and '$maxprice'";
//}else{
// $query = "SELECT count(*) FROM $table WHERE City like '$city' AND Zip_Code like '$zip' AND Beds < '$minbeds' and Full_Baths < '$minbaths' and List_Price between '$minprice' and '$maxprice'";
// }
if($minprice == "50"){
echo $minprice;
$minprice =50;
}
if($maxprice =='50'){
$maxprice =51;
}
if($minbeds ='ANY'){
$minbeds =10;
}
if($minbaths ='ANY'){
$minbaths =10;
}


$query2 = "SELECT count(*) FROM $table WHERE City LIKE'$city' and Zip_Code LIKE '$zip' and Beds like '$minbeds' and Full_Baths like '$minbaths' and List_Price between '100' and '200'";

$result = mysql_query($query, $db) or trigger_error("SQL", E_USER_ERROR);
$result2 = mysql_query($query2, $db) or trigger_error("SQL", E_USER_ERROR);
$query_data = mysql_fetch_row($result);
$numrows = $query_data[0];
$numresults = mysql_num_rows($result2);
echo $numresults;
$rows_per_page = 1;
$lastpage      = ceil($numrows/$rows_per_page);


$pageno = (int)$pageno;
if ($pageno < 1) {
  $pageno = 1;
} elseif ($pageno > $lastpage) {
  $pageno = $lastpage;
} // if


$limit = 'LIMIT ' .($pageno - 1) * $rows_per_page .',' .$rows_per_page;
$query = "SELECT * FROM $table WHERE Zip_Code like '$zip' AND Beds < '$minbeds' and Full_Baths < '$minbaths' and List_Price between '$minprice' and '$maxprice' $limit";
$result = mysql_query($query1, $db) or trigger_error("SQL", E_USER_ERROR);
?>
<div align="center">Home Investment Realty Listings
  <p></p>
  <?
if ($pageno == 1) {
   
echo "<br> <align=\"center\"> FIRST PREV ";

} else {
  echo " <a href='{$_SERVER['PHP_SELF']}?pageno=1'>FIRST</a> ";
  $prevpage = $pageno-1;
  echo " <a href='{$_SERVER['PHP_SELF']}?pageno=$prevpage'>PREV</a> ";
} // if


echo " ( Page $pageno of $lastpage ) ";


if ($pageno == $lastpage) {
  echo " NEXT LAST ";
} else {
  $nextpage = $pageno+1;
  echo " <a href='{$_SERVER['PHP_SELF']}?pageno=$nextpage'>NEXT</a> ";
  echo " <a href='{$_SERVER['PHP_SELF']}?pageno=$lastpage'>LAST</a> ";
} // i

?>
</div>
<?
while ($row = mysql_fetch_array($result))
{
?>
</div>
<table width="531" border="0" align="center" cellpadding="0" cellspacing="0" bordercolor="#FFFFFF" bgcolor="#003366">
            <tr>
              <td width="116"><div align="center"><span class="worthbox"><img src="/listingimages/<? echo $row['MLS']; ?>_1.jpg" width="102" height="73" /></span></div></td>
              <td width="284"><p class="worthbox"><strong><? echo $row['Address']; ?><strong><strong> <? echo $row['City']; ?></strong></strong></strong></p>
                <p class="worthbox"><strong><strong><strong> <strong><? echo $row['County']; ?>, <strong><? echo $row['Zip_Code']; ?></strong></strong></strong></strong></strong></p>
                <p class="worthbox"><strong><strong>MLS#: <? echo $row['MLS']; ?> </strong></strong></p>
                <p align="center" class="worthbox"><strong><strong><a href="searchresultdetails.php?mls=<? echo $row['MLS']; ?>"><img src="detials.gif" width="80" height="11" border="0" /></a></strong></strong></p></td>
              <td width="131"><p align="center" class="nhead"><span style="font-weight: bold;"><? echo $row['List_Price']; ?> ,000</span></p>               
                <p align="center" class="nhead"><span style="font-weight: bold;"><? echo $row['Sq_Ft_Heated']; ?> ft Heated</span></p>
                <p align="center" class="nhead"><span style="font-weight: bold;"><span class="worthbox"><? echo $row['Beds']; ?></span></span><span style="font-weight: bold;"><span class="worthbox">Beds, <? echo $row['Full_Baths']; ?> Baths, <? echo $row['Half_Baths']; ?>1/2 Baths</span></span></p></td>
            </tr>
            <tr bgcolor="#072444">
              <td>&nbsp;</td>
              <td height="13">&nbsp;</td>
              <td width="131">&nbsp;</td>
            </tr>
</table>
<?
}



?>
Link to comment
https://forums.phpfreaks.com/topic/17527-php-mysql-advanced-search/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.