How to retrieve data using select box with PHP? - php

I have more than 100 rows in an database and they are from different subjects, like Database lessons or Network lessons. I want that the user can choose what questions from those subjects he want to display. So I was thinking in using one select box, but I don't know how to handle the selected box with the database query. How could I do that ?
My code currently :
<div class = "container-fluid">
<div class = "container">
<?php
$query = "SELECT def_conteudo FROM conteudo WHERE nro_conteudo BETWEEN 1 AND 100";
$result = mysqli_query($con,$query);
while($fetch = mysqli_fetch_row($result)){
echo "<li>" . $fetch[0] . "</li>";
}
?>
</div>
</div>
The problem is that he just shows all the lessons from different subjects here, but I want to give the user the choice to choose what questions he want to display.
Example :
If the user want to display only Database lessons he would choose through the select box and would appear at the screen all questions from this particular subject, not all the subjects questions.

The HTML <select> element takes a list of <option> elements:
<select>
<option value="1">Option 1</option>
<option value="2">Option 2</option>
</select>
That snippet will produce a drop-down list to pick from the options Option 1 and Option 2 whose values are 1 and 2 respectively: https://jsfiddle.net/ux245gz3/
For your case, every <li> element you're creating should be an <option> inside of a <select> block:
<div class="container-fluid">
<div class="container">
<select id="subject_selection">
<?php
$query = "SELECT def_conteudo FROM conteudo WHERE nro_conteudo BETWEEN 1 AND 100";
$result = mysqli_query($con,$query);
while($fetch = mysqli_fetch_row($result)){
echo "<option value=\"$fetch[0]\">" . $fetch[0] . "</option>";
}
?>
</select>
</div>
</div>
This will produce a list of each of the names of the subjects with the value of the selection set to the displayed name. The MDN article on <select> will have more information about this.
Update to answer the full question:
You'll want to update the query to select a display value and the ID for each subject (assuming the primary key of the 'conteudo' table is called 'id':
SELECT id, def_conteudo FROM conteudo WHERE nro_conteudo BETWEEN 1 AND 100;
Then you can use the id as the value for each option:
<?php
$query = "SELECT id, def_conteudo FROM conteudo WHERE nro_conteudo BETWEEN 1 AND 100";
$result = mysqli_query($con, $query);
while ($fetch = mysqli_fetch_row($result)) {
echo "<option value=\"$fetch[0]\">$fetch[1]</option>";
}
?>
Then, once the user selects an option from the dropdown you can perform a GET to the page and query against the questions or lessons that have the proper relationship to the selected subject via the subject's id submitted by the form.
SELECT * FROM <licao/pergunta> WHERE contuedo_id = $_GET["id"];
As a friendly aside, at this point you should look into using something like PHP's PDO library to automatically handle sanitizing your dynamically generated SQL statements.

Related

select two values in one option html form php mysql

