Jump to content

Search Forms with Min Price and Max Price


Woody777

Recommended Posts

Hi

 

I have this Query and would like to get help on this.

 

I have a table with Price in it and on my search form I have Min Price and Max Price, I want to connect the min and max price to price so that when people do a search ith will display the results between the min and max price they chose, for instants:

 

Min Price = 1000 and Max Price = 10000 it have to give me that results between that 1000 and 10000.

 

$query = "SELECT distinct Img.propertyId as PropertyId, Title, ImageUrl, Location, Bedrooms, Bathrooms, Parking, Price FROM PROPERTIES as Prop LEFT JOIN IMAGES as Img ON Img.PropertyId = Prop.PropertyId WHERE 1=1 ";

 

Thank you 

Link to comment
Share on other sites

It means that all of the MySQL_* functions are being removed from php in the near (very?) future.  It's listed in the manual for every function and many, many forum posts make the suggestion to newbies to move away from MySQL_query and the like.  The way to go is PDO or mysqlI if you have to.

 

Heed the warning and learn a new set of functions.

Link to comment
Share on other sites

I have done something with pdo but that is on my sale and rent page:

 

include 'database.php';
                   $pdo = Database::connect();
                     $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
                   $sql = "SELECT  * FROM properties inner join images on images.propertyid = properties.propertyid WHERE Status ='For Sale' group by images.propertyid";
 
 
 
                   foreach ($pdo->query($sql) as $row) {
 
             $propertyid =   $row['PropertyId'];
                 $pdo = Database::connect();
        $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        $sql1 = "SELECT * FROM images where PropertyId = ?";
        $q1 = $pdo->prepare($sql1);
        $q1->execute(array($propertyid));
        $data = $q1->fetch(PDO::FETCH_ASSOC);
 
 
 
                            echo '<li class="col-md-4">';
 
                            echo '<div class="proj_block">';
                            echo '<img alt="" style="opacity: 1" src="'. $row['ImageUrl'].'"></div>';
                            echo '<div class="proj_descr"><h2>'. $row['Title'] . '</h2>';
                            echo '<div class="proj_descr_txt">'.'<b>Location: </b> '. $row['Location'] . '</div>';
                            echo '<div class="proj_descr_txt">'.'<b>Bed: </b> '. $row['Bedrooms'] . '</div>';
                            echo '<div class="proj_descr_txt">'.'<b>Bath: </b> '. $row['Bathrooms'] . '</div>';
                            echo '<div class="proj_descr_txt">'.'<b>Parking: </b> '. $row['Parking'] . '</div>';
                            echo '<div class="proj_descr_txt">'.'<b>Price: </b>R'. $row['Price'] . '</div>';
                            echo '<a href="readmore.php?PropertyId='.$propertyid.'">Read More</a>';
 
                            echo '</li>';
                   }
                   Database::disconnect();
Link to comment
Share on other sites

ooops!  I mis-read the error message earlier.  IT means that you didn't check the results of you query run before trying to fetch a record.  If you had (or if you had error checking turned on!) you would have read a message and aborted.

 

Turn on error checking.  And add a check on whether the queryresults var is false or not.  False is bad - show the error message from MySQL_error (?)

Link to comment
Share on other sites

 <?php

                  $type = $_POST['type'];

$status = $_POST['status'];

$minprice = $_POST['min_price'];

$maxprice = $_POST['max_price'];

$bedrooms = $_POST['bedrooms'];

$baths = $_POST['baths'];

 

 

$con=mysqli_connect("localhost","root","","saproperties");

// Check connection

if (mysqli_connect_errno()) {

  echo "Failed to connect to MySQL: " . mysqli_connect_error();

}

 

 

 

  $query = "SELECT distinct Img.propertyId as PropertyId, Title, ImageUrl, Location, Bedrooms, Bathrooms, Parking, Price FROM PROPERTIES as Prop LEFT JOIN IMAGES as Img ON Img.PropertyId = Prop.PropertyId WHERE 1=1 ";

 

if ($status != ''){

 $qStatus = " AND status = '$status' ";

 $query = $query . $qStatus;

}

if ($type != ''){

$qType = " AND type = '$type' ";

$query = $query . $qType;

}

if ($baths != ''){

$qBaths = " AND baths = '$baths' ";

$query = $query . $qBaths;

}

if ($bedrooms != ''){

$qBedrooms = " AND bedrooms = '$bedrooms' ";

$query = $query . $qBedrooms;

}

