Page 1 of 1

PHP Sort

Posted: Sun Jul 28, 2002 10:15 pm
by gmitra
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...

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&#1111;document.order.category.selectedIndex].value;" value="GO">

	<option>---------------</option>
        <option value="?id=results2&search=<?=$_POST&#1111;'search']?>&order=date&how=desc">Date (DESC)</option>
	<option value="?id=results2&search=<?=$_POST&#1111;'search']?>&order=date&how=asc">Date (ASC)</option>
        <option value="?id=results2&search=<?=$_POST&#1111;'search']?>&order=name&how=asc">Title (ASC)</option>
	<option value="?id=results2&search=<?=$_POST&#1111;'search']?>&order=name&how=desc">Title (DESC)</option>
        <option value="?id=results2&search=<?=$_POST&#1111;'search']?>&order=category&how=asc">Category (ASC)</option>
	<option value="?id=results2&search=<?=$_POST&#1111;'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">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                <input type="submit" value="Title Search" name="Title Search" style="font-size: 7pt; font-family: Verdana; border-style: solid; border-width: 1">&nbsp; </font>

   
<input type="text" name="search" size="30" style="font-family: Verdana; font-size: 8pt; border-style: solid; border-width: 1">



</form>

<!--end search-->
And this is the code for displaying my queries...

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)&#123;
  $order = 'date'; 
&#125;
if (!$how)&#123;
  $how = 'desc';
&#125;

$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) &#123;
print '<font face="verdana" size="2"><b>&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Sorry, there are no queries that match your search.<p>&nbsp;</p></b></font>';
&#125;
else &#123;
while($data = mysql_fetch_assoc($result)) &#123; 
?> 
    <tr> 
        <td width="10"></td> 
        <td width="100"><font face="verdana" size="2"><?=$data&#1111;"date"];?></font></td> 
        <td width="315"><font face="verdana" size="2"><a href="<?=$data&#1111;'link'];?>" style="color:#000000"><?=$data&#1111;'name'];?></a></font></td> 
        <td width="163"><font face="verdana" size="2"><a href="<?=$data&#1111;'catlink'];?>" style="color:#000000"><?=$data&#1111;'category'];?></a></font></td> 
    </tr> 
<?php 
&#125;
&#125;
?> 

</table></center>
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

...

Posted: Tue Jul 30, 2002 1:12 pm
by gotDNS
make sure you pass on a hidden version of what they searched for and have it re-search and re-sort as it goes...

Posted: Fri Aug 02, 2002 9:05 pm
by gmitra
I don't understand how to continue to pass a hidden version of what they searched for. I am able to post back the query for the sorting the first time using $_POST, but after that it doesn't work for me. Can you please give me an example of how to do so properly.

Posted: Mon Aug 05, 2002 9:34 am
by mikeq
Have hidden fields on your page and make their value the value of your $POST stuff that you want to keep between pages.

Posted: Mon Aug 05, 2002 9:44 am
by twigletmac
A hidden field can be added to your form using something like the following code:

Code: Select all

<input type="hidden" value="<?php echo $_POST&#1111;'some_value']; ?>">
It won't be visible on the page but all the values will be shown in the source code (so you can check that they're getting passed correctly).

Mac