set_select() is marking every option as selected - codeigniter

The set_select function of CodeIgniter 3 is always selecting the last option of any select input when I click to edit some existing item.
I will just show the first two selects, which is below:
<div class="form-group col-md-6">
<label for="id_marca">Marca do Carro</label>
<select id="id_marca" name="id_marca" class="form-control" required>
<option value="" selected disabled>Selecione</option>
<?php foreach ($marcas as $m) { ?>
<option value="<?php echo $m['id']; ?>" <?php echo set_select('id_marca', $m['id'], true); ?>>
<?php echo $m['nome_marca']; ?>
</option>
<?php } ?>
</select>
</div>
<div class="form-group col-md-6">
<label for="id_modelo">Modelo do Carro</label>
<select id="id_modelo" name="id_modelo" class="form-control" required>
<option value="" selected disabled>Selecione</option>
<?php foreach ($modelos as $m) { ?>
<option value="<?php echo $m['id']; ?>" <?php echo set_select('id_modelo', $m['id'], TRUE); ?>>
<?php echo $m['nome_modelo']; ?>
</option>
<?php } ?>
</select>
</div>
Below is a screenshot of the problem. As it can be seen in the Inspector, every option is selected.
I made it work few days ago, and now magically does not work anymore.

The problem with your code that you put true as third parameter in all of your option
set_select('first parameter will be name', 'second parameter will be the value', 'third parameter will be the "true/false" which make this value selected or not')
i think you need to look at this
UPDATE
$options = array(
'small' => 'Small Shirt',
'med' => 'Medium Shirt',
'large' => 'Large Shirt',
'xlarge' => 'Extra Large Shirt',
);
$shirts_on_sale = array('small', 'large');
echo form_dropdown('shirts', $options, 'large');
// Would produce:
<select name="shirts">
<option value="small">Small Shirt</option>
<option value="med">Medium Shirt</option>
<option value="large" selected="selected">Large Shirt</option>
<option value="xlarge">Extra Large Shirt</option>
</select>
you could see this
Sorry about that
UPDATE 2
<select name="myselect">
<option value="one" <?php echo set_select('myselect', 'one', TRUE); ?> >One</option>
<option value="two" <?php echo set_select('myselect', 'two'); ?> >Two</option>
<option value="three" <?php echo set_select('myselect', 'three'); ?> >Three</option>
</select>

Related

get value of multiselect in edit form

I want to edit a form so when i am in edit form previous edited values should shown in that field but i don't get the selected value from mysql
my edit form
<select data-placeholder="Select Rank..." class="chosen-select custom_select " multiple="" style="width: '-webkit-fill-available';" id="rank_restriction" name="rank_restriction[]">
<?php
foreach ($poll['rankname'] as $rank) { ?>
<option value="<?= $rank ?>"><?= $rank ?></option>
<?php } ?>
</select>
<?php
if (isset($edit['rank_restriction']) && !empty($edit['rank_restriction'])) {
$rank_restriction = $edit['rank_restriction']; // array format
} else {
$rank_restriction = set_value('rank_restriction'); // again in array format
}
?>
<select data-placeholder="Select Rank..." class="chosen-select custom_select "
multiple="" style="width: '-webkit-fill-available';" id="rank_restriction"
name="rank_restriction[]">
<?php
foreach ($poll['rankname'] as $rank) {
$selected = (!empty($rank_restriction) && in_array($rank, $rank_restriction)) ? 'selected' : '';
?>
<option value="<?= $rank ?>" <?php echo $selected ?>><?= $rank ?>
</option>
<?php } ?>
</select>

How to set data-list option value in second text box using html & JS (onChange() )

