Jump to content

How to sort mysql results


djjamiegee

Recommended Posts

hey

 

i have been searching the internet for days now and cannot not find a tutorial that makes sense on this topic.

 

i am doing an estate agent site and what i am after is links on the search results page to change the order of the results.

 

i have it set up currently so when a search is performed it will assend by lowest price to hightest price but i want a few links to make it change to max price to min price and from least bedrooms to most bedrooms.

 

i am new to php and mysql and this is really buggin me.

 

heres my 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>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Price Property Services | Search Results</title>
<link rel="stylesheet" type="text/css" href="pps.css" />

</head>
<body id="background">
<div id="maincontainer">
  <div id="mainheader">
    <div id="logo">
      <table width="780" border="0" class="table">
        <tr>
          <td width="182"><img src="logo.gif" width="182" height="92" /></td>
          <td width="588" align="right"><img src="mainbanner.jpg" width="588" height="92" /></td>
        </tr>
      </table>
    </div>
    <div id="nav">
      <ul id="nav">
        <li id="thome"><a href="index.html" class="selected"></a></li>
        <li id="tprop"><a href="properties.html"></a></li>
        <li id="tabout"><a href="about.html"></a></li>
        <li id="tlegal"><a href="legal.html"></a></li>
        <li id="tfees"><a href="fees.html"></a></li>
        <li id="tterms"><a href="terms.html"></a></li>
        <li id="tcontact"><a href="contact.html"></a></li>
        <li id="tlinks"><a href="links.html"></a></li>
      </ul>
    </div>
  </div>
  <div id="mainbodysearch">
  
    <?php

$type= ($_GET['type']=="") ? "" : "type = '{$_GET['type']}' AND";
$bedrooms=$_GET['bedrooms'];
$maxprice= ($_GET['maxprice']=="") ? "" : "maxprice < '{$_GET['maxprice']}' AND";
$area=$_GET['area'];

//connect to mysql
//change user and password to your mySQL name and password
mysql_connect("*****","******","*******"); 

//select which database you want to edit
mysql_select_db("*********"); 

//get the mysql and store them in $result
//change whatevertable to the mysql table you're using
//change whatevercolumn to the column in the table you want to search 
$result = mysql_query("SELECT * from properties WHERE $type bedrooms >= '$bedrooms' AND $maxprice area LIKE '$area'");

//how many properties found
$number=mysql_num_rows($result);
echo "$number properties found"; 
echo "<br><br>";	

if (mysql_num_rows($result) > 0) {
//grab all the content
while($r=mysql_fetch_array($result))
{	
   $type=$r["type"];
   $bedrooms=$r["bedrooms"];
   $maxprice=$r["maxprice"];
   $area=$r["area"];
   $first_address=$r["first_address"];
   $link=$r["link"];
   $image=$r["image"];
   $postcode=$r["postcode"];
   $lease=$r["lease"];
   $desc=$r["desc"];
  
  //display the row

echo "<table border='0' cellpadding='5' align='center' width='650' class='search'>";
echo "<tr>";
echo "<td width='100'><img src=\"" . $r["image"] .  "\"></td>";
echo "<td colspan='5' valign='top'>$desc</td>";
echo "<td align='right'><a href=\"" . $r["link"] . "\"><img src=\"info.png" . "\" border=0 alt=\"" . "\"></a></td>";
echo "</tr>";
echo "<tr>";
echo "<td width='100' rowspan='2' align='center'><font size='3'><b>£$maxprice</b></font></td>";
echo "<td width='120' colspan='2' align='left'><b>No of Bedrooms:</b> $bedrooms</td>";
echo "<td width='130' colspan='2' align='left'><b>Location:</b> $area</td>";
echo "<td width='250' colspan='2' align='left'><b>Lease Type:</b> $lease</td>";
echo "</tr>";
echo "<td width='120' colspan='2' align='left'><b>Property Type:</b> $type</td>";
echo "<td width='130' colspan='2' align='left'><b>Street:</b> $first_address</td>";
echo "<td width='250' colspan='2' align='left'><b>Post Code:</b> $postcode</td>";
echo "</tr>";
echo "</table>";
echo "<br>";
}}

else {
echo "<font size='3'>Sorry no properties were found using your search requirements, Please narrow your prefrences to return more results.</font><br><br>";
}

