Jump to content

Get URL For Second Query


justlukeyou

Recommended Posts

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?

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 :confused:

Link to comment
Share on other sites

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>&#163; $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";



?> 

Link to comment
Share on other sites

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>&#163; $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";



?> 

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

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']}";
}
}

 

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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.