0

I have made a webpage for a school project where I have a series of selection boxes (some are independent and some are depended on another) to make selections and then apply filter to make a query. It is still in test-mode and working fine for single selections. You can find the webpage here: http://gorevler.awardspace.biz/realdeal03.html

For example, when I select Europe from Region, Austria from Country, Plastics from Sector, Plastic Raw Materials from Sub-Sector and Polybutylene from Product, and click apply filter it gives out the result.

But, when I select Europe AND North America from Region, Austria from Country, Plastics from Sector, Plastic Raw Materials from Sub-Sector and Polybutylene from Product, and click apply filter, it gives only one result. but there are two records matching this filter in the database. So it does not recognise my multiple selection. Any advice about how I could make it work so that when I make multiple selection on a box and apply filter, it gives all the results including all the selection?

I guess it is something about the PHP coding, and I tried different things but no success yet. Since I am not a programmer, I don't seem to understand where the problem lays. Any advice about the coding would be appreciated.

barutto
  • 104
  • 1
  • 14

1 Answers1

0

Is it because when you select two regions your query becomes: SELECT ... FROM ... WHERE region ='North America' AND region = 'Europe' which means you will never get any results from the database as the region cannot be both? If so you'll need to make it so you use "OR" statements for multiple selected regions.

There are numerous ways of completing this. First of all you need a way of dealing with multiple inputs for your region, rather than just defining the one input in your php code. Basically once you have capture your multiple regiones then your query line is the only line that needs to change. This can become:

$sql = mysql_query("SELECT * from gorevler WHERE region IN ('%$bolge%','%$bolge2%','%bolge3%') AND country LIKE    '%$ulke%' AND sector LIKE '%$sektor%' AND sub_sector LIKE '%$altsektor%' AND product LIKE   '%$urun%'");

Or

$sql = mysql_query("SELECT * from gorevler WHERE (region = '$bolge' OR region = '$bolge2' OR region = '$bolge3) AND country LIKE    '%$ulke%' AND sector LIKE '%$sektor%' AND sub_sector LIKE '%$altsektor%' AND product LIKE   '%$urun%'");

EDIT with some pointers on your last question:

It just needs to come from your HTML. So if you select more than one region then it will post these to your PHP page. For example:

<option value="" data-filter-type="" selected="selected" id="europe">Europe</option>

<option value="" data-filter-type="" selected="selected" id="usa">USA</option>

then in your PHP

$bolge=$_POST['europe];
$bolge2=$_POST['usa'];

etc.

That's probably the quick and dirtiest method anyway.

d1ll1nger
  • 1,571
  • 12
  • 16
  • Yes you are right. When I select Europe and North America on the box, I'd like the result to show all records including Europe OR North America. So should I make a change on PHP codes below?: `$bolge=$_POST['filtre_region']; $sql = mysql_query("SELECT * from gorevler WHERE region LIKE '%$bolge%' AND country LIKE '%$ulke%' AND sector LIKE '%$sektor%' AND sub_sector LIKE '%$altsektor%' AND product LIKE '%$urun%'");` – barutto May 20 '14 at 17:30
  • In which case you'd need to build your query dynamically so if more regions are selected then the more clauses are added. So if region is america or europe then the query should be select ... from .. where ( region ='europe' or region ='usa') and ... ; that sort of query. – d1ll1nger May 20 '14 at 21:21
  • So do you think I should try sql IN operator? Or something else? I'd really appreciate it if you could give me the coding hint. – barutto May 21 '14 at 14:48
  • Thanks mate this is exactly what I am looking for! Just one last question: Do you have any advice on revising Php file foe multiple region selection? I know I am asking too much but that would be great help for me. – barutto May 22 '14 at 10:40
  • No probs. I've added a little more to the bottom of the answer. – d1ll1nger May 22 '14 at 13:43