Snatch Posted August 14, 2007 Share Posted August 14, 2007 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>"; Quote Link to comment https://forums.phpfreaks.com/topic/64891-results-ordering-problem/ Share on other sites More sharing options...
akitchin Posted August 14, 2007 Share Posted August 14, 2007 what are you trying to order by, what the the print($query) spit out, and how do you know it's not ordering correctly? we need more details to actually help you - we don't know what's actually wrong here. Quote Link to comment https://forums.phpfreaks.com/topic/64891-results-ordering-problem/#findComment-323741 Share on other sites More sharing options...
Snatch Posted August 14, 2007 Author Share Posted August 14, 2007 Sorry - users enter the product they want to search for and it queries two fields and returns the results. This bit works fine. However, it returns the results in any order and I would like the user to be able to order them by price or name. Quote Link to comment https://forums.phpfreaks.com/topic/64891-results-ordering-problem/#findComment-323774 Share on other sites More sharing options...
akitchin Posted August 14, 2007 Share Posted August 14, 2007 i realize what the code actually does, but how does it react when you enter the order parameter? uncomment print($query) and see what comes out. Quote Link to comment https://forums.phpfreaks.com/topic/64891-results-ordering-problem/#findComment-323777 Share on other sites More sharing options...
Snatch Posted August 14, 2007 Author Share Posted August 14, 2007 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? Quote Link to comment https://forums.phpfreaks.com/topic/64891-results-ordering-problem/#findComment-323791 Share on other sites More sharing options...
jishosan Posted August 14, 2007 Share Posted August 14, 2007 Before you submit the mysql_query, echo $query so we can validate the string being passed. Quote Link to comment https://forums.phpfreaks.com/topic/64891-results-ordering-problem/#findComment-323797 Share on other sites More sharing options...
akitchin Posted August 14, 2007 Share Posted August 14, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/64891-results-ordering-problem/#findComment-323818 Share on other sites More sharing options...
Snatch Posted August 14, 2007 Author Share Posted August 14, 2007 It prints this when i type test into the search box: SELECT * FROM products WHERE name like '%test%' or type like '%test%' but nothing gets printed out when I click on the ordering links? Quote Link to comment https://forums.phpfreaks.com/topic/64891-results-ordering-problem/#findComment-323842 Share on other sites More sharing options...
akitchin Posted August 14, 2007 Share Posted August 14, 2007 it's likely in how you've written your conditional: if (!empty($order)) give that a shot. Quote Link to comment https://forums.phpfreaks.com/topic/64891-results-ordering-problem/#findComment-323848 Share on other sites More sharing options...
Snatch Posted August 14, 2007 Author Share Posted August 14, 2007 That didn't seem to make any differance unless i'm putting it in the wrong place? Quote Link to comment https://forums.phpfreaks.com/topic/64891-results-ordering-problem/#findComment-323878 Share on other sites More sharing options...
akitchin Posted August 14, 2007 Share Posted August 14, 2007 //Use the ordering if an order has been passed if (!empty($order)) { $query = $query." order by $order"; } that's what i was referring to. Quote Link to comment https://forums.phpfreaks.com/topic/64891-results-ordering-problem/#findComment-323894 Share on other sites More sharing options...
jishosan Posted August 14, 2007 Share Posted August 14, 2007 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? Quote Link to comment https://forums.phpfreaks.com/topic/64891-results-ordering-problem/#findComment-323896 Share on other sites More sharing options...
Snatch Posted August 14, 2007 Author Share Posted August 14, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/64891-results-ordering-problem/#findComment-323949 Share on other sites More sharing options...
akitchin Posted August 14, 2007 Share Posted August 14, 2007 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"> Quote Link to comment https://forums.phpfreaks.com/topic/64891-results-ordering-problem/#findComment-323953 Share on other sites More sharing options...
jishosan Posted August 14, 2007 Share Posted August 14, 2007 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>"; Quote Link to comment https://forums.phpfreaks.com/topic/64891-results-ordering-problem/#findComment-323994 Share on other sites More sharing options...
Snatch Posted August 14, 2007 Author Share Posted August 14, 2007 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>"; Quote Link to comment https://forums.phpfreaks.com/topic/64891-results-ordering-problem/#findComment-324020 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.