I am using a php code to perform search queries through MySQL database. The database consists of a product title, url, posted date and store name. The query is looking at the title and returns relevant results. I want to add a sorting feature on my page that will allow users to sort those results by date and/or store. Its a pretty basic thing, but I am just having hard time to get it to work. Basically if a user searches for iPhone, here's what they would see in sorting options next to the search results
Posted Date
Today (8)
Yesterday (2)
Last week (15)
Store
Apple (3)
Amazon (2)
Microsoft (0) - just kidding, if its zero it shouldn't be here.
So when they click on those individual links, it will kind of sort out and just show those narrowed results to the users. Help me out guys! you are the best. Currently all the results are sorted by date in descending order by default. Here's the code that I am using for search.. Thanks!!
mysql_select_db("fake_db",$con);
$sql = "SELECT DISTINCT * FROM data WHERE (";
while(list($key,$val)=each($split_stemmed ))
{
if($val<>" " and strlen($val) > 0)
{
$sql .= "(title LIKE '%$val%') AND";
}
}
$sql=substr($sql,0,(strLen($sql)-3));
$sql .= ") ORDER BY pdate DESC";
$query = mysql_query($sql) or die(mysql_error());
$row_sql = mysql_fetch_assoc($query);
$total = mysql_num_rows($query);
if (!isset($_GET['startrow']) or !is_numeric($_GET['startrow']))
{
$startrow = 0;
}
else
{
$startrow = (int)$_GET['startrow'];
}
$sql .= " limit $startrow,$limit";
$result = mysql_query($sql) or die("Couldn't execute query");
$count = 1 + $startrow;
while ($row= mysql_fetch_array($result))
{
$title = $row["title"];
$link=$row["link"];
$pdate=$row["pdate"];
echo '
<h2 class="title"><a href="'.$link.'">'.$title.'</a></h2>
<div class="meta">
'.$pdate.'
</div>
';
$count++ ;
}
?>
</div>
<?php