I want to make a dynamic filter system based on a html form with multiple options, some like that:
<form>
<select>
<option></option>
-//-
</select>
<select>
<option></option>
--//--
</select>
</form>
and make a sql query but with condition from form, and every option have an "All" option which means no condition.
So i was thinking to make something like that, with multiple for and N^2 if condition, but is to much code and it's not faster... to make 16 if-uri and 16 SELECT for a 4 option form ...
So, know someone a faster solution ?
Thanks.
Edit
So let say i have a database users with ID, Name, Age, City.
I make a form with 3 option (1 for Name, 1 for Age, 1 for City) so i can see only some users, let say i want all users which have name Nick and it's from Miami.
I need to my SELECT condition to exclude the condition for AGE, like now select will be :Select * from Users where Name = 'Nick' and City='Miami', but if i want to see users with Age= 18 and City NY my select will need to exclude the condition for Name, and be like: Select * from Users where Age= 18 and City='NY'.
Related
I created an html that has a formulary that let you choose between four different options:
<select name="especie">
<option value = "1"> Hyundai </option>
<option value = "2"> Renault </option>
<option value = "3"> Ford </option>
<option value = "4"> Fiat </option>
</select>
Each of these options correspond to one table in a mySQL database (let´s say it´s called companies), and have the same variables. The html then lets you perform the query, and I want it to display the information according to the table you have previously chosen.
$query = "SELECT * FROM $table WHERE brand LIKE ´%$brand%´;";
So what I want is to create a PHP that chooses dynamically the table ($table) depending on the chosen option by the user (in the first chuck of code).
Any help would be really appreciated it.
Without re designing your DB structure - which could be the best course of action, there are two easy ways to do this. You could do it basically the way you said. If choosing this method you need to make sure that the table is being selected by a key from an array of tables so it is a safe input like so:
$tables = ["table1" => "table1","table2" =>"table2"];
$query = "SELECT * FROM ".$tables[$table_key_from_input]." WHERE brand LIKE ´%$brand%´;";
But this is not ideal as, when tables are added removed all code like this needs to be updated.
Another way of getting the desired outcome would be to make a database view which is a union of all the tables (plus the table name in an extra column) then you can just select from the view every time using the table as a variable supplied by the user input.
You can learn about views here: https://www.w3schools.com/sql/sql_view.asp
And see how to make a union here: https://www.w3schools.com/sql/sql_union.asp
The view code would look a little like this:
CREATE VIEW view_name AS
SELECT *, 'table1' AS 'table' FROM table1
UNION
SELECT *, 'table2' AS 'table' FROM table2
Then your query would be like this:
$query = "SELECT * FROM view_name WHERE brand LIKE ´%$brand%´ AND table = $table;";
(But ideally using PDO!)
Also if you do it this way if you ever re think your db structure and perhaps do what was suggested by El_Vanja in the comments you can, and by deleting the view and giving the table the same name the code wouldn't even have to change!
I have a form with this select:
<select name="territory[]"><option vaue="None Selected">Please Select</option>
<option value="Argentina">Argentina</option>
<option value="Australia">Australia</option>
// And so on...
</select>
Next to the select option is a button where they can add another country with the same select options. Now when they submit I want them to go into one column in the database called COUNTRIES. So if a person selects 3 countries such as Argentina, Australia and New Zealand, they will display in the database column countries like:
COUNTRIES
------------------
Argentina, Australia, New Zealand
I have been looking and UNION has popped up but I am unsure how to use this as other fields are single entries.
INSERT into b_project (NAME,COUNTRIES, CLIENT)
VALUES ('$_POST[name]','$_POST[countries]','$_POST[client]')
I have done it before where I have inserted them onto new rows in the table using:
foreach ($_POST['name'] as $index => $id)
Then the insert statement with '".$_POST['countries'][$index]."'
But I need all the values in one column. Your advice would be much appreciated.
The simple (but a workaround) answer:
$countries = implode(',', $_POST['countries']);
This will glue the countries with a comma.
The way you should do it:
Use another table in your table, in which you provide all the countries.
Then when a user submits the form, you need another table which map the country_id with the b_project_id. For each country you will have a record in that table.
It little bit more work, but that's the way it should be done.
Last piece of advice:
Please sanitize the inputs. You do not know for sure what a user will send with the POST data. It can be everything...
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.
In my MySQL I have under the table used where I save the make, model, price of a car and there is a column named car_parts for the extras like gps system, air bags.
This column saves the values from many checkboxes in this format a value, b value, f value, g value.
In my page I have a search form, where the user select from dropdown menus some values (like model and price) and through checkboxes some car parts. As I said the car parts are saved in the column car_parts.
This is how I constructed my query, since I allow the user to submit the form with some values empty:
$conditions = array();
if (!empty($colour)) $conditions[] = "colour = '".mysql_real_escape_string($colour)."'";
...
...
...
$conditionString = "WHERE ".implode(' AND ', $conditions);
My thought is that I need to have the checkboxes with different names like
<input type="checkbox" name="cruisecontrol" value="Cruise Control"> Cruise Control
and make use of the $_POST["cruisecontrol"]; in the database.
My question is how can the Cruise Control or any others, to be checked if these values are contained into the car_parts a,d,Cruise Control,j,t,r values and construct the query?
Depends how you're storing the information in the car_parts field. But I think the LIKE clause may be your friend here. Say the user checked 2 boxes these will be set in your post array. I would have the check boxes an array of post values like
<input type="checkbox" name="options[]" value="cruisecontrol" />
<input type="checkbix" name="options[]" value="radio" />
Then you can grab the options values and use them in your query.. supposeing you want a car with all checked options and both options are checked...
$whereClause = "WHERE `car_parts` LIKE '%".implode("%' AND LIKE '%",$_POST[options])."%'"
should produce something like
WHERE `car_parts` LIKE '%cruisecontrol%' AND LIKE '%radio%'
This will return results where car_parts contains both cruise control and radio.
Hope that helps. Also don't forget to sanitize the data before feeding it to your query!
Assume a table bank contains 3 fields, id, name, and balance.
I am trying to understand how to select all fields from this table and add another custom field to the result set as follows:
Query (something like)
SELECT * FROM bank WHERE balance+10 AS plusTen AND id != 12
PHP
echo $row['plusTen']; //echos whatever balance was for this record + 10 added to it
Is this even possible?
I appreciate any suggestions.
SELECT *, balance+10 AS plusTen
FROM ...
etc...
you can create virtual fields in a WHERE clause, if you insist, but things in where clauses don't get returned to the client, because they simply act as filters. You need to actually create the virtual field in the field portion of the query, as above.
You need to include the additional field in the SELECT not the WHERE part of the statment, like so:
SELECT *,
balance+10 AS plusTen
FROM bank
WHERE id != 12
To add more fields, just separate them with a comma after the SELECT part of the statement