How to pick a selected option from database - php

I have a select box like this:
<select name="id_category" id="">
<?php foreach($categorys as $category): ?>
<option value="<?= $category['id']?>"><?= $category['name']?></option>
<?php endforeach?>
</select>
There are three categorys: Residential, Comercial and Institutional.
How can I put it in a way that it already appears in the current selected category?

Having the selected category:
<option <? echo $selected == $category['id'] ? "selected " : "" ?>....

Related

PHP: How to display select options if no value from database

I am trying to display my dropdown with a value from the database, but if the value is null I want it to show my options.
Currently it keeps showing me the blank select option.
<select class="form-control col-sm-5" id="freqlevels" name="freqlevels" value="<?php if ($customerinfo['freqlevel']) { echo h($customerinfo['freqlevel']);} else { echo "" ; } ?>"">
<option value=""></option>
<option value="Twice Weekly">Twice Weekly</option>
<option value="Weekly">Weekly</option>
<option value="Fortnightly">Fortnightly</option>
<option value="Monthly">Monthly</option>
</select>
Please can you suggest what I should do?
put your condition outside the value
<?php if ($customerinfo['freqlevel']) { echo value="$customerinfo['freqlevel']";}
hope this will resolve your problem
You need to make use of conditional statements.
<select name="something" id="my-select">
<option value="0">Everyone can see me</option>
<?php if (empty($array['some_key'])) : ?>
<option value="1">I'm only if some_key is empty</option>
..etc..
<?php endif; ?>
</select>
Then you can check values against the option value:
<option value="<?php echo $key; ?>"
<?php echo ($key === $_POST['some_key'] ? 'selected' : ''); ?>>
Hello, world
</option>

How to set the value for dropdown box?

i want to display the value for all the textbox with the help of this code
value="<?php `if(!empty($result) && !empty($result['part'])){ echo $result['part']; } ?>">
Where part is the name of the database field...same thing i have to display for dropdown box how can i do this..In dropdown box the options are displayed with the help of another table
<select class="bootstrap-select show-tick" data-size="5" data-live-search="true" data-width="100%" name="TName1" required>
<option value="" disabled selected>Select Sub Account Name</option>
<?php foreach ($accName as $row ): ?>
<option value="<?=$row['id']?>" <?php echo set_select('accName', $row['id']); ?>>
<?=$row['accName']?>
</option>
<?php endforeach ?>
</select>
where accName is the name stored in another table..i want to display the name here..but it had to be stored as code(predefined integer value) in my database field...How can i did this..??
Value for drop down is set different way than that of text box or text area.
Drop down has options and one of its options (multiple in case it is a multiple dropdown) is set to be selected one.
So, <option> syntax:
<option value="value">Text</option>
If we want to make an option selected, use selected' attribute of`
<option value="value" selected="selected">Text</option>
So, in your case, solution would be:
1) Take value from database.
2) In the options loop, compare database value with current iteration <option> value.
3) If any of options' values are matching, use selected attribute of <option>
<select class="bootstrap-select show-tick" data-size="5" data-live-search="true" data-width="100%" name="TName1" required>
<option value="" disabled selected>Select Sub Account Name</option>
<?php foreach ($accName as $row ):
$selected = ($dbValueForDropDown == $row['id']) ? 'selected="selected"' : '';
?>
<option value="<?=$row['id']?>" <?php echo set_select('accName', $row['id']); ?> <?php echo $selected;?>><?=$row['accName']?></option>
<?php endforeach ?>
</select>
you should bring id and name of the accounts in the same results set by join in the query you send to the database and then run on the results and put the id in the value and the name in the display
<select class="bootstrap-select show-tick" data-size="5" data-live-search="true" data-width="100%" name="TName" required placeholder="select account name">
<option value="" disabled selected>Select Account Name</option>
<?php foreach ($accName as $row ):
$selected = '';
if($row['id'] == $result['code']){
$selected = 'selected = selected';
}
?>
<option <?php echo $selected; ?> value="<?=$row['id']?>" <?php echo set_select('accName', $row['id']); ?>>
<?=$row['accName']?></option>
<?php endforeach ?>
</select>

Retrieve stored drop-down value from database