I have 1 datalist containing food items Name & 2nd textbox to display price of selected food item . I'm not understanding how to make a function on datalist using js to onChange display price in textbox txtprice .
#I'm using codeigniter
<input list="browsers" id="browser" name="browser">
<datalist id="browsers" onchange="getComboA(this)">
<?php foreach($h as $row){ ?>
<option value="<?php echo $row->price;?>" >
<?php echo $row->menu_name;?>
</option>
<?php } ?>
</datalist>
<div class="col-12 col-md-2"><input type="text" placeholder="Price" class="form-control" id="txtPrice" name="txtPrice" value="<?php echo $row->price;?>" readonly >
</div>
eg : Item Name - Panner Tikka | Price - 150
It's quite easy actually, you could set it up like this :
<input list="browsers" id="browser" name="browser" onchange="setTxtPrice(this)">
<datalist id="browsers">
<?php foreach($h as $row){ ?>
<option value="<?php echo $row->price;?>" >
<?php echo $row->menu_name;?>
</option>
<?php } ?>
</datalist>
<div class="col-12 col-md-2">
<input type="text" placeholder="Price" class="form-control" id="txtPrice" name="txtPrice" value="<?php echo $row->price;?>" readonly >
</div>
<script type="text/javascript">
function setTxtPrice(input1) {
var input2 = document.getElementById('txtPrice');
input2.value = input1.value;
}
</script>
It is very easy to use onchange() in JS
Use this.value to get the value of datalist when option will change
<datalist id="browsers" onchange="PutValue(this.value)">
<?php foreach($h as $row){ ?>
<option value="<?php echo $row->price;?>" >
<?php echo $row->menu_name;?>
</option>
<?php } ?>
JS
function PutValue(Value){
$('#input_field_id').val(Value);
}

codeigniter dropdown select data from database

<select class="form-control">
<?php
$flag=0;
foreach ($result as $row) { ?>
if(flag==0)
{
<option value="<?php echo $row->tbl_group_id; selected="selected"?>" ><?php echo $row->tbl_group_rolename; ?></option>
flag=1;
}
else
{
<option value="<?php echo $row->tbl_group_id;?>" ><?php echo $row->tbl_group_rolename; ?></option>
}
} ?>
<option value="name">One</option>
<option value="email">Two</option>
<option value="three">Three</option>
<option value="four">Four</option>
<option value="five">Five</option>
</select>
I have one dropdownbox(on login page) in which I have to get all the data from my database and it must be shown dynamically.
My form is created in bootstrap way.
I want all the data to come from table, dynamically in dropdown and when I select it; I can register as that person for example admin/user.
I have tried nothing till now; nothing comes in my mind.
First of all, you have to fix your PHP start and end tags. This is necessary because otherwise the PHP interpreter does not know what to evaluate.
The rest is pretty straightforward. I changed your $flag to a boolean in order to check whether the current entry is the first entry and therefore should be selected. Also I removed the duplicated code and introduced an if that outputs selected="selected" for the first option.
<select class="form-control">
<?php
$first=true; // Used to select the first element
foreach ($result as $row): /* everything between here and 'endforeach' is part of the loop */ ?>
<option value="<?php echo $row->tbl_group_id ?>" <?php if ($first) echo 'selected="selected"' ?>>
<?php echo $row->tbl_group_rolename ?>
</option>
<?php if ($first) $first = false /* this makes sure that only one option element is selected */ ?>
<?php endforeach ?>
<option value="name">One</option>
<option value="email">Two</option>
<option value="three">Three</option>
<option value="four">Four</option>
<option value="five">Five</option>
</select>
The loop iterates over the rows. Note that the colon after the foreach statement uses an alternative syntax that makes it easier to read if mixed with HTML:
<?php foreach ($result as $row): ?>
<!-- HTML code here that gets printed once for each $row -->
<?php endforeach ?>
You then put some PHP code inside the HTML to make it dynamic.
<?php if ($first) echo 'selected="selected"' ?>
The code above prints selected="selected" if $first is true. After the first iteration $first becomes false:
<?php if ($first) $first = false ?>
<select class="form-control">
<?php
foreach ($result as $row) {
$sel = ($row->tbl_group_id == $someId)?'selected="selected"':'';
?>
<option value="<?php echo $row->tbl_group_id;?>" <?php echo $sel; ?> ><?php echo $row->tbl_group_rolename; ?></option>
<?php } ?>
<option value="name">One</option>
<option value="email">Two</option>
<option value="three">Three</option>
<option value="four">Four</option>
<option value="five">Five</option>
</select>

