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>
Related
enter image description hereI'm creating a program, using the dropdown list.
so here is the form "Pilih Unit Mesin", where the machine unit that has the number "Torsi" in the database automatically will appear numbers in the form of "Torsi"
The problem is how if the machine unit has no numbers in database, i hope in the form "Torsi" can be input manually number
Please help, thank you
<label>Pilih Unit Mesin</label>
<select class="form-control" name="id_unit" id="id_unit" required="">
<option value=""><b>nama proyek - unit mesin</b></option>
<?php foreach ($mesin as $key => $value) :?>
<?php if ($value['status_unit'] === '0'): ?>
<option <?php echo $unit_selected == $value['id_unit'] ? 'selected="selected"' : '' ?>
value="<?php echo $value['id_unit'].'">'.' - '.$value['nama_project'].' - '.$value['unit_mesin'].'</option>'; ?>">
<?php else: ?>
<?php endif; ?>
<?php endforeach?>
</select>
</div>
<label>Torsi</label>
<select class="form-control" name="torsi" id="torsi" required="">
<?php foreach ($poross as $key => $val) :?>
<?php if ($val['torsi_poros'] != null): ?>
<option <?php echo $poros_selected == $val['id_unit'] ? 'selected="selected"' : '' ?>
class="<?php echo $val['id_unit'] ?>" value="<?php echo $val['torsi_poros'] ?>"><?php echo $val['torsi_poros'] ?></option>
<span class="input-group-addon">Kg.mm</span>
<?php elseif ($val['torsi_poros'] != ""): ?>
<option <?php echo $poros_selected == $val['id_unit'] ? 'selected="selected"' : '' ?>
class="form-group input-group">
<input class="form-control" name="torsi" type="text" required="" pattern="^(\+|-)?[0-9].*$">
<span class="input-group-addon">Kg.mm</span>
</div>
<?php endif; ?>
<?php endforeach?>
</select><br>
enter image description here
Screenshot 1:
Screenshot 2:
Screenshot 3:
enter image description here
You could combine multiple array functions to check whether specific key from multidimensional array is not empty :
<label>Torsi</label>
<?php
if (empty(array_filter(array_column($poross, 'torsi_poros')))) {
?>
<input type="number" name="torsi" id="torsi" class="form-control" >
<?php } else { ?>
<select class="form-control" name="torsi" id="torsi" required="">
<?php foreach ($poross as $key => $val) :?>
<?php if ($val['torsi_poros'] != null): ?>
<option <?php echo $poros_selected == $val['id_unit'] ? 'selected="selected"' : '' ?>
class="<?php echo $val['id_unit'] ?>" value="<?php echo $val['torsi_poros'] ?>"><?php echo $val['torsi_poros'] ?></option>
<span class="input-group-addon">Kg.mm</span>
<?php elseif ($val['torsi_poros'] != ""): ?>
<option <?php echo $poros_selected == $val['id_unit'] ? 'selected="selected"' : '' ?>
class="<?php echo $val['id_unit'] ?>" value="<?php echo $val['torsi_poros'] ?>"><?php echo $val['torsi_poros'] ?></option>
<span class="input-group-addon">Kg.mm</span>
</div>
<?php endif; ?>
<?php endforeach?>
</select>
<?php } ?>
<br>
Using array_column to return the values from a single column in the array, then array_filter to discard empty array.
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>
So, I have the following div and I want to show it just if the item type is equal to "Mobile Phone" or Tablet, selected on the same page with a combo box (also an input but instead of writing it's a combobox), but I just can't figure it out how to wrap this div inside an if condition based on what is selected on the combo box, can anyone help me?
Something like this:
if item = "MobilePhone" or item = "Tablet" --------> Show div
But I have Php code already inside the div.. that's why i just can't do it
<div class="control-group <?php echo !empty($imeiError)?'error':'';?>">
<label class="control-label"><strong>IMEI</strong></label>
<div class="controls">
<input name="imei" type="text" placeholder="IMEI" value="<?php echo !empty($imei)?$imei:'';?>">
<?php if (!empty($imeiError)): ?>
<span class="help-inline"><?php echo $imeiError;?></span>
<?php endif;?>
</div>
</div>
And that's how i select the input that I want
<div class="control-group <?php echo !empty($itemError)?'error':'';?>">
<label class="control-label"><strong>Item</strong></label>
<div class="controls">
<select name="item">
<option value="MobilePhone" <?php echo !empty($item) && $item == 'MobilePhone' ? 'selected' : ''; ?>>MobilePhone</option>
<option value="Tablet" <?php echo !empty($item) && $item == 'Tablet' ? 'selected' : ''; ?>>Tablet</option>
<option value="Computer" <?php echo !empty($item) && $item == 'Computer' ? 'selected' : ''; ?>>Computer</option>
<option value="HeadPhones" <?php echo !empty($item) && $item == 'HeadPhones' ? 'selected' : ''; ?>>HeadPhones</option>
</select>
</div>
You already seem to be checking for $item in your second code, so assuming that $item is set you can use this code to show the div only if "MobilePhone" or "Tablet" is selected:
<?php if ($item == "MobilePhone" || $item == "Tablet") : ?>
<div class="control-group <?php echo !empty($imeiError)?'error':'';?>">
<label class="control-label"><strong>IMEI</strong></label>
<div class="controls">
<input name="imei" type="text" placeholder="IMEI" value="<?php echo !empty($imei)?$imei:'';?>">
<?php if (!empty($imeiError)): ?>
<span class="help-inline"><?php echo $imeiError;?></span>
<?php endif;?>
</div>
</div>
<?php endif ?>
If your div has display:none in your css.
Use select's onchange="myFunc()"
And in your myFunc() look for selected value and do whatever you want.
document.getElementById('yourDivId').style='display:block' sets your div visibility.
<select onchange="myFunction()" id="select"><!--selections with different values --></select>
<script> function myFunction() {
var selected=document.getElementById("select").value;
if(selected=="Mobile")
document.getElementById("yourDivId").style="display:block"; }
</script>
you can use a variable $html to organize html in if condition like below code.
<?php
$item = "MobilePhone";
$html = "";
if ($item == "MobilePhone" || $item == "Tablet") {
$html .= '<div class="control-group';
echo !empty($imeiError)?'error':'';
$html .= '<label class="control-label"><strong>IMEI</strong></label><div class="controls">
<input name="imei" type="text" placeholder="IMEI" value="'.!empty($imei)?$imei:''; '">';
if (!empty($imeiError)):
$html .= '<span class="help-inline">';
echo $imeiError;
$html .= '</span>';
endif;
$html .='</div></div>';
}
?>
i have a registration form using multiselect which insert options in the value 1,2,3,4,5,6,7 based on selection
However, after inserting i want to provide an option for editing where users edit
and i want the already selected values from database to be preselected while looping other options as unselected
I am experiencing unidentified offset 3, unidentified offset 3........
<div class="form-group">
<label for="touch-spin-1">Course Days</label>
<select multiple="multiple" name="coursedays[]" style="width: 100%;" data-toggle="select2" data-placeholder="<?php //echo $course_days; ?>" data-allow-clear="true" >
<?php
$entities = "1,2,3,4,5,6,7"; $entity = explode(",",$entities); $servs = explode("," , $course_days); $arr_length = count($entity); for($i=0;$i<=$arr_length;$i++){
?>
<option <?php if (in_array($servs, $entity)) { echo "selected";} ?> value="<?php echo $servs[$i]; ?>"> <?php echo $servs[$i]; ?></option>
<?php } ?></select></div>
try in this way
<?php
$course_days = "2,3,4";
$entities = "1,2,3,4,5,6,7";
$entity = explode(",", $entities);
$servs = explode(",", $course_days);
foreach($entity as $en) {
?>
<option value="<?php echo $en; ?>" <?php echo (in_array($en, $servs)) ? 'selected' : ''; ?>>
<?php echo $en; ?>
</option>
<?php } ?>
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;