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

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.

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.

Related

get list from table used to create "filter button"

This must be something trivial but long to explain. I cannot form good question for google. I dont know if I should get this result in SQL query or I need to change PHP part which includes sql query. (My knowledge is small, I am making my own very simple planner website but im stuck on creating button for filtering options.)
Imagine I have table named “Records” with 3 columns “ID, Item, Category”.
In categories I write all data in CAPITAL LETTERS and categories can be repeating.
So example table Records looks like this(in columns layout like this: id-item-CATEGORY):
1-sleep-HEALTH 2-ate breakfast-FOOD&DRINK 3-drank
coffee-FOOD&DRINK 4-took eyedrops-HEALTH 5-tram to
work-TRAVEL
I know how to list all data from column Category, but I want to avoid repeating categories. I need to get list of categories like this (Health and Food&drink does not duplicate):
HEALTH FOOD&DRINK TRAVEL
So with this list I can than via php generate html select tag options.
In php part I would use something like this:
$query1=mysqli_query($db,"select * from Records asc”);
while($query2=mysqli_fetch_array($query1)){
echo “<option value=“.$query2[‘category_option’].”>”.$query2[‘category_option’].”</option>”
};
So the result HTML will look something like this.
<select>
  <option value="HEALTH">HEALTH</option>
  <option value="FOOD&DRINK">FOOD&DRINK</option>
  <option value="TRAVEL">TRAVEL</option>
</select>
Can you please point me right direction? Or mention some best practice to reach this kind of functionality. Thanks to all which try to help.
Use DISTINCT in your SQL:
SELECT DISTINCT Category FROM Records
No duplicates will be returned.
Have a play with it on this link https://www.w3schools.com/Sql/sql_distinct.asp
Note that now we are only fetching one column here instead of *.
You can also use GROUP BY:
SELECT * FROM Records GROUP BY Category
https://www.w3schools.com/Sql/sql_groupby.asp

SQL query Selecting categories and details without repeating

I am writing a very simple sql query but for some reason I am unable to pull it through. I have mysql table with the following fields; cities, towns and persons. There are several cities, with several towns, which have several people. I want to be able to select a city, say London and be able to see all the towns under london and be able to see everyone living in those towns. What is happening at the moment is that when I query all the tables using their ids, I get a repeat of categories.
I am attaching a picture of what I am trying to achieve
What I am achieveing is
city1 -town1 - person1
city1-town 2-person 1
$query = "SELECT *
FROM cities, Transport, town
WHERE cities.id = town.city
AND town.townID = person.townID
GROUP BY cities.cityName"
I do not want the city to repeat, but at the same time I want a list of everything in that city

Drop Down list to affect another Drop Down list results - using PHP Queries

I have 2 Drop Down menus, both of which get their values from my database.
The 2 drop downs are - Town & County.
Obviously, only certain Towns relate to certain counties.
In order to capture this, they are stored within the database as such:
Leeds, Morley
Leeds, Chapel Town
London, Essex etc..
I need to force the user to choose the County first (which I have done by disable/enable). How ever, on selection of County, I need to refresh the 2nd drop down box with the related 'Town' values from the database.
The current code for 'Getting the town' is as such:
echo "<option value='0' >Choose a Town</option>";
$town_se=$_SESSION['town'];
if(isset($county_se)){
$locat=mysql_query("SELECT DISTINCT town_name FROM locations WHERE county_name='$county_se'");
}else{
$locat=mysql_query("SELECT DISTINCT town_name FROM locations ");
}
while($row=mysql_fetch_assoc($locat)){
$town=$row['town_name'];
echo "<option value='$town' >$town</option>";
}
Of course, this doesn't work! Is there a simpler way to replicate the results from the first drop down?
Many thanks in advance.
I'm having a hard time decoding what is going on in your example code. But just to make sure that we are on the same page... PHP is server-side and HTML is client-side. The two cannot interact without something in-between, like a new page-request, javascript or AJAX.
If the data you are presenting is reasonable in size, I would probably just recommend putting all that data in the HTML from the start, but hiding it. That is, create one dropdown for the towns in every county, but hide them all until the user selects a county. (Requires a bit of JavaScript)
Another common solution is to simply submit (and refresh) the page when the user selects a county, thus, you generate a new page with the town dropdown populated with towns for that county. (Requires a bit of JavaScript)
If you want to go really high-tech you could use AJAX and dynamically load the data into the second dropdown when the user selects a county. Although this requires that you have a pretty good understanding of PHP and JS.

Database Schema For Populating Select Lists with PHP/MySQL

I have a two part question with no code to show, but i hope you can help:
Essentially, i have a website which lets businesses create a profile for themselves (original table schema here).
Now within the site I want a form with three select lists in it: the first select list is for business catagory/tag - I have managed to create a query which will return entries from my main business registration table by using a Toxi solution.
However i also want a second select list with countries or regions, and a third select list with states/counties which will load automatically BASED on the selection from the Country select list.
Then naturally, on submitting the form, i want to return the results to the visitor.
So, apart from wondering how to achieve this much in PHP/MySQL, i have a more fundamental question about whether to set up the countries/states 'tagging' or 'catagories' field as another Toxi solution, or if there's a better way to do it. For instance, should i/can i just create seperate tables to populate the select lists with states/counties based on country selection, and have the actual country and state fields that a business selects on registration, stored within my main business table, and just match the strings when the user hits submit, in order to reduce queries to MySql and improve speed?
As always, your advice and help is greatly appreciated.
Thanks
Dan
Think about the "full name" of a state (or whatever political division is one level narrower than "country"). In the Americas, here are some "full names" of states.
California, US
Connecticut, US
Ontario, CA
Sonora, MX
Lara, VE
Base your lists on a two-column table of the full names of states.
ISO country codes
The critical point is that your selection lists shouldn't allow values like "Alabama, MX" or "Sonora, US".

Select country first from a dropdown menu, then region and then city?

I'm using html SELECT to make 3 drop-down menus. One for a list of countries, one for regions and one for cities.
They all have different tables in the database and they are linked to each other with foreign keys.
I want to force the user to first select a country while the other two menus are locked. When he has selected a country he will be able to select a region, and then a city. How do I do this?
Which part are you specifically having problems with?
1) Populate the countries list when the user loads the page, other 2 menus are disabled. (SELECT * FROM Countries;)
2) When the user selects a country, send an AJAX request to the server containing the country name. (SELECT * FROM Regions WHERE id = country_id;)
3) When you receive the AJAX response, populate the regions list and enable it.
4) Repeat steps 2 and 3 for the city name.
You've got several possibilities.
You could write a single select that retrieves all the country-region-city combinations at one time, and use the rows to create Javascript objects representing this data at load time. Then create a Javascript method that updates the dependent rows when a new country or region is selected.
You could write 3 select statements, 1 for countries, 1 for regions for a given country, and 1 for cities for a given region. The countries select would run at page load time, and the other queries could be performed using an Ajax request when country and region are selected.
Try this premade script and start accepting answers.., people might be more willing to help.

Categories