Jump to content

Recommended Posts

I'm trying to allow users to order the results of their search but it's not working. Can anyone tell me what i'm doing wrong? Thanks!

 

//Get the word submitted by the form 
$searchTitle = $_GET["search"]; 
if (!empty($searchTitle)) 
{ 
print "  Looking for products containing $searchTitle <br><br/>"; 

//Get the order method if one has been passed to this page 
$order = $_GET["order"]; 

// create query  - This query combines data from the film table and the director table 
$query = "SELECT * FROM products WHERE name like '%$searchTitle%' or type like '%$searchTitle%'";

//Use the ordering if an order has been passed 
if (!$order=="") 
{ 
  $query = $query." order by $order "; 
} 

//print $query; 
// execute query 
$result = mysql_query($query) or die ("Error in query"); 

// see if any rows were returned 
if (mysql_num_rows($result)>0) { 
echo "<div id=sortactions>". 
             "Order results by: ". 
		 "<a href='search.php?order=name'>Name / </a>". 
             "<a href='search.php?order=price'>Price</a></div>";

Link to comment
https://forums.phpfreaks.com/topic/64891-results-ordering-problem/
Share on other sites

The results just dissapear. No error messages. The only thing I can think of is the url changes from http://localhost/website/search.php?search=test  to  http://localhost/website/search.php?order=name  does it need to keep the "search=test" bit before order=name? If so how would I go about doing this?

you add parameters to a URL by separating them with an ampersand (&):

 

http://localhost/website/search.php?search=test&order=name

 

if you neglect to add search in there, your query will likely trigger an error because it will be seeing an empty string (ie. WHERE name LIKE '%%').  again, as we've said, post what print($query) spits out so we can see what query is actually being executed.

Can we see the code for the page submit? As was previously mentioned, you will need to ensure that the system is passing the variables correctly

 

http://localhost/page/page.php?search=string&order=name

 

If the &order is not listed properly, then order will not be populated. Could you post the full URL for a search that passes both variables, and the relevant query that your script builds?

submit page:

 

 

             <div id="search">
  	<script type="text/javascript" src="./js/cleardefault.js"></script>
<form method="get" action="search.php">
<input name="search" type="text" size="40" value="click here to search" class="cleardefault" /> 
</form> 
  </div>

 

Process:

 

<?php 

include "connection.php"; 



//Get the word submitted by the form 
$searchTitle = $_GET["search"]; 
if (!empty($searchTitle)) 
{ 
print "  Looking for products containing $searchTitle <br><br/>"; 

//Get the order method if one has been passed to this page 
$order = $_GET["order"]; 

// create query  - This query combines data from the film table and the director table 
$query = "SELECT * FROM products WHERE name like '%$searchTitle%' or type like '%$searchTitle%'";

//Use the ordering if an order has been passed 
if (!$order=="") 
{ 
  $query = $query." order by $order "; 
} 

//print $query; 
// execute query 
$result = mysql_query($query) or die ("Error in query"); 

// see if any rows were returned 
if (mysql_num_rows($result)>0) { 
echo "<div id=sortactions>". 
             "Order results by: ". 
		 "<a href='search.php?order=name'>Name / </a>". 
             "<a href='search.php?order=price'>Price</a></div>";


    while ($row = @ mysql_fetch_array($result)) { 
    //while($row = mysql_fetch_row($result)) { 
         echo "<div id=browsestyle><table width=80% border=0>" .
  				"<tr>" .
   				"<td width=10% valign=top rowspan=9><span id=imgpad><img src=".$row["image"]." height=50 width=50 /></span></td></tr>" .
   				"<tr><td width=25% valign=top><strong>Type: </strong></td><td width=75% valign=top>". $row["type"] ."</td></tr>" .
			"<tr><td width=25% valign=top><strong>Name: </strong></td><td width=75% valign=top><a href = 'getprod.php?prodid=" . $row["id"] ."'>". $row["name"] ."</a></td></tr>" .
			"<tr><td width=25% valign=top><strong>Price: </strong></td><td width=65% valign=top>" . $row["price"] . "</td></tr>" .
			"</table></div>" ;
    } 
} 

else { 
       // print status message 
    echo "No Results Found!"; 
} 

// free result set memory 
mysql_free_result($result); 

// close connection 
mysql_close($conn); 
} 
?> 

 

If I search for software the url is this: http://localhost/website/search.php?search=software

 

and it displays the results.

 

If I then click on the link to order the results by price the url is: http://localhost/website/search.php?order=name

 

and the results dissapear.

you add parameters to a URL by separating them with an ampersand (&):

 

http://localhost/website/search.php?search=test&order=name

 

if you neglect to add search in there, your query will likely trigger an error or just plain return no rows because it will be seeing an empty string (ie. WHERE name LIKE '%%').

 

i'm not sure i could have been clearer.  format your links so that they include the search term in it:

 

<a href="blah/blah/blah.php?search=<?php echo $_GET['search']; ?>&order=name">

akitchin is exactly right. The system does not retain the 'search' variable when you click on the second link. It is basically reloading the query, but with an empty search string, which means the entire query is basically "order by name". You need to modify your order links to retain the search= string by appending the order string to it. You could use things like Session variables to get around this limitation, but it seems like the simple fix that was mentioned will cover it.

 

Because you are using echo commands for you html, you should be able to just put it like this:

 

if (mysql_num_rows($result)>0) { 
echo "<div id=sortactions>". 
             "Order results by: ". 
		"<a href='search.php?search={$searchTitle}&order=name'>Name / </a>". 
             "<a href='search.php?search={$searchTitle}&order=price'>Price</a></div>";

Thanks for your help the code below seems to of sorted it! :)

 

echo "<div id=sortactions>". 
             "Order results by: ". 
		 "<a href='search.php?search=$searchTitle&order=name'>Name / </a>". 
             "<a href='search.php?search=$searchTitle&order=price'>Price</a></div>"; 

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.