how i can select two values in one form select html?
how explode exactly?
this is my table
id name divition
1 john IT
2 blair ACCOUNTING
3 Fira Finance
i have form like this
<select name="select" id="divi">
<option value="it">IT</option>
<option value="ACCOUNTING.FINANCE">Accounting Finance</option>
and the php process will be like this
if (isset($_POST['submit'])) {
$divi = $_POST['divi'];
$divi = explode(".",$divi);
$sql = "SELECT * FROM table WHERE divition = '$divi'";
i need show data when user select Acc & Finance it will show all name with divition Accounting and finance.
Thank you so much
An option is to loop through the array $divi
like:
if (isset($_POST['submit'])) {
$divi = $_POST['divi'];
$divi = explode(".",$divi);
$sql = "";
foreach($divi as $field) {
$sql = "SELECT * FROM table WHERE divition = '$field'";
}
How can you do that -
Accounting & Finance are in different row.
Instead of that use multiple select option Tag values.

Selecting all values if nothing is selected from drop-down list

I have a from by which I generate data after submitting the drop-down values of form to another page.
<form method="post" action="details.php">
<select name="Station">
<option value="All">All</option>
<option value="Home">Home</option>
<option value="Office">Office</option>
</select>
<select name="Section">
<option value="All">All</option>
<option value="Living">Living Room</option>
<option value="Dinning">Dinning Room</option>
<option value="Bathroom">Bathroom</option>
</select>
<input type="submit">
</form>
Based on the above form I have Station and Section Columns in a MySQL database with the values that I mentioned.
It works fine if I select Home from Station and Bathroom from Section, But if I select All from Station and Bathroom from Section then I get an error because I don't have a record with the value All in column named Station. Instead I have records with values Home and Office (by All I mean that both Home and Office should be generated).
My question is how to generate details of both stations when I select All from drop-down list.
In my details.php page I have:
$execItems = $conn->query("SELECT StationName, SectionName FROM profiles WHERE Station = '$Station' AND Section = '$Section'");
It works fine if I select Home or Office from Station, but if I select All from Station then it won't work because there is no value by the name of All in Station column (By All I meant that both Home and Office should be selected.).
you need to build dynamic where clause based on input like this
<?php
$where ="1=1";
if(isset($_POST['Station']) && !empty($_POST['Station']) && $_POST['Station'] !='All')
{
$where .=" and station = '$Station'";
}
if(isset($_POST['Section']) && !empty($_POST['Section']) && $_POST['Section'] !='All')
{
$where .=" and section= '$section'";
}
"SELECT StationName, SectionName FROM profiles WHERE".$where
While a dynamically written WHERE clause would be a better way to go, a quick fix is to adjust the WHERE clause to account for ALL being passed in from the form.
WHERE (station = '$Station' OR '$Station' = 'ALL') AND (Section = '$Section' OR '$Section' = 'ALL')
If someone is to choose, for station the form value ALL and the value Dining for the Section form, then this WHERE clause becomes:
WHERE (station = 'ALL' or 'ALL' = 'ALL') AND (Section = 'Dining' or 'Dining' = All)
That first OR will generate a TRUE because 'All' = 'All' and the second will return TRUE in the event that the record being tested has a section value of Dining.

fetch record in combobox2 if index of combobox1 is changed i.e do it without pressing button (if index of other combobox is changed) using PHP

I'm working on a project in which I have 2 combobox (drop down lists) I want to display record in combobox2 as the index of combobox1 is changed.
Example
<select name = "semester">
<option value = "1st">1st</option>
<option value = "2nd">2nd</option>
<option value = "3rd">3rd</option>
</select>
<select name = "course">
<?php
require "con.php";
$query = mysql_query(select courses from course_tbl where semester = ".$_POST['semester']);
while($row = mysql_fetch_row($query))
{
echo "<option value = '$row[0]'>$row[0]</option>";
}
?>
</select>
means load all courses of the semester that user selects. means if index of semester is changed then immediately load all courses in other combobox. Please help.

MYSQL make values selected php

I have 3 SQL tables, "artists", "tracks" and "tracks_artists" (=linking table, because one track can have multiple artists, I'm restricted to use a linking table).
I now have an "edit page" where I want to show 4 selects where you can choose an arists, but with the track's artists already selected. So if an track has 2 artists, I want the first 2 selects to display the previously selected artists (alongside with all the others artists in my database) and the other 2 selects just to show all the artists in the select (with nobody selected).
I'm now using this, but this only works when the track has just one artist:
<select name="edit_artist">
<?php
$query_artist = "SELECT * FROM artists";
$result_artist = mysql_query($query_artist) or die(mysql_error());
while ($row_artist = mysql_fetch_array($result_artist)) {?>
<?php
$query_ta = "SELECT artistID FROM tracks_artists WHERE trackID = $trackID";
$result_ta = mysql_query($query_ta);
while ($row_ta = mysql_fetch_array($result_ta)){
if($row_ta[0]==$row_artist[artistID]){$selected = "selected='selected'";}
else {$selected ="";}
}
?>
<option value="<?php echo $row_artist[artistID]?>" <?php echo $selected;?>><?php echo $row_artist[name];?></option>
<?php }?>
</select>
You need to use the multiple attribute on your select
<select name="edit_artist" multiple="multiple">
If you want to submit this you'll need to make the edit_artist name an array using brackets
<select name="edit_artist[]" multiple="multiple">

How to display specific value from SQL in a dropdown depending on another condition

I need some help for a problem that I have been stuck with for a while.
I have a list with ads showing vehicles of different categories - cars, motorcycles, trucks, boats etc. I have a dropdown that should work as a filter. Depending on what vehicles are in the list, the dropdown values should be equal to the category of the vehicle.
For example, if in the list there are 3 cars: Audi, BMW and Porsche and 2 boats: Cranchi and Azimut, the dropdown should show 5 values: Audi, BMW, Porsche, Cranchi, Azimut.
However, for the example above i only manage to show in the dropdown all car and boat makes from the DB, but not only the relevant to the list entries.
Here is my code, any ideas will be appreciated:
<option value="">Please Select</option>
<?php
if($all=='ALL')
{
foreach($arrVehicleType as $arrVehicleTypes){
$qry_makes_all = "SELECT * FROM ".$tblprefix."makes WHERE status ='1' AND type = '".$arrVehicleTypes."' ORDER BY title ASC";
$rs_makes_all = $db->Execute($qry_makes_all);
while(!$rs_makes_all->EOF){
$sql_query_all_all = "SELECT type FROM ".$tblprefix."vehicles WHERE user_dealer_id = '".$DealeId."' AND make='".$rs_makes_all->fields['id']."' AND user_dealer_type = 2 AND completed_status = 1 AND status = 1 ";
$rs_results_all_all = $db -> Execute($sql_query_all_all);
$TYPE = $rs_results_all_all -> fields['type'];
?>
<option value="<?php echo $rs_makes_all->fields['id'];?>,<?php echo $rs_makes_all->fields['title']." ($vehicletext)";?>
</option>
<?php $rs_makes_all->MoveNext(); } }} else {
while(!$rs_makes->EOF){?>
<option value="<?php echo $rs_makes->fields['id'];?>,,<?php echo $arrVehicleType[0];?>">
<?php echo $rs_makes->fields['title'];?>
</option>
<?php
$rs_makes->MoveNext(); } } ?>
</select>
Use DISTINCT in Mysql.
$qry_makes_all = "SELECT DISTINCT /* list column names */ FROM ".$tblprefix."makes WHERE status ='1' AND type = '".$arrVehicleTypes."' ORDER BY title ASC";
This will select all the distinct values from your table.

Categories