How to fetch value in multi select dropdown in php? - php

code:
<select data-placeholder="Click and Select Menu For Assign..." multiple class="chosen-select form-control" name="assign[]">
<?php
$main_menu = explode(",",$menu);
foreach($main_menu as $fetch)
{
?>
<option value="<?php echo $fetch; ?>"<?=($fetch==$fetch)? 'selected="selected"':''?>></option>
<?php
}
?>
<option value="null" style="color:red;">Select Menu For Assign</option>
<?php
$sql = "select menu from assign";
$result = mysqli_query($con,$sql);
while($row = mysqli_fetch_array($result))
{
echo "<option value='".$row['menu']."'>".$row['menu']."</option>";
}
?>
</select>
In this code I have create multi select dropdown. Now I want to fetch value inside the multi select dropdown like aaa bbb ccc but now its look like as we see in simple dropdown. So, How can I fix this issue ?Please help me.
Thank You

Related

How to display selected value in the bootstrap multiple dropdown from the database

I am using CodeIgniter, I have bootstrap multiple select dropdowns. I am getting all the drop-down name in the dropdown list.
Now I am on the edit page, I have to display the selected value in the drop-down.
Would you help me out in this issue?
I have data in a database like
venue_id
1,4
5,6,7
1
10,15,4,9
view
<select name="venue_id[]" id="venue_id" multiple="multiple" class="selectpicker form-control">
<?php
foreach($venue as $list){
echo '<option value="'.$list->venue_id.'">'.$list->venue.'</option>';
}
?>
<select name="venue_id[]" id="venue_id" multiple="multiple" class="selectpicker form-control">
<?php
$selected_array = explode(',',$current_venue_id);
foreach($venue as $list){
$mark_as_select = (in_array($list->venue_id,$selected_array)) ? 'selected' : NULL;
echo '<option value="'.$list->venue_id.'" '.$mark_as_select.'>'.$list->venue.'</option>';
}
?>
$current_venue_id which comes from the database.
Example $current_venue_id = 1,4 or $current_venue_id = 5,6,7 or
$current_venue_id = 1 or $current_venue_id = 10,15,4,9

How do I fix the selected item in a drop down box

I have edit form where I get info from database
<select name="table">
<?php
//fetch all tables from database
$user = $con->query("SELECT * FROM table") or die(mysql_error());
while($row = $table->fetch_object()) { ?>
<option value="<?php echo $row->tablename;?>">
<?php echo $row->tablename; ?>
</option> <?php }?> </select>
<label for="time">Time :</label>
<select name="time">
<option value="twotothree">2PM-3PM</option>
<option value="threetofour">3PM-4PM</option>
<option value="fourtofive">4PM-5PM</option>
<option value="fivetosix">5PM-6PM</option>
</select>
The names of the tables from drop-down menu are different. Everytime I select a table and a time slot and save the data, the selection goes back to the first row of the menu. Eg I select table 3 and 4PM-5PM after saving it goes back to table 1 and 2PM-3PM. I need to be fixed on the last selection as I might use 4PM-5PM for table 4 also. Any idea? Thanks
You can add selected attribute when rendering your select list, depending on $_POST variable, when it's available. For example for you table select element:
<select name="table">
<?php
$user = $con->query("SELECT * FROM table") or die(mysql_error());
while($row = $table->fetch_object()) { ?>
<option value="<?php echo $row->tablename;?>" <?php if (isset($_POST['table']) && $_POST['table'] == $row->tablename) echo 'selected'; ?> >
<?php echo $row->tablename; ?>
</option>
<?php }?>
</select>
In similar way you can do for time select element.

using php code within html to dynamically load a dropdown list

I am trying to use php code within html to grab content from a database to populate the values for a drop down menu (see below). When I run the php script on its own (test.php) I get all the expected values. When nested within html I get nothing but a single blank value under Select a Species. I would expect this to run through the while loop more than once as there are about 7 values returned and I would also expect the contents to include data derived from the table.
What I am attempting is possible correct??
Is it just an error with code (no errors popping up in the logs)
<label for="select">Species Observed : </label>
<select name="select" class="textfields" id="species">
<option id = "0">-- Select a Species -- </option>
<?php
$con = mysqli_connect("localhost","xxx","xxxx","databases");
$result = mysqli_query($con, "SELECT * FROM bc_species WHERE 1");
while ($row = $result->fetch_assoc()){
?>
<option><?php echo $row['species'];?></option>
<?php } ?>
</select>
<option><?php echo $row["species"]; ?></option>
<?php
$optionData = '<option id = "0">-- Select a Species -- </option>';
$con = mysqli_connect("localhost","xxx","xxxx","databases");
$result = mysqli_query($con, "SELECT * FROM bc_species WHERE 1");
while ($row = $result->fetch_assoc()){
$optionData .= "<option>".$row['species']."</option>" ;
}
?>
<label for="select">Species Observed : </label>
<select name="select" class="textfields" id="species">
<?php echo $optionData;?>
</select>
//Put your fetched data on an array then loop them like this
<?php
foreach ($list as $data => $item) {
?>
<option value="<?php echo $data?>">
<?php echo $data?>
</option>
<?php
}
?>
This should work:
<label for="select">Species Observed : </label>
<select name="select" class="textfields" id="species">
<option id = "0">-- Select a Species -- </option>
<?php
$con = mysqli_connect("localhost","xxx","xxxx","databases");
$result = mysqli_query($con, "SELECT * FROM bc_species");
while ($row = $result->fetch_assoc()){
echo '<option>'. $row["species"].'</option>
} ?>
</select>
You were not iterating through the values within the HTML <option> tags, so I moved those into the while loop. (notice the PHP section includes echoing those tags).
I also removed the unnecessary "SQL injection-looking" where 1 clause from your query.

