How to selected multiple select with explode - php

So, I have $dp->isi = "1,2,3,4"
<select name="isi[]" class="form-control select2" multiple="multiple" data-placeholder="Choose"
style="width: 100%;">
<?php foreach ($data->result() as $db) : ?>
<option value="<?php echo $db->id;?>"
<?php if ($db->id == explode(',',$dp->isi)) { ?>
selected="selected"<?php } ?>>
<?php echo $db->name; ?>
</option>
<?php endforeach; ?>
</select>
Using codeIgniter, I want $dp->isi value to be selected on multipleselect.

Related

Set_value() Array fields select option Codeigniter not working

I trying to create dynamic server side select input, after i submit, the set_value('nilai[]') not showing any value.
Here's my Controller below:
$this->load->library('form_validation');
$this->form_validation->set_rules('nilai[]', 'Nilai Pantuhir', 'required');
if ($this->form_validation->run() == FALSE)
{
$this->load->view('pantuhir/pantuhir_form');
} else {
$list_pantuhir = $this->input->post('nilai');
foreach ($list_pantuhir as $key => $value) {
echo $value."<br />";
}
}
Here's my view below :
<div class="form-group <?php if(form_error('nilai[]')){echo 'has-error';} ?>">
<select class="form-control" name="nilai[]">
<option value="">- Choose-</option>
<option value="<?php echo $rowPerson['intUserId'].'-'.'A';?>" <?php if(set_value('nilai[]') == $rowPerson['intUserId'].'-'.'A') { echo 'selected'; } ?>>A</option>
<option value="<?php echo $rowPerson['intUserId'].'-'.'B';?>" <?php if(set_value('nilai[]') == $rowPerson['intUserId'].'-'.'B') { echo 'selected'; } ?>>B</option>
</select>
<?php echo form_error('nilai[]'); ?>
</div>
I want to show set_value and get selected in option field if validation not correct.
Hope this will help you :
Use set_select instead of set_value. If you use a menu, this function permits you to display the menu item that was selected, after the form validation throws any error
It should be like this :
<div class="form-group <?php if(form_error('nilai[]')){echo 'has-error';} ?>">
<select name="nilai[]" >
<option value="" >---Choose----</option>
<option
value="<?=$rowPerson['intUserId'].'-A';?>"
<?=set_select('nilai[]', $rowPerson['intUserId'].'-A');?>
>A</option>
<option
value="<?php echo $rowPerson['intUserId'].'-B';?>"
<?=set_select('nilai[]', $rowPerson['intUserId'].'-B');?>
>B</option>
</select>
<?php echo form_error('nilai[]'); ?>
</div>
For more : https://www.codeigniter.com/user_guide/helpers/form_helper.html#set_select
Use this code in the view
<div class="form-group <?php if(form_error('nilai[]')){echo 'has-error';} ?>">
<select class="form-control" name="nilai[]">
<option value="">- Choose-</option>
<option value="<?php echo $rowPerson['intUserId'].'-'.'A';?>" <?php if(set_value('nilai[]',rowPerson['intUserId'].'-'.'A') == $rowPerson['intUserId'].'-'.'A') { echo 'selected'; } ?>>A</option>
<option value="<?php echo $rowPerson['intUserId'].'-'.'B';?>" <?php if(set_value('nilai[]',$rowPerson['intUserId'].'-'.'B') == $rowPerson['intUserId'].'-'.'B') { echo 'selected'; } ?>>B</option>
</select>
<?php echo form_error('nilai[]'); ?>
</div>

echo selected in chosen select

I am using chosen select drop down to show auto complete drop down. I want to set selected value for edit. I tried following code which works for normal select option but not working for chosen select
<select class="chosen-select" >
<option value=""></option>
<?php if(!empty($list))
{
foreach($list as $d)
{
?>
<option value="<?php echo $d->id; ?><?php if($d->id == 2) { echo "selected"; } ?>"><?php echo $d->name; ?></option>
<?php } } ?>
</select>
You are putting your selected inside your value attribute, you need to write it after :
<select class="chosen-select" >
<option value=""></option>
<?php if(!empty($list)) {
foreach($list as $d) {
?>
<option value="<?php echo $d->id; ?>"<?php if($d->id == 2) { echo " selected"; } ?>><?php echo $d->name; ?></option>
<?php } } ?>
</select>
Building on #roberto06's answer, the following should be a bit cleaner to look at.
BTW, you really should consider using a template engine.
<select class="chosen-select">
<option value=""></option>
<?php if (!empty($list)): ?>
<?php foreach ($list as $d): ?>
<option value="<?php echo $d->id; ?>" <?php echo ($d->id == 2) ? "selected" : "">
<?php echo $d->name; ?>
</option>
<?php endforeach; ?>
<?php endif; ?>
</select>

some one help me, i cant get value from database when edit filed to select