I have a drop-down in my manageDevices View.
<select name="TrackerType" id="dropdown" style="width:68% !important;">
<option value="Mobile">Mobile</option>
<option value="Device">Device</option>
<option value="Other">Other</option>
</select>
I want to save the value of the drop-down in the database in my addDevice view and load the editDevice view with the values retrieved from database.
I was able to save the value of the drop-down to the database.
Now I want to fetch the stored value to the editDevice page and show the selected value in the drop-down. I can get the name of the selected value using,
<?php echo $devicearray['type'] ?>
I want to show this value as the "selected value" in the drop-down.
I am using codeigniter framework to develop this. Any hint will be highly appreciated
At your view file for each option:
<option value="Device"<?php echo ($devicearray['type']=='Device'?'selected="selected"':''); ?>>Device</option>
It would be easier to store type as an integer, but for 3 values not big difference
you can try this
<select name="TrackerType" id="dropdown" style="width:68% !important;">
<option value="Mobile" <?php echo ($devicearray['type']=='Mobile') ? "selected" : ""; ?>>Mobile</option>
<option value="Device" <?php echo ($devicearray['type']=='Device') ? "selected" : ""; ?>>Device</option>
<option value="Other" <?php echo ($devicearray['type']=='Other') ? "selected" : ""; ?>>Other</option>
</select>
Method:2
$$devicearray['type'] = "selected";
?>
<select name="TrackerType" id="dropdown" style="width:68% !important;">
<option value="Mobile" <?php echo #$Mobile; ?>>Mobile</option>
<option value="Device" <?php echo #$Device; ?>>Device</option>
<option value="Other" <?php echo #$Other; ?>>Other</option>
</select>

in an update, how to select an option from a select box?

Good afternoon,
Using cakephp, how can I select a value in a select box in an update operation?
in the view I get the variables like this for example:
<?php $category = $itemEditar['Anuncio']['category']; ?>
and I need to select an option from a select box:
<select id="select_category" name="enquiry">
<option value="" >selecione</option>
<option value="Category1">Categoria1</option>
<option value="Category2">Categoria2</option>
<option value="Category3">Categoria2</option>
</select>
to be an update operation, I need to mark the category that was saved in the database, and am not getting how to do this.
You want to check the $category against each or the options and if they match set the selected attribute .
<select id="select_category" name="enquiry">
<option value="" >selecione</option>
<option<?= $category == "Category1"?" selected = 'selected'":"" ?> value="Category1">Categoria1</option>
<option<?= $category == "Category2"?" selected = 'selected'":"" ?> value="Category2">Categoria2</option>
<option<?= $category == "Category3"?" selected = 'selected'":"" ?> value="Category3">Categoria2</option>
</select>
the correct answer is:
<select id="select_category" name="enquiry" >
<option value="Category1" <?php echo $category == "Category1"?" selected = 'selected'":"" ?> >Categoria1</option>
<option value="Category2" <?php echo $category == "Category2"?" selected = 'selected'":"" ?> >Categoria2</option>
<option value="Category3" <?php echo $category == "Category3"?" selected = 'selected'":"" ?> >Categoria2</option>
</select>

Auto select value of dropdown

How can I auto select a field in dropdown.
Say if someone goes to www.xyx/form/?abc
Some value gets selected in the dropdown,
Or if someone goes to www.xyx/form/?def
Some other value gets selected in the dropdown.
I am comfortable with JS and php.
assuming example.com/?sel=xxx
<?php
$sel = $_GET['sel'];
?>
<select ...>
<option val="xxx" <?php if($sel==='xxx') echo 'selected="selected"';?>>Option XXX</option>
<option val="yyy" <?php if($sel==='yyy') echo 'selected="selected"';?>>Option YYY</option>
</select>
No Javascript needed.
PHP
<select name="select">
<option value="abc"<?php ($_GET['select'] == 'abc'? echo 'selected="selected"' : ''); ?>>ABC</option>
<option value="def"<?php ($_GET['select'] == 'def'? echo 'selected="selected"' : ''); ?>>DEF</option>
</select>
<option value="abc" <?php echo isset($_GET['abc']) ? 'selected="selected"' : ''; ?>>abc</option>
hmmm, so what will you do when you have 100s of items in the Options list? The other ideas wont look so great then.
Then you will need to just simply write 1 line of code at the end of select tag:
<?php if(isset($_POST['env_foil_color'])) echo "<script>document.getElementById('env_foil_color').value='{$_POST['env_foil_color']}';</script>"; ?>
where, 'env_foil_color' is the select tag's ID and Name both

Categories