MYSQL make values selected php - 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">

Related

check correctly checkboxes from mysql (php)

I have a problem with checkboxes.
I have some project they you can filter at categories.
So the problem is when you want edit a project that automatic the correct category are checked.
here you see a project but the categories are empty
In mine databank I use is a combo table - one to insert my categories and one for the project and there I have a combo table from.
Here you see my code for display the checkboxes and select for editing so now I looking for some help, for checked the correctly checkbox that belongs to that project.
<div id="categoriefilter">
<?php
$sql = "SELECT * FROM `categories` ORDER BY `id` ASC";
$cats = DB::getResult($sql);
foreach($cats as $cat){
?>
<label>
<input name="cat[]" type="checkbox" value="<?php echo $cat['id']; ?>" >
<?php echo $cat['categorienaam']; ?>
</label>
<?php
}
?>
</div>
If I understand your question correctly. You need 3 tables here
Table1 : projects id, name, ...
Table2: categories id, name, ...
Table3: projects_categories project_id, category_id, ...
The third table is needed to store the categories which are selected for projects
And inside your project page
You need to do the followings:
Select your project by its id
Select categories
Select categories related to the project ( from Table3 )
Here is example ( this is not a working code, just a showcase )
// your project
$project = DB::getResult("SELECT * FROM project WHERE id = :id "); // <== id here
// list of categories
$categories = DB::getResult("SELECT * FROM categories ");
// list of categories for this project
$project_categories = DB::getResult("SELECT category_id FROM projects_categories WHERE project_id = :project_id ");
$project_categories = (array) $project_categories; // in case DB::getResult is not array
<?php
foreach($categories as $category) :
?>
<label>
<input name="cat[]"
<?php if(in_array($category['id'], $project_categories)) echo 'checked="checked"'; ?>
type="checkbox" value="<?= $category['id']; ?>" >
<?php echo $category['name']; ?>
</label>
<?php endforeach; ?>
I'm not down voter. If you knew your problem, you probably mightn't post the question.
You don't include the details code. However for the part as far as I'm understanding,
You should run database UPDATE query inside foreach loop. You don't need to keep the html input inside the loop.
<?php
// check if checkbox is checked
if (!empty($_POST['cat'])){
foreach($_POST['cat'] as $cat){
// run update query here
$sql = "UPDATE `categories` SET `categorienaam` = $whatever WHERE `id` = $cat";
$result = DB::getResult($sql);
}
}
?>
<label>
<input name="cat[]" type="checkbox" value="<?php echo $RowOfApplicableSelectQuery['id']; ?>" >
</label>
$RowOfApplicableSelectQuery comes from the query that you are using to fetch and display your record primarily.
I hope it gives you a clue about what are you looking for.

How to retrieve data using select box with 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.

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.

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