justlukeyou Posted March 4, 2011 Share Posted March 4, 2011 Hi, I have added product search links to my site: www.domain.php?product=football But now I want to add links which read the URL in the search so it adds the price search on: get (www.domain.com.php?product=television)&price=1-100 That way people can view the products and then if they wish sort the product by price band. If I hand write the link in the URL bar and it does work, now I just need to form the links in clickable links. This seems the simplest way to do it. Is it possible to do this or should I be doing another way? Quote Link to comment https://forums.phpfreaks.com/topic/229569-get-url-for-second-query/ Share on other sites More sharing options...
Muddy_Funster Posted March 4, 2011 Share Posted March 4, 2011 is there a reason your not using POST and a form? Quote Link to comment https://forums.phpfreaks.com/topic/229569-get-url-for-second-query/#findComment-1182777 Share on other sites More sharing options...
justlukeyou Posted March 4, 2011 Author Share Posted March 4, 2011 Do you mean a search box? I am looking to add that later but many sites such as www.mydeco.com enable people to search for products and then sort by price ranges by using links. I have the product links working but I want to add the price range query. I thought forms were for entering data. Quote Link to comment https://forums.phpfreaks.com/topic/229569-get-url-for-second-query/#findComment-1182782 Share on other sites More sharing options...
hoogie Posted March 4, 2011 Share Posted March 4, 2011 You can certainly do it this way if you like. To me it would make more sense to use a "sort" variable to the URL instead of a "price" variable. That way you could use one variable for all your links: www.domain.com.php?product=television&sort=price www.domain.com.php?product=television&sort=title www.domain.com.php?product=television&sort=date etc. Just keep in mind that it's really easy for users to enter malicious code in your URL, so be sure to escape these variables before using them in your database. Quote Link to comment https://forums.phpfreaks.com/topic/229569-get-url-for-second-query/#findComment-1182815 Share on other sites More sharing options...
Muddy_Funster Posted March 4, 2011 Share Posted March 4, 2011 if you are using GET then just take the parenthesis out of the url. e.g. URL = http://results.php?product=football&price=1-100 | then on results.php have the following: $product = $_GET['product']; $price_range = $_GET['price']; //.............REST OF PAGE Although, I'm getting confused (yet again - happens a lot these days) You are talking about sorting by price, but are passing information that would be used as a filter, rather than a sort Quote Link to comment https://forums.phpfreaks.com/topic/229569-get-url-for-second-query/#findComment-1182816 Share on other sites More sharing options...
justlukeyou Posted March 4, 2011 Author Share Posted March 4, 2011 Hi, I am using Get but I couldn't get this to work. Is there a standard way to click between different links and filters? Quote Link to comment https://forums.phpfreaks.com/topic/229569-get-url-for-second-query/#findComment-1182956 Share on other sites More sharing options...
hoogie Posted March 4, 2011 Share Posted March 4, 2011 It might be time to show some of your code so we can see what's going on. Quote Link to comment https://forums.phpfreaks.com/topic/229569-get-url-for-second-query/#findComment-1182957 Share on other sites More sharing options...
justlukeyou Posted March 4, 2011 Author Share Posted March 4, 2011 Thanks, this is the code. ini_set('display_errors', 1); error_reporting(-1); $query = "SELECT * FROM productfeed"; if(isset($_GET['description']) && !empty($_GET['description'] )) { $description = $_GET['description']; $query .= " WHERE description like '%$description%' LIMIT 0, 10"; } if(isset($_GET['price']) && !empty($_GET['price'])) { $price = explode('-', $_GET['price']); $lowPrice = (int)$price[0]; $highPrice = (int)$price[1]; $query .= " AND price BETWEEN $lowPrice AND $highPrice"; } $result = mysql_query($query); while($row = mysql_fetch_assoc($result)) { $id = $row['id']; $image = $row['awImage']; $link = $row['link']; $description = $row['description']; $fulldescription = $row['fulldescription']; $price = $row['price']; echo "<div class='productdisplayshell'> <div class='productdisplayoutline'> <div class='productborder'><center> <a href='$link' target='_blank'><img src='$image' width=\"95%\" /></a> </center> </div></div> <div class='productdescriptionoutline'> <div class='productdescriptionbox'> <a href='$link' target='_blank' >$description</a> </div> <div class='productfulldescriptionbox'>$fulldescription</div> </div> <div class='productpriceoutline'> <div class='productpricebox'> <center>£ $price</center> </div> <div class='productbuybutton'> <center><a href='$link' target='_blank' ><img src=/images/buybutton.png /></a></center> </div> </div> </div>"; } if ($_GET['description'] == $description ) { echo 'Sorry, this product is not available. Please visit our <a href="http://www.ukhomefurniture.co.uk">Homepage</a>.'; } if( !$result = mysql_query($query) ) { echo "<br>Query string: $query<br>Produced error: " . mysql_error() . '<br>'; } ?> <?php function sanitizeString($string) { return mysql_real_escape_string($string); } $description = sanitizeString($_GET['description']); $query .= " WHERE description like '%$description%' LIMIT 0, 10"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/229569-get-url-for-second-query/#findComment-1182958 Share on other sites More sharing options...
hoogie Posted March 4, 2011 Share Posted March 4, 2011 At first glance, your problem might be that "LIMIT 0, 10" needs to go at the end of the query. You're tacking on the price AND statement AFTER your LIMIT statement. Quote Link to comment https://forums.phpfreaks.com/topic/229569-get-url-for-second-query/#findComment-1182965 Share on other sites More sharing options...
justlukeyou Posted March 4, 2011 Author Share Posted March 4, 2011 Hi, I have removed that thanks. Can someone tell me how the process works. If I click one link to filter what code or process do I follow to and other queries onto to the previous URL? Quote Link to comment https://forums.phpfreaks.com/topic/229569-get-url-for-second-query/#findComment-1182966 Share on other sites More sharing options...
hoogie Posted March 4, 2011 Share Posted March 4, 2011 I might not be understanding your question, but here goes: If you want to filter by price range, your code is pretty close to working. On the page with the link, your link should look like this: <a href="www.domain.com.php?product=television&price=1-100">Price (1-100)</a> The code you posted should be changed to this: ini_set('display_errors', 1); error_reporting(-1); $query = "SELECT * FROM productfeed"; if(isset($_GET['description']) && !empty($_GET['description'] )) { $description = $_GET['description']; $query .= " WHERE description like '%$description%'"; } if(isset($_GET['price']) && !empty($_GET['price'])) { $price = explode('-', $_GET['price']); $lowPrice = (int)$price[0]; $highPrice = (int)$price[1]; $query .= " AND price BETWEEN $lowPrice AND $highPrice"; } $query .= " LIMIT 0, 10"; $result = mysql_query($query); while($row = mysql_fetch_assoc($result)) { $id = $row['id']; $image = $row['awImage']; $link = $row['link']; $description = $row['description']; $fulldescription = $row['fulldescription']; $price = $row['price']; echo "<div class='productdisplayshell'> <div class='productdisplayoutline'> <div class='productborder'><center> <a href='$link' target='_blank'><img src='$image' width=\"95%\" /></a> </center> </div></div> <div class='productdescriptionoutline'> <div class='productdescriptionbox'> <a href='$link' target='_blank' >$description</a> </div> <div class='productfulldescriptionbox'>$fulldescription</div> </div> <div class='productpriceoutline'> <div class='productpricebox'> <center>£ $price</center> </div> <div class='productbuybutton'> <center><a href='$link' target='_blank' ><img src=/images/buybutton.png /></a></center> </div> </div> </div>"; } if ($_GET['description'] == $description ) { echo 'Sorry, this product is not available. Please visit our <a href="http://www.ukhomefurniture.co.uk">Homepage</a>.'; } if( !$result = mysql_query($query) ) { echo "<br>Query string: $query<br>Produced error: " . mysql_error() . '<br>'; } ?> <?php function sanitizeString($string) { return mysql_real_escape_string($string); } $description = sanitizeString($_GET['description']); $query .= " WHERE description like '%$description%' LIMIT 0, 10"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/229569-get-url-for-second-query/#findComment-1182968 Share on other sites More sharing options...
justlukeyou Posted March 6, 2011 Author Share Posted March 6, 2011 Hi, My code does work. If I enter: php?description=furniture&price=1-200 Then it displays everything in the database which is furniture priced 1-200. I can add link to my site which is www.domain.co.uk.php?description=furniture But now I want to write www.domain.co.uk.php?description=(url term)&price=1-200 How do I add two filters together. The first filter is already in the URL bar. Quote Link to comment https://forums.phpfreaks.com/topic/229569-get-url-for-second-query/#findComment-1183567 Share on other sites More sharing options...
Pikachu2000 Posted March 6, 2011 Share Posted March 6, 2011 You build the query string, in the proper order, based on the values that are present in the URL. This is purely a conceptual example. there is no sanitization or error handling in this code, but it should be enough to give you a pretty good start on it. $query = 'SELECT id, name, description FROM table'; if( strlen($_GET['description']) > 0 || strlen($_GET['price']) > 0 ) { $query .= ' WHERE'; if( strlen($_GET['description']) > 0 ) { $query .= " description = '{$_GET['description']}'"; $description = TRUE; } if( strlen($_GET['price']) > 0 ) { if( $description === TRUE ) { $query .= " AND"; } $query .= " price = {$_GET['price']}"; } } Quote Link to comment https://forums.phpfreaks.com/topic/229569-get-url-for-second-query/#findComment-1183579 Share on other sites More sharing options...
justlukeyou Posted March 6, 2011 Author Share Posted March 6, 2011 Thanks, So how do write the link for it to read what is already in the URL bar? (url contents)+(&price=1-200) Quote Link to comment https://forums.phpfreaks.com/topic/229569-get-url-for-second-query/#findComment-1183583 Share on other sites More sharing options...
Pikachu2000 Posted March 6, 2011 Share Posted March 6, 2011 The variables that were submitted and are already in the $_GET array will be in the $_SERVER['QUERY_STRING'] superglobal. You can tack that on to the string for the link, as long as it doesn't conflict with what you're trying to add. <a href="http://www.somesite.com/page.php?new_variable1=text1&new_variable2=text2&<?php echo $_SERVER['QUERY_STRING']; ?>">Link</a> Quote Link to comment https://forums.phpfreaks.com/topic/229569-get-url-for-second-query/#findComment-1183586 Share on other sites More sharing options...
justlukeyou Posted March 6, 2011 Author Share Posted March 6, 2011 So do I actually put PHP into the link? Im trying to create something very similar to this where it has different filters with cab clicked in different orders. http://mydeco.com/c/furniture/sofas/258/129/ Quote Link to comment https://forums.phpfreaks.com/topic/229569-get-url-for-second-query/#findComment-1183609 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.