How to add currency symbols into the currency list?

How can I add currency symbols into the currency list?
This is the code,
<?php if($this->getCurrencyCount() > 1): ?>
<div class="form-micro">
<select onchange="window.location.href=this.value" name="custom-currency-selector" id="custom-currency-selector">
<?php foreach ($this->getCurrencies() as $_code => $_name): ?>
<option value="<?php echo $this->getSwitchCurrencyUrl($_code)?>"
<?php if($_code == $this->getCurrentCurrencyCode()): ?>
selected="SELECTED"
<?php endif; ?>>
<?php echo $_name ?>
</option>
<?php endforeach; ?>
</select>
</div>
<?php endif; ?>
will output,
<option ...>Euro</option>
<option ...>British Pound</option>
<option ...>Danish Krone</option>
<option ...>Swedish Krona</option>
<option ...>Swiss Franc</option>
<option ...>United States Dollars</option>
But I need to add their symbols after the text,
<option ...>Euro €</option>
<option ...>British Pound £</option>
<option ...>Danish Krone DKK</option>
<option ...>Swedish Krona SEK</option>
<option ...>Swiss Franc CHF</option>
<option ...>United States Dollars $</option>
Is it possible?
Yes,Tealou ,it is possible ,you can get this using below code
Mage::app()->getLocale()->currency($cuurencycode)->getSymbol();
Your modified code is
<?php if($this->getCurrencyCount() > 1): ?>
<div class="form-micro">
<select onchange="window.location.href=this.value" name="custom-currency-selector" id="custom-currency-selector">
<?php foreach ($this->getCurrencies() as $_code => $_name): ?>
<option value="<?php echo $this->getSwitchCurrencyUrl($_code)?>"
<?php if($_code == $this->getCurrentCurrencyCode()): ?>
selected="SELECTED"
<?php endif; ?>>
<?php echo $_name ?><?php Mage::app()->getLocale()->currency($_code)->getSymbol(); ?>
</option>
<?php endforeach; ?>
</select>
</div>
<?php endif; ?>

Unable to set_select for multiple select form element

<select multiple name="skills[]">
<option value=".NET">.Net<?php echo set_select('skills[]', '.NET'); ?></option>
<option value="JAVA">Java<?php echo set_select('skills[]', 'JAVA'); ?></option>
<option value="php">PHP<?php echo set_select('skills[]', 'php'); ?></option>
<option value="cloud">Cloud<?php echo set_select('skills[]', 'cloud'); ?></option>
</select></td>
Hello here I am trying to set_select() for select multiple but not able to do. Need help, how Can I do that.. Thanks.
Sorry but tried following code it is working, :)
<select multiple name="skills[]">
<option value=".NET"<?php echo set_select('skills', '.NET'); ?>>.Net</option>
<option value="JAVA"<?php echo set_select('skills', 'JAVA'); ?>>Java</option>
<option value="php"<?php echo set_select('skills', 'php'); ?>>PHP</option>
<option value="cloud"<?php echo set_select('skills', 'cloud'); ?>>Cloud</option>
</select>
You can do that by specifying the third parameter of set_select, if set to TRUE it sets an item as default. For selecting all the options in your example just try the following code:
<select name="myselect[]" multiple>
<option value="one" <?php echo set_select('myselect', 'one', TRUE); ?> >One</option>
<option value="two" <?php echo set_select('myselect', 'two',TRUE); ?> >Two</option>
<option value="three" <?php echo set_select('myselect', 'three',TRUE); ?> >Three</option>
</select>
See info here

Resources