if ($minprice != ''){

$qMinprice = " AND min_price = '$minprice' ";

$query = $query . $qMinprice;

}

if ($maxprice != ''){

$qMaxprice = " AND max_price = '$maxprice' ";

$query = $query . $qMaxprice;

}

$query = $query." GROUP BY PropertyId";

 

$result = mysqli_query($con,$query);

 

if ($status == 'For Sale'){

 

 

 

 

while($row = mysqli_fetch_array($result)) {

 

 

 

 echo '<li class="col-md-4">';

 

                            echo '<div class="proj_block">';

                            echo '<img alt="" style="opacity: 1" src="'. $row['ImageUrl'].'"></div>';

                            echo '<div class="proj_descr"><h2>'. $row['Title'] . '</h2>';

                            echo '<div class="proj_descr_txt">'.'<b>Location: </b> '. $row['Location'] . '</div>';

                            echo '<div class="proj_descr_txt">'.'<b>Bed: </b> '. $row['Bedrooms'] . '</div>';

                            echo '<div class="proj_descr_txt">'.'<b>Bath: </b> '. $row['Bathrooms'] . '</div>';

                            echo '<div class="proj_descr_txt">'.'<b>Parking: </b> '. $row['Parking'] . '</div>';

                            echo '<div class="proj_descr_txt">'.'<b>Price: </b>R'. $row['Price'] . '</div>';

                            echo '<a href="readmore.php?PropertyId='.$row['PropertyId'].'">Read More</a>';

 

                            echo '</li>';

 

}

}else if ($status == 'For Rent'){

 

 

 

 

while($row = mysqli_fetch_array($result)) {

 

 echo '<li class="col-md-4">';

 

                            echo '<div class="proj_block">';

                            echo '<img alt="" style="opacity: 1" src="/sa property finder/'. $row['ImageUrl'].'"></div>';

                            echo '<div class="proj_descr"><h2>'. $row['Title'] . '</h2>';

                            echo '<div class="proj_descr_txt">'.'<b>Location: </b> '. $row['Location'] . '</div>';

                            echo '<div class="proj_descr_txt">'.'<b>Bed: </b> '. $row['Bedrooms'] . '</div>';

                            echo '<div class="proj_descr_txt">'.'<b>Bath: </b> '. $row['Bathrooms'] . '</div>';

                            echo '<div class="proj_descr_txt">'.'<b>Parking: </b> '. $row['Parking'] . '</div>';

                            echo '<div class="proj_descr_txt">'.'<b>Price: </b>R'. $row['Price'] . '</div>';

                            echo '<a href="readmore.php?PropertyId='.$row['PropertyId'].'">Read More</a>';

 

                            echo '</li>';

 

}

}

mysqli_close($con);

                  ?>

Link to comment
Share on other sites

Sorry but I dont understand so clearly? 

 

Like this: 

 

  $query = "SELECT distinct Img.propertyId as PropertyId, Title, ImageUrl, Location, Bedrooms, Bathrooms, Parking, Price FROM PROPERTIES as Prop LEFT JOIN IMAGES as Img ON Img.PropertyId = Prop.PropertyId WHERE 1=1 AND Price <= maxprice and >= minprice ";
Link to comment
Share on other sites

THAT'S BECAUSE YOU HAVEN'T LISTENED TO ME!

 

You have to add a line after the query checking it for an error.

 

(run the query)

$result = mysqli_query($con,$query);
(test the query ran)
if (!$result)
{
    echo "Query failed to run - error message is: ".MySQL_error();
    exit();
}
// now fetch the results since YOU ARE SURE it ran ok
Link to comment
Share on other sites

Hi there

 

I am having an issue on this query, I want to get the min price and max price search to work can anyone help me on this please it is kinda urgent?

 

Must I have a Min Price and Max Price inside my table for it to work?

 

I have to connect the Min Price and Max Price to my Price inside my table. 

 

My Query:

 

$query = "SELECT distinct Img.propertyId as PropertyId, Title, ImageUrl, Location, Bedrooms, Bathrooms, Parking, Price FROM PROPERTIES as Prop LEFT JOIN IMAGES as Img ON Img.PropertyId = Prop.PropertyId WHERE 1=1 AND Price BETWEEN >parameterMinPrice AND <parameterMaxPrice ";

 

 Here is the whole PHP Code:

 

  <?php
 
if (isset($_POST)) {
$status = $_POST['status'];
$type = $_POST['type'];
$bedrooms = $_POST['bedrooms'];
$bathrooms = $_POST['bathrooms'];
$minprice = $_POST['min_price'];
$maxprice = $_POST['max_price'];
}
 
 
 