Get data from database and put in SELECT TAG

I have three select tags (1)Category, (2)Sub-Category, (3)Product Description and I want to fill it with data from database. But what I want is to when I select eg. Office Supplies from the Category on the Sub Category will only display eg. Ballpen, Ruler, Notebook and when I select eg. Ballpen on the Product Description will only display the description of the Ballpen. Btw I had created some code made from PHP but it only fills the select tags. I hope someone will help me. This is for out Thesis. :)
<html>
<head>
<title>Test Select</title>
</head>
<body>
<?php
$query = "SELECT DISTINCT(`product_cate`) FROM `tbl_product` ";
$result = mysql_query($query);
?>
Category:
<select id="select1">
<option></option>
<?php while($data = mysql_fetch_array($result)){
$displayData = $data['product_cate'];
?>
<option value="<?php echo $displayData;?>"><?php echo $displayData; ?></option>
<?php } ?>
</select>
<?php
$query2 = "SELECT DISTINCT(`product_sub`) FROM `tbl_product`";
$result2 = mysql_query($query2);
?>
Sub Category:
<select id="select2">
<option></option>
<?php while($data2 = mysql_fetch_array($result2)){
$displayData2 = $data2['product_sub'];
?>
<option value="<?php echo $displayData2;?>"><?php echo $displayData2;?></option>
<?php }?>
</select>
<?php
$query3 = "SELECT DISTINCT(`product_desc`) FROM `tbl_product`";
$result3 = mysql_query($query3);
?>
Product Description:
<select id="select3">
<option></option>
<?php while($data3 = mysql_fetch_array($result3)){
$displayData3 = $data3['product_desc'];
?>
<option value="<?php echo $displayData3;?>"><?php echo $displayData3;?></option>
<?php }?>
</select>
</body>
</html>
General guide on the path to take would be to use jquery. Basically what will happen is that you'll load the first select ie Category. When the user selects a category, the change should fire up an ajax call to query the db and get sub directories from the category id posted. the call returns the sub category as json which you'll put in the select box. same plan for the product description
This can be a starting point and follow up from there:
jQuery dropdown dependent
Lots of similar questions here
You can just link options with an other attribute for example :
<select class="cat"><option>Category<option><select>
<select class="sub_cat"><option data-cat="Category">Sub-Category<option><select>
after that use can use javascript to hide unwanted categories for example (with jquery) :
$('.cat').change(function() {
$('.sub_cat option:not([data-cat="Category"])').hide();
});

Displaying a php mysql result with two values with a foreach loop in PHP

I have result that I want to display as a drop down menu. The query selects id and name from a table.
$usersQuery = "SELECT id, name
FROM users";
$usersResult = mysqli_query ($dbc, $usersQuery);
I want to use this result as a list in a drop down menu. This is what i have so far.
<select id="dropdown" name="dropdown">
<option value="select" selected="selected">Select</option>
<?php
while ($usersRow = mysqli_fetch_array($usersResult, MYSQLI_ASSOC)){
foreach ($usersRow as $value){
echo "<option value=\"$value\"";
echo ">$value</option>\n";
}
}
?>
</select>
this would work fine if I just wanted to display name as both the value and the display to the user. But what I want to do is use the selected id as "value" for the select option and I want to show the name selected to the user. I have tried this but it does not work.
<select id="dropdown" name="dropdown">
<option value="select" selected="selected">Select</option>
<?php
while ($usersRow = mysqli_fetch_array($usersResult, MYSQLI_ASSOC)){
foreach ($usersRow as $id=>$name){
echo "<option value=\"$id\"";
echo ">$name</option>\n";
}
}
?>
</select>
Any help would be great.
Thanks in advance.
mysqli_fetch_array() is a function which converts your query results into an array. which means you can display your values like you would with a normal array value.
<select id="dropdown" name="dropdown">
<option value="select" selected="selected">Select</option>
<?php
while ($usersRow = mysqli_fetch_array($usersResult, MYSQLI_ASSOC)){
echo "<option value=\"".$usersRow['id']."\"";
echo ">".$usersRow['name']."</option>\n";
}
?>
</select>
No need for the foreach iteration; mysqli_fetch_array() already provides an associative array. After each fetch do
// Assuming "id" is a numeric value
printf('<option value="%d">%s</option>', $usersRow['id'], $usersRow['name']);
The foreach is unnecessary - using a while loop on the mysqli_fetch_array command will return all the results with each row in an array - you can use it like so:
while ($usersRow = mysqli_fetch_array($usersResult, MYSQLI_ASSOC)){
echo "<option value=\"".$usersRow['id']."\"";
echo ">".$usersRow['name']."</option>\n";
}

Categories