PHP Bootstrap multi select options - php

In my code I am using PHP which populates the bootstrap multi selectpicker from MySQL database but the problem is it only selects the last option instead of multiple options, I don't know what I am missing in it? Here is my my code
function MultiBindCombo($tablenames, $columnnames1, $columnnames2, $comboname, $selectedopt) {
global $conn;
$sql="SELECT ". $columnnames1. ", " . $columnnames2 . " FROM ". $tablenames;
$result = mysqli_query($conn, $sql);
if( ! $result ) {
echo mysql_error();
exit;
}
echo '<select class="selectpicker form-control" data-live-search="true" name="' . $comboname . '" multiple="multiple">';
$array = explode(',', $selectedopt);
while ($row=mysqli_fetch_array($result)) {
foreach ($array as $select_option){
if($row[$columnnames1] == $select_option) {
$print_selected = 'selected';
} else {
$print_selected = '';
}
echo $select_option;
}
echo '<option data-tokens="' . $row[$columnnames1] . '" value="' . $row[$columnnames1] . '"' .$print_selected. '>' . $row[$columnnames2] . '</option>';
}
echo '</select>';
}

To get all selected values in a multiselect you need to use name attribute with []:
echo '<select class="selectpicker form-control" data-live-search="true" name="' . $comboname . '[]" multiple="multiple">';
After that you can iterate over $_POST[$comboname] with a foreach.
Update:
Let's look closer to your code here:
while ($row=mysqli_fetch_array($result)) {
foreach ($array as $select_option){
// You iterate over every value of array, so in the end
// `$print_selected` is defined depending on value of the
// last `$select_option`
if($row[$columnnames1] == $select_option) {
$print_selected = 'selected';
} else {
$print_selected = '';
}
// remove this. Why you echo `$select_option`?
echo $select_option;
}
echo '<option data-tokens="' . $row[$columnnames1] . '" value="' . $row[$columnnames1] . '"' .$print_selected. '>' . $row[$columnnames2] . '</option>';
}
Rewrite it as:
while ($row=mysqli_fetch_array($result)) {
$print_selected = '';
foreach ($array as $select_option){
if($row[$columnnames1] == $select_option) {
$print_selected = 'selected';
// break `foreach` as you found the right item
break;
}
}
// if right item is not found - `$print_selected` is empty
echo '<option data-tokens="' . $row[$columnnames1] . '" value="' . $row[$columnnames1] . '"' .$print_selected. '>' . $row[$columnnames2] . '</option>';
}

Related

Create months as options for select tag using php

I need to create a select tag with months as options using php
like this:
<option value='01'>JAN</option>
<option value='02'>FEB</option>
... and so on
here is my try - without success
<select>
<?php
$str = '{"01":"JAN","02":"FEB","03":"MAR","04":"APR","05":"MAY","06":"JUN","07":"JUL","08":"AUG","09":"SEP","10":"OCT","11":"NOV","12":"DEC"}';
$obj = new stdClass(json_decode($str));
foreach($obj->$key as $key=>$val){
echo "<option value = '" . $key . "'>" . $val . "</option>";}
?>
</select>
<?php
for($month = 1; $month <= 12; $month++) {
echo '<option value="' . $month . '">' . strtoupper(DateTime::createFromFormat('!m', $month)->format('M')) . '</option>';
}
Dont make life so complicated
<?php
$arr= ['01'=>'JAN', '02'=>'FEB', '03'=>'MAR', '04'=>'APR',
'05'=>'MAY', '06'=>'JUN', '07'=>'JUL', '08'=>'AUG',
'09'=>'SEP', '10'=>'OCT', '11'=>'NOV', '12'=>'DEC'
];
foreach($arr as $key=>$val){
echo "<option value = '" . $key . "'>" . $val . "</option>";
}
?>

how can i get the selected value of dropdown list in php?

i have seen several question but i can not find any answer?
i am add a some value to dropdown through the loop and want to show the value when i select one value from dropdown box .but i can not find .
$items[] = $file ;
echo '<select name="value">';
foreach($items as $video){
echo '<option value="' . $video . '">' . $video . '</option>' ;
}
echo '</select>';
$value = $_GET['footage'];
How can I get the value when I select some value in the dropdown box.
You need to add HTML selected attribute to <select>
$value = $_GET['footage']; /* typo*/
$items[] = $file ;
echo '<select name="value">';
foreach($items as $video){
$selected = ($value == $video) ? 'selected="selected"' : '';
echo '<option value="' . $video . '"' . $selected . '>' . $video . '</option>' ;
}
echo'</select>';

php dropdown menu with deafult selected value

