I have a problem, where I want to get a value from the database, and if the value matches the one in my option, the option should be selected.
This is my form:
<!-- Select Basic -->
<div class="form-group">
<label class="col-md-4 control-label" for="tidspunkt_varighed">Vælg Antal Timer</label>
<div class="col-md-4">
<select id="tidspunkt_varighed" name="tidspunkt_varighed" class="form-control">
<option value="1">1 Time</option>
<option value="2">2 Timer</option>
<option value="3">3 Timer</option>
<option value="4">4 Timer</option>
</select>
</div>
</div>
I don't want to make an if clause every line.
Thanks in advance.
Kristian
You should use a loop as your values are incremental when check with one if() inside the loop for the checked value.
<?php
$options = '';
$valueFromDb = 1;
for($i = 1;$i<=4 ; $i++)
{
if( $i == $valueFromDb) {
$options .= '<option value="'.$i.'" selected="selected">'.$i.'Time</option>';
} else {
$options .= '<option value="'.$i.'" >'.$i.'Time</option>';
}
}
?>
<div class="form-group">
<label class="col-md-4 control-label" for="tidspunkt_varighed">V�lg Antal Timer</label>
<div class="col-md-4">
<select id="tidspunkt_varighed" name="tidspunkt_varighed" class="form-control">
<?php echo $options;?>
</select>
</div>
</div>
I think, you can use an array somehow like that:
<?
$value=YOUR_VALUE_FROM_DB
$selected[$value]="selected";
?>
<option value="1" <?=$selected[1]?>>1 Time</option>
<option value="2" <?=$selected[2]?>>2 Timer</option>
etc.
if you are not displaying these options using a loop then you have to put if with every option like
<option value="1" <?php if($dbvalue == 1){echo 'selected="selected"';}>1 Time</option>
and in case you are showing options using a loop you can put a condition in the loop and make a string like
$selected = 'selected="selected"';
based on the condition, and echo $selected with every option.
$val_from_db = 3;
$output = '<!-- Select Basic -->
<div class="form-group">
<label class="col-md-4 control-label" for="tidspunkt_varighed">Vælg Antal Timer</label>
<div class="col-md-4">
<select id="tidspunkt_varighed" name="tidspunkt_varighed" class="form-control">';
foreach( $values as $key=>$value ) {
if ( $key==$val_from_db )
$selected = 'selected="selected" ';
else
$selected = '';
$output .= '<option '.$selected.'value="'.$key.'">'.$value.'</option>';
}
$output .= '</select>
</div>
</div>';
echo $output;
Related
Hi what i would like to do in my dropdown is to put a condition if($formdata[0]['Phase']==1){ to show only the option values from $ahk 1x in dropdown but if($formdata[0]['Phase']==3){ to show only the option values from $ahk 3x in dropdown here is my code
<div class="form-group col-sm-6 col-xs-12">
<label for="d13"><?php echo $t[$tmpvalues[0]['Lang']]['d13'];?></label>
<select class="form-control el-text-box" name="d13" onchange="this.form.submit()">
<?php for ($j=0; $j<sizeof($ahk); $j++){
$selected="";if($ahk[$j]==$formdata[0]['d13']){$selected="selected";}
?>
<option value="<?php echo $ahk[$j];?>" <?php echo $selected; ?>><?php echo $ahk[$j];?></option>
<?php
}
?>
</select>
</div>
values of $ahk are
$ahk[0]="1X10A";
$ahk[1]="1X20A";
$ahk[2]="1X30A";
$ahk[3]="1X40A";
$ahk[4]="1X50A";
$ahk[5]="1X60A";
$ahk[6]="1X70A";
$ahk[7]="1X80A";
$ahk[8]="3X10A";
$ahk[9]="3X20A";
$ahk[10]="3X30A";
$ahk[11]="3X40A";
$ahk[12]="3X50A";
$ahk[13]="3X60A";
$ahk[14]="3X80A";
$ahk[15]="3X100A";
$ahk[16]="3X160A";
$ahk[17]="3X200A";
$ahk[18]="3X300A";
$ahk[19]="3X400A";
$ahk[20]="3X500A";
$ahk[21]="3X1000A";
$ahk[22]="3X1600A";
$ahk[23]="3X2000A";
$ahk[24]="3X2600A";
$ahk[25]="3X3200A";
Use an if statement to check if the array value matches the phase.
<div class="form-group col-sm-6 col-xs-12">
<label for="d13"><?php echo $t[$tmpvalues[0]['Lang']]['d13'];?></label>
<select class="form-control el-text-box" name="d13" onchange="this.form.submit()">
<?php
$prefix = $formdata[0]['Phase'] . "X";
foreach ($ahk as $option) {
if (substr($option, 0, 2) == $prefix) {
$selected = $option == $formdata[0]['d13'] ? "selected" : "";
?>
<option value="<?php echo $option;?>" <?php echo $selected; ?>><?php echo $option;?></option>
<?php
}
}
?>
</select>
</div>
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>
I can't show the data that I want to edit from my table. The combobox and the table don't show data when I edit the data categories, sub-categories, and manufacturers. I want to be able to see the data from my table after editing.
Controller form
public function data_items_edit($id)
{
$this->load->model('additem_m');
$model3 = $this->additem_m;
$data['table'] = $model3->get_where2($id)[0];
//$data['table'] = $model3->get();
$this->load->model('category_m');
$model = $this->category_m;
$data['category'] = $model->get();
$this->load->model('subcategory_m');
$model1 = $this->subcategory_m;
$data['sub'] = $model1->get();
$this->load->model('manufactures_m');
$model2 = $this->manufactures_m;
$data['manu'] = $model2->get();
//print_r($data['table']);
$this->load->view('items/edit_items_v', $data);
}
Data table controller
public function index()
{
if($this->session->has_userdata('isLogin')){
$this->load->model('additem_m');
$model = $this->additem_m;
$data['table'] = $model->get('items.*, item_categories.name as ic,
item_categories_sub.name as ics, item_manufactures.name as im',
[
['table'=>'item_categories','condition'=>'item_categories.id =
items.item_category_id'],
['table'=>'item_categories_sub','condition'=>'item_categories_sub.id
= items.item_category_sub_id'],
['table'=>'item_manufactures','condition'=>'item_manufactures.id =
items.item_manufacturer_id'],
]);
//print_r($data['table']);
$this->load->view('items/items_v', $data);
}else{
redirect('login');
}
}
View Form
<div class="form-group">
<label class="col-sm-4 control-label form-label">Category :</label>
<div class="col-sm-8">
<select name="item_category_id" class="form-control" id="category">
<option value='' <?php if($category == '0'){ echo 'selected';} ?>>--Select--</option>
<?php foreach($category as $category){
echo '<option value="'.$category->id.'">'.$category->name.'</option>';
} ?>
</select>
</div>
</div>
<div class="form-group">
<label class="col-sm-4 control-label form-label">Sub Category</label>
<div class="col-sm-8">
<select name="item_category_sub_id" class="form-control" id="category_sub">
<option value=''>- Select Sub Category -</option>
</select>
</div>
</div>
<div class="rightcontact">
<div class="form-group">
<label class="col-sm-4 control-label form-label">Manufacturer</label>
<div class="col-sm-8">
<select name="item_manufacturer_id" class="form-control" id="item_manufacturer_id">
<option>- Select Manufacturer -</option>
<?php foreach($manu as $manu){
echo '<option value="'.$manu->id.'">'.$manu->name."</option>";
} ?>
</select>
</div>
</div>
</div>
My view
You have to add selected attribute to the options.
$category == $category->id ? "selected" : ""
Something like this
<?php
foreach($category as $category){
echo '<option value="'.$category->id.'" '. $category == $category->id ? "selected" : "" .'>'.$category->name.'</option>';
^ ^
}
?>
I have a select box connect to database for countries, it works but trying to keep the selected data in the form clears itself out. I have tried adding a selected but gives error
<div class="form-group">
<label>Country</label>
<?php
$sql = "SELECT * FROM countries ";
$result = query($sql);
?>
<select class="form-control input-lg box" id="country" name="country">
<option value="">Select a country</option>
<?php
$i = 0;
while (($row = mysqli_fetch_assoc($result)) != false) {
?>
<option value="<?=$row["country_id"];?>"><?=$row["country_name"];?></option>
<?php
$i ++;
}
?>
</select>
</div>
Assign the return value of a ternary operator to the variable $selected and add it to the option tag
<?php
$selected = ($_POST['country'] == $row["country_id"])?"selected":"";
?>
<option value="<?=$row["country_id"];?>" <?=$selected ?>>
<?=$row["country_name"];?>
</option>
I have a modal form with two select boxes with the same current_models class.
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label class="control-label">Model</label>
<select class='form-control' id='left_model_id' name='left_model_id'>
<div id="left_model" class="current_models"></div>
</select>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label class="control-label">Model</label>
<select class='form-control' id='right_model_id' name='right_model_id'>
<div id="right_model" class="current_models"></div>
</select>
</div>
</div>
</div>
Each select needs to have it's own id, but they are loading the same select options into groups like this:
$('.current_models').load('ajax_get_models_man_group.php');
Here's my ajax_get_models_man_group.php page:
<?php
require_once 'core/init.php';
$user = new User();
if(!$user->isLoggedIn()) {
Redirect::to('login.php');
}
$models = DB::getInstance()->query("SELECT man.id AS man_id, man.man AS man, models.name AS model, models.id AS model_id FROM hearing_aid_models models LEFT JOIN hearing_aid_man man ON man.id = models.man_id WHERE models.currently_fitting = 'Y' AND company_id = '" . $user->data()->company_id . "' ORDER BY man ASC");
if ($models->error()) {
echo 'Error occurred.';
} else {
$currentGroup = null;
foreach( $models->results() as $result ) {
// start a new optgroup
if( $currentGroup == null || $result->man_id != $currentGroup ) {
// end the previous group
if( $currentGroup != null ) {
echo "</optgroup\n>";
}
// start a new group
echo "<optgroup data-id='{$result->man_id}' label='{$result->man}'>\n";
$currentGroup = $result->man_id;
}
echo "<option value='{$result->model_id}'>{$result->model}</option>\n";
}
// end the last opt group
if( $currentGroup != null ) echo "</optgroup>\n";
}
?>
The problem is that the values do not load into the select divs!
Here's my ajax source code results:
<optgroup data-id='1' label='Audibel'>
<option value='95'>Start 7 Wireless</option>
<option value='96'>Start 7</option>
<option value='99'>A3i Platinum</option>
</optgroup>
<optgroup data-id='16' label='Phonak'>
<option value='98'>Naida Q70</option>
</optgroup>
<optgroup data-id='2' label='Starkey'>
<option value='100'>S Series i30</option>
<option value='81'>3 Series I 70</option>
<option value='82'>3 Series I 90</option>
</optgroup>
1) Get rid of divs inside select tags
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label class="control-label">Model</label>
<select class='form-control current_models' id='left_model_id' name='left_model_id'>
</select>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label class="control-label">Model</label>
<select class='form-control current_models' id='right_model_id' name='right_model_id'>
</select>
</div>
</div>
</div>
2) load ajax inside the right div
$('.current_models').load('ajax_get_models_man_group.php');