how to pre-select option from dropdown on the base of data coming from database or model to view.
I also want to save this data if change in dropdown option.
I try this code
<select class="form-control appointment_status" >
<option <?php if ($row['status'] == 0) { ?> selected <?php } ?> value="0">In process</option>
<option <?php if ($row['status'] == 1) { ?> selected <?php } ?> value="1">Completed</option>
</select>
This is normally what I like to do(this goes intto your controller):
// Select Categories
$categories_options = array();
$categories_options[0] = 'Select Categories';
$categories_list = $this->Terms_model->get_list();
foreach($categories_list as $cat){
$categories_options[$cat->term_id] = $cat->title;
}
$data['categories_options'] = $categories_options;
Assuming that your passing a $data variable to your view, you will be able to display it like this:
<!-- Post Categories -->
<?php
$data = array(
'class' => 'form-control js-example-basic-multiple',
'value' => set_value('categories'),
'multiple' => 'multiple',
);
?>
<div class="form-group">
<?= form_label('Categories','categories'); ?>
<?= form_dropdown('categories[]', $categories_options, 0, $data); ?>
</div>
and then to edit it(a different view):
<?php
$options = array(
'' => 'Please select an option',
'0' => 'In process',
'1' => 'Completed',
);
$data = array('class' => 'form-control');
?>
<div class="form-group">
<?= form_label('Status', 'status'); ?>
<?= form_dropdown('status', $options, $item->status, $data); ?>
</div>
Obviously you will need to change more stuff but I'm sure it will give you a better idea and approach.
Try something like this:
$status = ($row['status']) ? 'selected' : '';
echo <<<END
<option value="0" $status>In process</option>
<option value="1" $status>Completed</option>
END;
Using class... most of dropdown components uses active class to definy the first element on dropdown, so just put it on which element you want and will do the trick
Related
Here i need to add default selection in radio button(checked).
<?php $form = ActiveForm::begin(['id' => 'login-form']); ?>
<?= $form->field($model, 'email') ?>
<?php if(!empty($usernames)){
echo '<label>Select Any Username To Which You Want To Login</label><div class="all_username">';
foreach ($usernames as $key => $value) { ?>
<?= $form->field($model, 'username[]')->radio(['value' => $value['username'], 'id' => ''])->label($value['username']) ?>
<?php } ?>
</div>
<?= $form->field($model, 'password')->passwordInput() ?>
<?php } ?>
You need to use following method where 1 is your selected ID.
$form->radioButtonList($model,'1', $usernames, array('separator'=>"" ));
You can probably use something like this:
foreach ($usernames as $key => $value) {
if(/*somecondition*/){
$model->username = $value['username'];
}?>
<?= $form->field($model, 'username[]')->radio(['value' => $value['username'], 'id' => ''])->label($value['username']) ?>
<?php } ?>
Only by assigning the $model->username a default value you can choose which radio will be selected.
I am populating data from database to radio button and want to check the first value as default but I have no idea on how to do it. Here is my coding that I've done so far.
View
<?php if($designThemes != null){; ?>
<?php foreach ($designThemes as $designTheme) { ?>
<?php
$designThemesValue = (set_value('theme_name') ? set_value('theme_name') : $designTheme['slug_url']);
$designThemesAttr = array(
'name' => 'designThemes',
'value' => $designThemesValue,
);
?>
<div class="col-md-4 col-sm-4">
<div class="radio custom-radio">
<label>
<?php echo form_radio($designThemesAttr); ?>
<span class="fa fa-circle"></span> <?php echo $designTheme['theme_name'] ?>
</label>
</div>
</div>
<?php }}; ?>
If I add 'checked' => TRUE in $designThemesAttr = array() the system will check the last value of radio button as the default, but I wanted to check the first value.
solved your issue you can set it by conditional statement as replace your code by this
<?php if($designThemes != null){; ?>
<?php $i=1;foreach ($designThemes as $designTheme) { ?>
<?php
$designThemesValue = (set_value('theme_name') ? set_value('theme_name') : $designTheme['slug_url']);
$designThemesAttr = array(
'name' => 'designThemes',
'value' => $designThemesValue,
);
if($i==1){
$designThemesAttr['checked'] = TRUE;
}
?>
<div class="col-md-4 col-sm-4">
<div class="radio custom-radio">
<label>
<?php echo form_radio($designThemesAttr); ?>
<span class="fa fa-circle"></span> <?php echo $designTheme['theme_name'] ?>
</label>
</div>
</div>
<?php $i++; }}; ?>
'checked' => TRUE in $designThemesAttr = array() the system will check the last value of radio button as the default. This is right, because you doing it inside a for loop, and only one radio remains selected inside a group. So the last one is showing checked.
So maintain a variable to count the iteration like:
$count = 1;
if( $count = 1; )
{
// use 'checked' => TRUE here
$count++; // The value is incremented so that the if condition can't run again. This will add 'checked' => TRUE to first radio only.
}
I have a custom taxonomy and I've added a category dropdown to the archive page to allow users to quickly switch between categories. I have the following code, which works in redirecting users to the new category that they choose. However, the dropdown always defaults to the first option in the <select>. How can I pass the selected <option> through and set it as the default value when a user switches categories?
<?php
$args = array(
'taxonomy' => 'custom_taxonomy',
'orderby' => 'name',
'show_count' => 1,
'hierarchical' => 1,
'title_li' => 'title'
);
<form id="categoriesform" action="<?php bloginfo('url'); ?>" method="post">
<div>
<?php $cats = get_categories($args); ?>
<select id="categories" name="custom_taxonomy">
<option value="allCategories">All Categories</option>
<?php foreach ($cats as $cat) : ?>
<option value="<?php echo get_term_link($cat, $cat->taxonomy) ?>">
<?php echo $cat->name ?></option>
<?php endforeach; ?>
</select>
</div>
</form>
<script>
jQuery(document).ready(function() {
jQuery('#categories').change(function() {
if (jQuery(this).val() == 'allCategories') {
window.location = 'http://example.com/all-categories';
} else {
window.location = jQuery(this).val();
}
});
});
</script>
First you need to get current category id via get_queried_object()->term_id; then you check and add selected in option.
<?php
$current=get_queried_object()->term_id;
$args = array(
'taxonomy' => 'custom_taxonomy',
'orderby' => 'name',
'show_count' => 1,
'hierarchical' => 1,
'title_li' => 'title'
);
<form id="categoriesform" action="<?php bloginfo('url'); ?>" method="post">
<div>
<?php $cats = get_categories($args); ?>
<select id="categories" name="custom_taxonomy">
<option value="allCategories">All Categories</option>
<?php foreach ($cats as $cat) : ?>
<option <?php echo ($current == $cat->term_id ? 'selected' : ''); ?> value="<?php echo get_term_link($cat, $cat->taxonomy) ?>">
<?php echo $cat->name ?></option>
<?php endforeach; ?>
</select>
</div>
</form>
<script>
jQuery(document).ready(function() {
jQuery('#categories').change(function() {
if (jQuery(this).val() == 'allCategories') {
window.location = 'http://example.com/all-categories';
} else {
window.location = jQuery(this).val();
}
});
});
</script>
I want to echo database value for the drop down in edit mode. but in edit page it doesn't show the previously selected drop down value. now i want to echo database values to the drop down. how can i do it. My view controller and model are below. Please any one can help me.
View
<!--//Address-->
<tr><td><?php echo '<label for="heading" class="col-sm-0 control-label">Address </label>';?></td>
<td class="col-lg-10">
<?php
$data = array(
'name' => 'address',
'value' => $post->address,
'class' => 'form-control',
'style' => 'width:140%',
'placeholder' => 'Address of Your Site'
);
echo form_input($data);
echo '<br>';
?>
</td>
</tr>
<!--//Address-->
<?php foreach ($single as $post):?>
$attributes = 'class = "form-control" id = "client" style="width:140%; height:35px;"';
$buildings = array('-SELECT-'=>'-SELECT-', 'House'=>'House', 'Building Complex'=>'Building Complex', 'Commercial Building'=>'Commercial Building', 'Hotel'=>'Hotel','Hospital'=>'Hospital' );
echo form_dropdown('building', $buildings, $post->building,set_value('building'), $attributes);
?>
Controller
function edit_single(){
$id = $this->uri->segment(3);
//load project details according to relevent id
$data['single'] = $this->project_registration_details->show_pro_id($id);
$this->load->view('client/project_registration_edit_view',$data);
}
Model
function show_pro_id($id){
$this->db->select('*');
$this->db->from('project_registration');
$this->db->join('client', 'client.client_id = project_registration.reg_client_id');
$this->db->where('project_registration.id',$id);
$query = $this->db->get();
$result = $query->result();
return $result;
}
Use
set_value('building',$post->building)
in your selected part of form_dropdown function.
Try..
echo form_dropdown('building', $buildings,set_value('building',$post->building), $attributes);
Updated Answer
<?php foreach($single as $post) { ?>
<select name="building" class = "form-control" id = "client" style="width:140%; height:35px;">
<?php
$buildings = array('-SELECT-'=>'-SELECT-', 'House'=>'House', 'Building Complex'=>'Building Complex', 'Commercial Building'=>'Commercial Building', 'Hotel'=>'Hotel','Hospital'=>'Hospital' );
foreach($buildings as $key => $building) { ?>
<option value="<?php echo $key; ?>" <?php echo set_select('building', $post->building); ?>><?php echo $building; ?></option>
<?php } ?>
</select>
<?php } ?>
I am trying to populate my dynamic dropdown menu using CI:
My issue is that when I set my dropdown value and text ($formImageCaptionDropDown = array($get_images['id'] => $get_images['description']);) it does not display - just displays a blank dropdown box.
I have done print_r on my $get_images so I know that this is working correct.
I also know that I am to use $var['value'] because I have copied the format of another page that uses the images model and is working fine.
I just cannot seem to populate the dropdown menu
I have the following code:
<?php
//Setting form attributes
$get_images = $this->image_model->getImages();
$formImageCaption = array('id' => 'imageCaption', 'name' => 'imageCaption');
$formImageCaptionDropDown = array($get_images['id'] => $get_images['description']);
$formImageCaptionSelection = array('id' => 'captionSelection', 'name' => 'captionSelection');
$formSubmit = array('id' => 'submit','name' => 'submit','value' => 'Edit Caption');
?>
<div id ="formLayout">
<?php echo form_open('admin/imagecaption',$formImageCaption);
echo form_fieldset();
?>
<p>Please Select A Caption Below To Edit: </p>
<?php echo form_label ('Caption:', 'caption');?>
<?php echo form_dropdown('caption', $formImageCaptionDropDown); ?>
<div id="errorCaption"><?php echo form_error('caption');?></div>
<span class="small">Required Field</span>
<?php echo form_fieldset_close();
echo form_close(); ?>
</div>
I have resolved my issue by doing the following:
I moved the line $get_images = $this->image_model->getImages(); into my controller as $data['$get_images'] = $this->image_model->getImages();
In my view I did the following:
<?php foreach ($get_images as $image){
echo '<option value="'.$image['id'].'">'.$image['description'].'</option>';
};
?>`