?>
  </div>
  <div id="searchbox">
    <div id="searchbox1"><img src="sellingnav.jpg" width="120" height="200" /></div>
    <div id="searchbox1"><img src="buyingnav.jpg" width="120" height="200" /></div>
  </div>
  <div id="mainbodysearch1">
    <div id="finder">
      <form action="finderresults.php" method="get">
        <table width="608" border="0" cellpadding="1">
          <tr>
            <td width="109"><legend>Type</legend></td>
            <td width="100"><legend>Min. Bedrooms</legend></td>
            <td width="88"><legend>Max. Price</legend></td>
            <td width="120"><legend>Area [eg. basildon]</legend></td>
            <td width="153"><legend></legend></td>
            <td width="12"><legend> </legend></td>
          </tr>
          <tr>
            <td><select name="type" id="select">
              <option value="">Any</option>
              <option value="house">House</option>
              <option value="flat">Flat</option>
              <option value="bungalow">Bungalow</option>
              <option value="mid">Mid Terrace</option>
              <option value="end">End Terrace</option>
              <option value="semi">Semi Detached</option>
              <option value="detached">Detached</option>
              <option value="cottage">Cottage</option>
              <option value="other">Other</option>
            </select></td>
            <td><select name="bedrooms" id="select">
              <option value="1">1 Bedroom</option>
              <option value="2">2 Bedrooms</option>
              <option value="3">3 Bedrooms</option>
              <option value="4">4 Bedrooms</option>
              <option value="5">5+ Bedrooms</option>
            </select></td>
            <td><select name="maxprice" id="select">
              <option value="">Any</option>
              <option value="50000">£50,000</option>
              <option value="100000">£100,000</option>
              <option value="150000">£150,000</option>
              <option value="200000">£200,000</option>
              <option value="250000">£250,000</option>
              <option value="300000">£300,000</option>
              <option value="350000">£350,000</option>
              <option value="400000">£400,000</option>
              <option value="450000">£450,000</option>
              <option value="500000">£500,000 +</option>
            </select></td>
            <td><input type="text" name="area" id="label" /></td>
            <td valign="middle"><input name="submit" type="submit" value="" class="submit"/></td>
            <td> </td>
          </tr>
        </table>
      </form>
    </div>
  </div>
  <div id="footer"><a href="">Property Updates</a>  | <a href="">HIP's</a>  | <a href="">Recruitment</a>  | <a href="">Privacy Policy</a>  | <a href="">Site Map</a></div>
</div>
</body>
</html>

 

many thanks in advanced

 

jamie

Link to comment
Share on other sites

hey

 

i have been searching the internet for days now and cannot not find a tutorial that makes sense on this topic.

 

i am doing an estate agent site and what i am after is links on the search results page to change the order of the results.

 

i have it set up currently so when a search is performed it will assend by lowest price to hightest price but i want a few links to make it change to max price to min price and from least bedrooms to most bedrooms.

 

i am new to php and mysql and this is really buggin me.

 

 

if you're asking what I think you're asking, you can use ORDER BY with your mysql queries

 

examples:

 


<?php

$query = 'SELECT * FROM properties ORDER BY maxprice ASC';   //sort by maxprice ascending

$query2 = 'SELECT * FROM properties ORDER BY maxprice DESC';  //sort by maxprice descending

$query3 = 'SELECT * FROM properties ORDER BY bedrooms ASC';  //sort by bedrooms ascending

$query4 = 'SELECT * FROM properties ORDER BY bedrooms DESC';  sort by bedrooms descending

?>

 

and you can obviously add the other conditions back into those queries as needed, but that should give you an idea of how you can sort the stuff.  you can also use a switch statement to pull sort method from $_GET to choose the proper sort method (so have sort links on the page just change the $_GET variable you set for the sorting to a different sort method)

Link to comment
Share on other sites

hey

 

thanks for your reply i do understand what you are saying but how would i get a html link to search a different query, i guess what im asking is what would go into the <a href> tags to make this happen?

 

many thanks

 

jamie

 

that's exactly what you do.  here's a quick example, i'll make up my own variables and stuff but it will be really easy for you to change the stuff to work with your application:

 


<?php

//make these link to itself, so if this file is ThisPage.php, make these links link to ThisPage.php and the $_GET variables will handle the sorting method

echo '<a href="ThisPage.php?sort=price_asc">Click here to sort by price ascending</a><br>';
echo '<a href="ThisPage.php?sort=price_desc">Click here to sort by price descending</a><br>';
echo '<a href="ThisPage.php?sort=bedrooms_asc">Click here to sort by bedrooms ascending</a><br>';
echo '<a href="ThisPage.php?sort=bedrooms_desc">Click here to sort by bedrooms descending</a><br>';

switch ($_GET['sort'] )
{
	case "price_asc": 
				$query = 'SELECT * FROM properties ORDER BY maxprice ASC';
				break;
	case "price_desc":
				$query = 'SELECT * FROM properties ORDER BY maxprice DESC';
				break;
	case "bedrooms_asc":
				$query = 'SELECT * FROM properties ORDER BY bedrooms ASC';
				break;
	case "bedrooms_desc":
				$query = 'SELECT * FROM properties ORDER BY bedrooms DESC';
}

Link to comment
Share on other sites

hiya

 

thanks for that i get it now the only problem i am coming across now is when i click a link say price assending it refreshs the page to change the order but i then lose the data the person submitted on the form so it then returns no results.

 

so i need something to keep those results but i dont know what.

 

thanks

 

jamie

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.