Jump to content

sort table


alienmojo

Recommended Posts

i have a php script that displays results in a table after the user gives certain criteria. i want to be able to sort the results buy clicking the table header EX.price and it refreshes itself with the rows sorted. I have all of that done except after you click on the header i get an error saying my quiery fail. This is because when it refreshes itself it does not retain the criteria sent by the form the user filled out to build the quiery.

 

here is my code if u need futher explimation or have any question let me know

 

<html>
<head>
<link rel="stylesheet" href="CSS.css" type="text/css" />
<title>Search Results</title>
</head>
<body align="center">

<div id="sidebar">
<img src="images/bigdoglogo.gif" width="225"height="79"  border="0"><div id="menu">
<a href="Home.html">Home</a>
<a href="AboutUs.html">About Us</a>
<a href="CMS.php">Realtor login</a>
<a href="newmembers.html">Member Signup</a>
<a href="memberlogin.html">Member's Login</a>
<a href="morgage.html">Mortgage Info</a>
<a href="contact.html">Contact Us</a></div>
</div>
</div>
<div id="content">

</body>
<h1>Search Results</h1><br><br>

<?php

$sort=$_GET['sort'];


$SQL = "SELECT * FROM estate ";
$SQLTEST = "SELECT * FROM estate ";




if($_GET["city"]) 
{
$city=$_GET["city"];
$SQL =" $SQL WHERE city='".$city."'  ";
}



if($_GET["state"]) 
{
if($SQL != $SQLTEST)
{
$state=$_GET["state"];
$SQL =" $SQL AND state='".$state."'  ";
}
else
{
$state=$_GET["state"];
$SQL =" $SQL WHERE state='".$state."'  ";
}
}



if($_GET["zipcode"]) 
{
if($SQL!=$SQLTEST)
{
$zipcode=$_GET["zipcode"];
$SQL =" $SQL AND zipcode='".$zipcode."'  ";
}
else
{
$zipcode=$_GET["zipcode"];
$SQL =" $SQL WHERE zipcode='".$zipcode."'  ";
}
}



$pricemin=$_GET["pricemin"];
$pricemax=$_GET["pricemax"];
if($pricemax=="1")
$pricemax="99999999999999";
if($_GET["pricemin"]||$pricemax) 
{

if($SQL!=$SQLTEST)
{
$SQL =" $SQL AND price BETWEEN ".$pricemin." AND ".$pricemax."  ";
}
else
{
$SQL =" $SQL WHERE price BETWEEN ".$pricemin." AND ".$pricemax."  ";
}
}


//100
$bathsmin=$_GET["bathsmin"];
$bathsmax=$_GET["bathsmax"];
if($bathsmax="1")
$bathsmax="99999999999999";
if($_GET["bathsmin"]||$bathsmax) 
{

if($SQL!=$SQLTEST)
{
$SQL =" $SQL AND baths BETWEEN ".$bathsmin." AND ".$bathsmax."  ";
}
else
{
$SQL =" $SQL WHERE baths BETWEEN ".$bathsmin." AND ".$bathsmax."  ";
}
}



$bedroomsmin=$_GET["bedroomsmin"];
$bedroomsmax=$_GET["bedroomsmax"];
if($bedroomsmax="1")
$bedroomsmax="99999999999999";
if($_GET["bedroomsmin"]||$bedroomsmax) 
{

if($SQL!=$SQLTEST)
{
$SQL =" $SQL AND bedrooms BETWEEN ".$bedroomsmin." AND ".$bedroomsmax."  ";
}
else
{
$SQL =" $SQL WHERE bedrooms BETWEEN ".$bedroomsmin." AND ".$bedroomsmax."  ";
}
}



if($_GET["county"]) 
{
if($SQL!=$SQLTEST)
{
$county=$_GET["county"];
$SQL =" $SQL AND county='".$county."'  ";
}
else
{
$county=$_GET["county"];
$SQL =" $SQL WHERE county='".$county."'  ";
}
}



if($_GET["buyorlease"]) 
{
if($SQL!=$SQLTEST)
{
$buyorlease=$_GET["buyorlease"];
$SQL =" $SQL AND buyorlease='".$buyorlease."'  ";
}
else
{
$buyorlease=$_GET["buyorlease"];
$SQL =" $SQL WHERE buyorlease='".$buyorlease."'  ";
}
}



if($_GET["typeofprop"]) 
{
if($SQL!=$SQLTEST)
{
$typeofprop=$_GET["typeofprop"];
$SQL =" $SQL AND typeofprop='".$typeofprop."'  ";
}
else
{
$typeofprop=$_GET["typeofprop"];
$SQL =" $SQL WHERE typeofprop='".$typeofprop."'  ";
}
}

if($sort==1)
$SQL =" $SQL AND ORDER BY price ";
if($sort==2)
$SQL =" $SQL AND ORDER BY baths ";
if($sort==3)
$SQL =" $SQL AND ORDER BY bedrooms ";
if($sort==4)
$SQL =" $SQL AND ORDER BY squarefeet ";





$con = mysql_connect("localhost","root","******");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("realestate", $con);


$result = mysql_query($SQL);

echo $SQL1;

echo "<table border='5'>
  <tr>
  <th>Picture</th>
  <th><a href=\"results.php?sort=1\">Price</a></th>
  <th><a href=\"results.php?sort=2\">Baths</a></th>
  <th><a href=\"results.php?sort=3\">Bedrooms</a></th>
  <th>State</th>
  <th>City</th>
  <th><a href=\"results.php?sort=4\">Square Feet</a></th>
  </tr>";

while($row = mysql_fetch_array($result))
  {
  $accountnum=$row['accountnum'];  

  echo "<tr>";
  echo '<td><a href="descrip.php?accountnum='.$accountnum.'"> <img src="images/'.$row['imagename'].'" width="75" height="75"></a> </td>';
  echo "<td>" . $row['price'] . "</td>";
  echo "<td>" . $row['baths'] . "</td>";
  echo "<td>" . $row['bedrooms'] . "</td>";
  echo "<td>" . $row['state'] . "</td>";
  echo "<td>" . $row['city'] . "</td>";
  echo "<td>" . $row['squarefeet'] . "</td>";
  echo "</tr>";

  }

echo "</table>";


if(mysql_num_rows($result) == 0)
ECHO "<h3>NO MATCHES FOUND</h3>";
mysql_close($con);

?>

</body>
</html>

Link to comment
https://forums.phpfreaks.com/topic/37410-sort-table/
Share on other sites

The simplest way to fix this is with sessions.  Add this code at the TOP of your script.  Before the <html>, and in the first column.

 

<?php session_start(); ?>

 

Then you can store values in the $_SESSION[] array, and they will be kept between requests.  Voila!

Link to comment
https://forums.phpfreaks.com/topic/37410-sort-table/#findComment-178824
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.