I have create a select option form but when edit i can get current value from database.
<select name="t_proyek_kd_proyek" id="t_proyek_kd_proyek" class="select-search" />
<option value=""></option>
<?php
$t_cost=$this->db->query("select * from t_proyek ");
foreach($t_cost->result() as $value){
$selected= '';
if($nm_proyek == $value->nm_proyek){
$selected = 'selected';
}
?>
<option value="<?php echo $value->kd_proyek; ?>" <?php echo $selected;?>>
<?php echo $value->nm_proyek; ?>
</option>
<?php }?>
</select>
You should not use value for select tag. Check the condition in inside the option tag and make it is selected like following
<select name="t_proyek_kd_proyek" id="t_proyek_kd_proyek" class="select-search" placeholder="Pilih..." />
<option value=""></option>
<?php
$t_cost=$this->db->query("select * from t_proyek ");
foreach($t_cost->result() as $value){?>
<option value="<?php echo $value->kd_proyek; ?>" <?php if($nm_proyek==$value->kd_proyek) { ?> selected="selected" <?php }?> ><?php echo $kd_proyek=$value->nm_proyek; ?></option>
<?php }?>
</select>
<?php
$conn = new mysqli('SERVER NAME', 'DB USER NAEM', 'DB USER PASSWORD', 'DB NAME');
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
//set the value of which option you want to selected
$nm_proyek = '';
?>
<!-- for combobox you should use 'multiple' -->
<select name="t_proyek_kd_proyek" id="t_proyek_kd_proyek" class="select-search" placeholder="Pilih..." multiple />
<option value=""></option>
<?php
$query = $conn->query("SELECT * FROM `t_proyek`");
while($value = $query->fetch_object()) {?>
<option value="<?php echo $value->kd_proyek; ?>" <?php $nm_proyek == $value->kd_proyek ? "selected='selected'" : "" ?> ><?php echo $kd_proyek=$value->nm_proyek; ?></option>
<?php }?>
</select>

set_select for select tag in codeigniter

How to set value for select tag ,as when i tried for text field
<input type="input" name="name" value="<?php echo set_value('name') ?>"/>
its working but when i tried for select tag
<SELECT class="form-control" name="user" style="width:200px;">
<option value="">--Select--</option>
<?php
foreach ($result as $row):
echo "<option value='" . $row['id'] . "' >" . $row['employee_name'];
?>
<?php endforeach ?>
</SELECT>
its not working ,when validation run falls its again showing --Select-- option
try this--> use set_select()
<select id="user" name="user" style="width: 230px; height: 40px;" >
<option value="" selected>--Select--</option>
<?php foreach ($result as $row) { ?>
<option value="<?php echo $row['id'] ; ?>" <?php echo set_select('user', $row['id'], False); ?> ><?php echo $row['employee_name'] ; ?> </option>
<?php } ?>
</select>
For Codeigniter Dropdown list you should use set_select() instead of set_value() function. You can use like
<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>
best way is to use form_dropdown method to use these as you are already looping and creating options create option array in controller and pass it to view like
$options = array('--Select--');
foreach ($result as $row){
$options[$row['id']] = $row['employee_name'];
}
echo form_dropdown('user',$options,set_select('user','default_value'));
if not any select value from dropdown then default value will be used
this might help you.
<SELECT class="form-control" name="user" style="width:200px;">
<option value="">--Select--</option>
<?php
foreach ($result as $row):
echo "<option value='".$row['id']."' set_select('user', $row['id']);.">". $row['employee_name'];
?>
<?php endforeach ?>
</SELECT>

How do I Populate Multi Select drop dwon using php

Here I am listing all cars.customers want to compare car so they will select from this drop down. A person can select multiple cars. At the first time he is selecting 'Audi' and Saab' I will store it into data base next if he came I need to populate Saab and audi as select how I can do this using php
<select name="cars" multiple>
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="opel">Opel</option>
<option value="audi">Audi</option>
</select>
Here is my code
<select id="cars" class="multiselect" multiple="multiple" name="cars[]">
<?PHP
if($carslist->num_rows() >0)
{
foreach($carslist->result_array() as $entry):
?> <option value="<?php echo($entry['ID']); ?>" ><?php echo($entry['car_name']); ?></option>
<?php
endforeach;
}
?>
</select>
Following code I tried $resources contain select cars
<select id="cars" class="multiselect" multiple="multiple" name="cars[]">
<?PHP
if($carslist->num_rows() >0)
{
foreach($carslist->result_array() as $entry):
if($resources->num_rows() >0)
{
foreach($resources->result_array() as $car):
if($entry['ID'] == $employee['car_id'])
{
$select = 'selected="selected"';
}
else
{
$select = '';
}
endforeach;
}
?> <option value="<?php echo($entry['ID']); ?>" <?php echo $select;?> ><?php echo($entry['car_name']); ?></option>
<?php
endforeach;
}
?>
</select>
but it showing error
Here, try something like this, and see if it works:
Here is the controller:
<?php
function something(){
$data = array();
$data['cars'] = $this->some_model->some_function_to_return_cars_array();
$data['selected'] = $some_array_of_selected_cars();
$this->load->view('some_view', $data);
}
?>
And this is the view:
<select id="cars" class="multiselect" multiple="multiple" name="cars[]">
<option value="">Select:</option>
<?php
foreach( $cars as $key => $val ){
?>
<option value="<?php echo $val['some_id'] ?>"
<?php
if( in_array( $val['some_id'], $selected ) ) echo ' selected';
?>
><?php echo $val['some_name'] ?></option>
<?php
}
?>
</select>

Categories