$con=mysqli_connect("localhost","root","","saproperties");
 
// Check connection
if (mysqli_connect_errno()) {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
 
 
 
$query = "SELECT distinct Img.propertyId as PropertyId, Title, ImageUrl, Location, Bedrooms, Bathrooms, Parking, Price FROM PROPERTIES as Prop LEFT JOIN IMAGES as Img ON Img.PropertyId = Prop.PropertyId WHERE 1=1 AND Price BETWEEN >parameterMinPrice AND <parameterMaxPrice ";
 
 
 
if ($status != ''){
 $qStatus = " AND status = '$status' ";
 $query = $query . $qStatus;
}
if ($type != ''){
$qType = " AND type = '$type' ";
$query = $query . $qType;
}
if ($bedrooms != ''){
$qBedrooms = " AND bedrooms = '$bedrooms' ";
$query = $query . $qBedrooms;
}
if ($bathrooms != ''){
$qBathrooms = " AND bathrooms = '$bathrooms' ";
$query = $query . $qBathrooms;
}
if ($minprice != ''){
$qMinprice = " AND min_price = '$minprice' ";
$query = $query . $qMinprice;
}
if ($maxprice != ''){
$qMaxprice = " AND max_price = '$maxprice' ";
$query = $query . $qMaxprice;
}
$query = $query." GROUP BY PropertyId";
 
 
$result = mysqli_query($con,$query);
if (!$result)
{
    echo "Query failed to run - error message is: ".MySQL_error();
    exit();
}
 
if ($status == 'For Sale'){
 
 
 
 
while($row = mysqli_fetch_array($result)) {
 
 
 
 echo '<li class="col-md-4">';
 
                            echo '<div class="proj_block">';
                            echo '<img alt="" style="opacity: 1" src="'. $row['ImageUrl'].'"></div>';
                            echo '<div class="proj_descr"><h2>'. $row['Title'] . '</h2>';
                            echo '<div class="proj_descr_txt">'.'<b>Location: </b> '. $row['Location'] . '</div>';
                            echo '<div class="proj_descr_txt">'.'<b>Bed: </b> '. $row['Bedrooms'] . '</div>';
                            echo '<div class="proj_descr_txt">'.'<b>Bath: </b> '. $row['Bathrooms'] . '</div>';
                            echo '<div class="proj_descr_txt">'.'<b>Parking: </b> '. $row['Parking'] . '</div>';
                            echo '<div class="proj_descr_txt">'.'<b>Price: </b>R'. $row['Price'] . '</div>';
                            echo '<a href="readmore.php?PropertyId='.$row['PropertyId'].'">Read More</a>';
 
                            echo '</li>';
 
}
}else if ($status == 'For Rent'){
 
 
while($row = mysqli_fetch_array($result)) {
 
 echo '<li class="col-md-4">';
 
                            echo '<div class="proj_block">';
                            echo '<img alt="" style="opacity: 1" src="/sa property finder/'. $row['ImageUrl'].'"></div>';
                            echo '<div class="proj_descr"><h2>'. $row['Title'] . '</h2>';
                            echo '<div class="proj_descr_txt">'.'<b>Location: </b> '. $row['Location'] . '</div>';
                            echo '<div class="proj_descr_txt">'.'<b>Bed: </b> '. $row['Bedrooms'] . '</div>';
                            echo '<div class="proj_descr_txt">'.'<b>Bath: </b> '. $row['Bathrooms'] . '</div>';
                            echo '<div class="proj_descr_txt">'.'<b>Parking: </b> '. $row['Parking'] . '</div>';
                            echo '<div class="proj_descr_txt">'.'<b>Price: </b>R'. $row['Price'] . '</div>';
                            echo '<a href="readmore.php?PropertyId='.$row['PropertyId'].'">Read More</a>';
 
                            echo '</li>';
 
}
}
mysqli_close($con);
                  ?>
Link to comment
Share on other sites

a) the syntax of your BETWEEN term is incorrect. its - expr BETWEEN min AND max. where the min and max value would be provided by $minprice and $maxprice, after you have validated them (or use prepared queries.)

b) you would probably want to remove the conditional logic using $minprice and $maxprice later in the code since that will add equal comparisons with the price that will probably never be true.

 

c) your Propertyid should be unique in the PROPERTIES table, so there's no need to use distinct or GROUP BY PropertyId in the query.

 

d) since you are using mysqli_query(), you must use mysqli_error($con) in the query error checking logic.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.