Question

This is probably an easy solution but I'm drawing a blank. I'm trying to create a PHP filter based on values such as $, $$, $$$, $$$$, and $$$$$. I would like to be able to select more than price and have it filter. My SQL table looks like:
price_id price_name
1           $
2           $$
3           $$$
4           $$$$
5           $$$$$

My url looks like www.example.com/search.php?address=567&price_id=1&price_id=2. When I enter the GET below, I am able to get the last price price_id=2 but not price_id=1. Am I able to get both price_ids from the URL and how?
$price_id = $_GET['price_id']; echo ''.$price_id.'';

Thanks a bunch!

Was it helpful?

Solution

PHP is not doing it automatically for you because all the variables have the same name: price_id=1 gets overwritten by price_id=2. There are two ways around this:

If you don't control the URL, can do it manually by using then explode('&', $_SERVER['QUERY_STRING']).

Alternatively, if you have control of the URL being generated, just put square brackets on the end: price_id[]=1&price_id[]=2

OTHER TIPS

 www.example.com/search.php?address=567&price_id=1&price_id=2

change this into

 www.example.com/search.php?address=567&price_id1=1&price_id2=2

then

do

$price_id1 = $_GET['price_id1'];
$price_id2 = $_GET['price_id2'];

echo ''.$price_id1.''.$price_id2;

try and tell

Make price_id and array []:

search.php?address=567&price_id[]=1&price_id[]=2

Then you have $_GET['price_id'][0] and $_GET['price_id'][1].

The problem is that you can't have pass the same parameter twice. It would be like declaring $value = 1; $value = 2 and then trying to read 1 and 2 from value, if that makes sense. You can fix this by putting a special character in between the values in the price_id. Something like price_id=1*2 or you could simply start at price_id and then for each new id you could increment it to be price_id1, price_id2, etc. I like the first because you can explode it and not have to check if other ones are set, but it's all preference.

You get the second price because both have the same id, Try using post i.e

<form method="post" action ="act.php">
<input name=price[1]>
<input name=price[2]>
</form>

And get the values using post
//act.php
$prices = $_POST['price'];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top