PHP Sort
Posted: Sun Jul 28, 2002 10:15 pm
Hello, this is sort of a follow up from a previous post of mine on the database forum. I've set up a simple search engine on my website, but I am having problems with a script to sort the search results. My layout basically consists of a drop down box that includes various sorting options (by name, date, category, etc...) and a search box next to it to search articles on my database by name. I've gotten my sorting drop down box to be able to sort everthing in my database and sort the first page of search results. So basically if someone searched for something on my site they'd receive a set of results. These results can be sorted using the drop down box, but after they've been sorted, they can be sorted again. For some reason if you try sorting the search results twice instead of sorting and displaying the results it sorts and displays everything in the database.
Here is my current code for the drop down and search box...
And this is the code for displaying my queries...
Any help is appreciated.
*F.Y.I. - I'm running Windows XP, Apache 1.3.22, PHP 4.1.0, and MySQL 3.23.39
Here is my current code for the drop down and search box...
Code: Select all
<!--begin order-->
<form name="order">
<font face="verdana" size="2">Sort By </font>
<select size="1" name="category" style="font-family: Verdana; font-size: 8pt; border-style: solid; border-width: 1; padding-left: 4; padding-right: 4; padding-top: 1; padding-bottom: 1" onChange="location=document.order.category.optionsїdocument.order.category.selectedIndex].value;" value="GO">
<option>---------------</option>
<option value="?id=results2&search=<?=$_POSTї'search']?>&order=date&how=desc">Date (DESC)</option>
<option value="?id=results2&search=<?=$_POSTї'search']?>&order=date&how=asc">Date (ASC)</option>
<option value="?id=results2&search=<?=$_POSTї'search']?>&order=name&how=asc">Title (ASC)</option>
<option value="?id=results2&search=<?=$_POSTї'search']?>&order=name&how=desc">Title (DESC)</option>
<option value="?id=results2&search=<?=$_POSTї'search']?>&order=category&how=asc">Category (ASC)</option>
<option value="?id=results2&search=<?=$_POSTї'search']?>&order=category&how=desc">Category (DESC)</option>
</select>
</form>
<!--end order-->
</td>
<td align="left" width="408">
<!--begin search-->
<FORM METHOD="post" ACTION="http://localhost/george/articles/index.php">
<font size="2" face="Verdana">
<input type="submit" value="Title Search" name="Title Search" style="font-size: 7pt; font-family: Verdana; border-style: solid; border-width: 1"> </font>
<input type="text" name="search" size="30" style="font-family: Verdana; font-size: 8pt; border-style: solid; border-width: 1">
</form>
<!--end search-->Code: Select all
<?php include('../georgedb.php');?>
<center><table border="0" cellspacing="1">
<tr>
<td width="10"></td>
<td width="100"><b><font face="Verdana" size="2">Date</font></b></td>
<td width="315"><b>
<font face="Verdana" size="2">Title</font></b></td>
<td width="163"><b><font face="Verdana" size="2">Category</font></b></td>
</tr>
<?
if (!$order){
$order = 'date';
}
if (!$how){
$how = 'desc';
}
$sqlquery = "SELECT * FROM articles WHERE name LIKE '%$search%' ORDER BY $order $how";
$result = mysql_query($sqlquery);
$number = mysql_numrows($result);
$i = 0;
if ($number < 1) {
print '<font face="verdana" size="2"><b>
Sorry, there are no queries that match your search.<p> </p></b></font>';
}
else {
while($data = mysql_fetch_assoc($result)) {
?>
<tr>
<td width="10"></td>
<td width="100"><font face="verdana" size="2"><?=$dataї"date"];?></font></td>
<td width="315"><font face="verdana" size="2"><a href="<?=$dataї'link'];?>" style="color:#000000"><?=$dataї'name'];?></a></font></td>
<td width="163"><font face="verdana" size="2"><a href="<?=$dataї'catlink'];?>" style="color:#000000"><?=$dataї'category'];?></a></font></td>
</tr>
<?php
}
}
?>
</table></center>*F.Y.I. - I'm running Windows XP, Apache 1.3.22, PHP 4.1.0, and MySQL 3.23.39