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>';
}
}
Related
I am listing multiple categories.
<?php
function listCategory($id = 0, $string = 0, $catID = 0){
global $sib;
$query = $sib -> prepare( "select * from categories where subCatID=? and isActive=? order by toSort asc" );
$query -> execute( array($id, 1) );
$view = $query -> fetchAll( PDO::FETCH_ASSOC );
$xe = $querye -> rowCount();
if ( $xe ) {
foreach ($view as $row) {
if ( $row["subCatID"] == 0 ) {
echo '<optgroup label="' . $row["title"] . '">';
} else {
echo '<option value="' . $row["categoryID"] . '" >';
}
echo str_repeat( " - ", $string ) . $row["title"];
listCategory( $row["categoryID"], $string + 1, $catID );
if ( $row["subCatID"] == 0 ) {
echo '</optgroup>';
} else {
echo '</option>';
}
}
} else { return false; }
}
listCategory( 0, 0, 0 );
?>
There is no problem with listing. Array contains category ID numbers.
$catIDs = array(4,7,18);
The categories whose id numbers are listed must be selected that are equal to their id number.
I've made a few attempts at this. I could not be successful.
I've made a few attempts at this. I could not be successful.
I solved the problem.
echo '<option value="' . $row["categoryID"] . '" >';
I edited this field
$catListA = array(4,7,18);
$selected = in_array($row["categoryID"],$catListA ) ? "selected" : "";
echo '<option value="' . $row["categoryID"] . '" '. $selected .'>';
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>';
}
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>';
I have these arrays:
$payments = array(1=>'Cash',2=>Cheque,3=>credit,4=>other);
$selected = array(2,1);
foreach($payments as $key=>$value) {
foreach($selected as $id) {
if ($key == $id) {
echo $id . "is selected" . '<br>';
}
else{
echo $id . " is not selected" . '<br>';
}
}
}
what expected:
1 is selected
2 is not selected
3 is not selected
4 is selected
but i got:
1 is not selected
1 is selected
2 is selected
2 is not selected
3 is not selected
3 is not selected
4 is not selected
4 is not selected
what's the wrong in my loops?
You don't need inner loop:
$payments = array(1=>'Cash',2=>Cheque,3=>credit,4=>other);
$selected = array(2,1);
foreach($payments as $key=>$value) {
if (in_array($key, $selected)( {
echo $key . "is selected" . '<br>';
} else {
echo $key . " is not selected" . '<br>';
}
}
You could simply use in_array() to check if an element is selected:
$payments = array(1=>'Cash',2=>'Cheque',3=>'credit',4=>'other');
$selected = array(2,1);
foreach($payments as $key=>$value) {
if (in_array($key, $selected)) {
echo $value . "is selected" . '<br>';
} else {
echo $value . " is not selected" . '<br>';
}
}
By the way, you need quotes around the payment method names.
You can use in_array() instead, so you can omit your inner loop:
foreach($payments as $key=>$value) {
if (in_array($key, $selected)) {
echo $id . "is selected" . '<br>';
}else{
echo $id . " is not selected" . '<br>';
}
}
Example
UPDATE: an alternative solution. Not better (maybe), but different :)
$payments = array(1=>'Cash', 2=>'Cheque', 3=>'Credit', 4=>'Other');
$selected = array('Cash','Cheque');
foreach($selected as $chosen){
$selected_values = array_search($chosen, $payments);
echo "Number ". $selected_values." (".$chosen.") is selected.<br>";
}
You can see the output here
Old solution (as everyone else...)
$payments = array(1=>'Cash', 2=>'Cheque',3=>'Credit',4=>'Other');
$selected = array(2,1);
foreach($payments as $key=>$value) {
if (in_array($key, $selected)) {
echo $key. " is selected" . '<br>';
}else{
echo $key. " is not selected" . '<br>';
}
}
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.