I am working on a PHP project as follows
if( mysql_num_rows( $result ) > 0 ) {
echo '<select name="officerlist">';
while ( $rows = mysql_fetch_array ( $result ) ) {
echo '<option>' . $rows['officer'] . '</option>';
if ($types[ $maxindex ] == $rows['officer']){
echo ' selected';
}
}
echo '</select>';
}
I am able to populate the query result into the dropdown menu. However, I want to implement it to have a default selected value based on value from another array. (ie if $types[$maxindex] is found in $rows['officer'], it will auto select the value.).
Anyone able to advise?
Thank you and best regards!
try this
if( mysql_num_rows($result)>0 ) {
echo '<select name="officerlist">';
while ( $rows = mysql_fetch_array ($result) ) {
if ($types[$maxindex] == $rows['officer']){
echo '<option selected="selected">' . $rows['officer'] . '</option>';
}
else
{
echo '<option>' . $rows['officer'] . '</option>';
}
}
echo '</select>';
should be like this,
while ( $rows = mysql_fetch_array ($result) ) {
if ($types[$maxindex] == $rows['officer']){
echo '<option selected="selected" value="'.$rows['officer'].'">' . $rows['officer'] . '</option>';
} else {
echo '<option value="'.$rows['officer'].'">' . $rows['officer'] . '</option>';
}
}
You need add the selected attribute inside the <option> tag, like
<option selected='selected'>youroption</option>.
Try this,
if( mysql_num_rows($result)>0 ) {
echo '<select name="officerlist">';
while ( $rows = mysql_fetch_array ($result) ) {
$selected ="";
if ($types[$maxindex] == $rows['officer']){
$selected = ' selected="selected" ';
}
echo '<option '.$selected.'>' . $rows['officer'] . '</option>';
}
echo '</select>';
}
while ( $rows = mysql_fetch_array ($result) ) {
if ($types[$maxindex] == $rows['officer']){
echo '<option SELECTED>' . $rows['officer'] . '</option>';
} else {
echo '<option>' . $rows['officer'] . '</option>';
}
}

This does not make $sel = 1 yet it should, why doesn't it?

This does not make $sel = 1 yet it should, why doesn't it?
$countryVar = 'SouthAfrica';
$day = 'South Africa';
$cutCountry = str_replace(" ", "", $day);
if($cutCountry == $CountryVar) {
echo '<option value="' . $cutCountry . '" selected>' . $row["Country"] . '</option>';
$sel = 1;
} else {
echo '<option value="' . $cutCountry . '">' . $row["Country"] . '</option>';
}
if($sel == 0) {
echo '<option value="na" selected>- Please Select Country -</option>';
}
You mistyped the variable name on line 4:
$countryVar != $CountryVar
There is spelling mistake , please change like below.
Replace if($cutCountry == $CountryVar) {
To if($cutCountry == $countryVar) {
Spelling mistake in $countryVar.

output columns as an array for inarray checking

The following code works. However I was wondering if there was a way to clean up the top part where the columns are outputted as an array.
I didn't include the full script because it is too long, but basically I have three different queries and I want to match the values from the table (resultInput...textarea...images) with the editable values I defined.
In order for me to do this I need to have the columns outputted as an array.
I was wondering if there was a cleaner way of doing this? (top-part until //end output as array)?
$ArrayResultInput = array();
while ($row = mysql_fetch_array($resultInput)) {
$ArrayResultInput[] = $row['Field'];
}
$inArrayResultInput = $ArrayResultInput;
//
$ArrayResultTextarea = array();
while ($row = mysql_fetch_array($resultTextarea)) {
$ArrayResultTextarea[] = $row['Field'];
}
$inArrayResultTextarea = $ArrayResultTextarea;
//
$ArrayResultImages = array();
while ($row = mysql_fetch_array($resultImages)) {
$ArrayResultImages[] = $row['Field'];
}
$inArrayResultImages = $ArrayResultImages;
// end output as array
while ($row = mysql_fetch_assoc($resultUpdate)) {
foreach ($row as $fieldname => $value) {
if (in_array($fieldname, $inArrayResultInput)) {
echo '<div class="wrapper"><label>' . ucfirst(str_replace('_', ' ', $fieldname)) . '<br><input name="' . $fieldname . '" type="text" class="input" value="' . $value . '"><br></label></div>';
}
}
foreach ($row as $fieldname => $value) {
if (in_array($fieldname, $inArrayResultTextarea)) {
echo '<div class="wrapper"><label>' . ucfirst(str_replace('_', ' ', $fieldname)) . '<br><textarea name="' . $fieldname . '">' . $value . '</textarea><br></label></div><script type="text/javascript">CKEDITOR.replace(\'' . $fieldname . '\',{toolbar:\'Basic\'});</script>';
}
}
foreach ($row as $fieldname => $value) {
if (in_array($fieldname, $inArrayResultImages)) {
echo '<div class="wrapper"><span class="form-file">' . ucfirst(str_replace('_', ' ', $fieldname)) . '</span><input id="' . $fieldname . '" name="' . $fieldname . '" type="input" class="input" value="' . $value . '"><input type="button" value="Browse" onclick="CKFinder.popup(\'../\', null, null, SetFileField_' . $fieldname . ');" class="button browsebttn medium"><br></div><script type="text/javascript">function SetFileField_' . $fieldname . '(fileUrl){document.getElementById(\'' . $fieldname . '\').value=fileUrl;}</script>';
}
}
}

Categories