Selecting Multiple Options in Selection Boxes and Getting Results From Database?

依然范特西╮ 提交于 2019-12-11 20:44:47

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/23745079/selecting-multiple-options-in-selection-boxes-and-getting-results-from-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!