How to have the max value selected inside a select - php

I have the following:
PHP CODE:
<select name="valoracion<?php echo $i; ?>" id="valoracion<?php echo $i; ?>">
<?php
for($h=1; $h<6; $h++){
echo '<option value="'.$h.'">'.$h.'</option>';
}
?>
</select>
This prints:
<select name="valoracion0" id="valoracion0">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
</select>
When this <select> appears I want by default for the selected value to be the max number. How can I do this?

Like this..
<select name="valoracion<?php echo $i; ?>" id="valoracion<?php echo $i; ?>">
<?php
$cnt=6;$sel=""; // <--- Add a variable $sel with nothing assigned
for($h=1; $h<$cnt; $h++){
if($h==($cnt-1)) //<-- Here goes the check
{
$sel="Selected";
}
echo '<option '.$sel.' value="'.$h.'">'.$h.'</option>';
}
?>
</select>

You can try this,
<select name="valoracion<?php echo $i; ?>" id="valoracion<?php echo $i; ?>">
<?php
for($h=1; $h<6; $h++){
if($h == 5){
echo '<option value="'.$h.'" selected >'.$h.'</option>';
}else{
echo '<option value="'.$h.'">'.$h.'</option>';
}
}
?>
</select>

This should work:
<select name="valoracion<?php echo $i; ?>" id="valoracion<?php echo $i; ?>">
<?php
for($h=1; $h<6; $h++){
if($h == 5){ $selected = " selected=\"selected\""; }else{ $selected = NULL; }
echo '<option value="'.$h.'"'.$selected.'>'.$h.'</option>';
}
?>
</select>

Related

Eliminate the Selected option if already selected in database...?

I have a select box with options up to 20, for example, if option 5 or any other is selected and saved in a database in the specific column it won't show next time...
please guide me
<select class="form-control text-center" name="code" id="Code" >
<option value="">Select Code</option>
<?php for($i = 1; $i <= 20; $i++){ ?>
<option value="<?php echo $i; ?>"><?php echo $i; ?></option>
<?php } ?>
</select>
Controller:
function index(){
//below code should be in Model this is only for example
$this->db->select('code');
$query = $this->db->get_where('hr_levels', array('created_by' => $_SESSION['username']));
if ($query->num_rows() > 0) {
$code = $query->row('code');
} else {
$code = 0;
}
$option = array();
for($i=1;$i<=20;$i++){
if ($code != $i) {//no need to add if in view, just pass the data to view
$option[$i] = $i;
}
}
$data['options'] = $option;
$this->load->view('option_view',$data);
}
view:
<select class="form-control text-center" name="code" id="Code" >
<option value="">Select Code</option>
<?php foreach($options as $key => $value){
<option value="<?php echo $key; ?>"><?php echo $value; ?></option>
<?php } ?>
</select>
You can approach it like this
$availableOption = range(1,20);
$selectedOrInDb = [5,6,8,9]; // Selected OR from DB
$remaining = array_diff($availableOption, $selectedOrInDb);
Now you can use $remaining to loop through
<select class="form-control text-center" name="code" id="Code" >
<option value="">Select Code</option>
<?php foreach($remaining as $key => $value){ ?>
<option value="<?php echo $value; ?>"><?php echo $value; ?></option>
<?php } ?>
</select>
Controller
public function get_selected(){
$this->load->model('model_name');
$data['selected'] = $this->model_name->function_name($_SESSION['username']);
//Already saved values will be get on the basis of any condition
return $this->load->view('file.php');
}
Model
public function function_name($username){
return $this->db->get_where('table', ['created_by' => $username])->row()->value;
}
Now you get already selected value in $selected
<select class="form-control text-center" name="code" id="Code" >
<option value="">Select Code</option>
<?php for($i = 1; $i <= 20; $i++){
if($i != $selected)?>
<option value="<?php echo $i; ?>"><?php echo $i; ?></option>
<?php } } ?>
</select>

HTML select option selected does not work

I want to set the current date by default but it works only on year's select (the last one).
I wouldn't like to use javascript in this case. Just html and php.
I have already tried "selected='selected'" and "autocomplete=off" as shown in different other posts.
Here my code
<form id="form-orders-date" onsubmit="selectOrdersDate();">
<select id="orders-day">
<?php
for ($i=1; $i <= 31; $i++) {
if (sprintf('%02d', $i) == date("d")) {
echo "<option value=\"".sprintf('%02d', $i)."\" selected>".sprintf('%02d', $i)."</option>";
}
else{
echo "<option value=\"".sprintf('%02d', $i)."\">".sprintf('%02d', $i)."</option>" ;
}
}
?>
</select>
<select id="orders-month">
<?php
for ($i=1; $i <= 12; $i++) {
if (sprintf('%02d', $i) == date("m")) {
echo "<option value=\"".sprintf('%02d', $i)."\" selected>".sprintf('%02d', $i)."</option>";
}
else{
echo "<option value=\"".sprintf('%02d', $i)."\">".sprintf('%02d', $i)."</option>" ;
}
}
?>
</select>
<select id="orders-year">
<?php
for ($i=2012; $i <= 2020; $i++) {
if (sprintf('%02d', $i) == date("Y")) {
echo "<option value=\"".sprintf('%02d', $i)."\" selected>".sprintf('%02d', $i)."</option>";
}
else{
echo "<option value=\"".sprintf('%02d', $i)."\">".sprintf('%02d', $i)."</option>" ;
}
}
?>
</select>
<input type="submit" value="OK" />
</form>
here the plain HTML when the PHP has been run:
<form id="form-orders-date" onsubmit="selectOrdersDate();">
<select id="orders-day">
<option value="01">01</option>
<option value="02" selected>02</option>
<option value="03">03</option>
<option value="04">04</option>
<option value="05">05</option>
<option value="06">06</option>
<option value="07">07</option>
<option value="08">08</option>
<option value="09">09</option>
<option value="10">10</option>
<option value="11">11</option>
<option value="12">12</option>
<option value="13">13</option>
<option value="14">14</option>
<option value="15">15</option>
<option value="16">16</option>
<option value="17">17</option>
<option value="18">18</option>
<option value="19">19</option>
<option value="20">20</option>
<option value="21">21</option>
<option value="22">22</option>
<option value="23">23</option>
<option value="24">24</option>
<option value="25">25</option>
<option value="26">26</option>
<option value="27">27</option>
<option value="28">28</option>
<option value="29">29</option>
<option value="30">30</option>
<option value="31">31</option>
</select>
<select id="orders-month">
<option value="01">01</option>
<option value="02">02</option>
<option value="03" selected>03</option>
<option value="04">04</option>
<option value="05">05</option>
<option value="06">06</option>
<option value="07">07</option>
<option value="08">08</option>
<option value="09">09</option>
<option value="10">10</option>
<option value="11">11</option>
<option value="12">12</option>
</select>
<select id="orders-year">
<option value="2012">2012</option>
<option value="2013">2013</option>
<option value="2014">2014</option>
<option value="2015">2015</option>
<option value="2016">2016</option>
<option value="2017" selected>2017</option>
<option value="2018">2018</option>
<option value="2019">2019</option>
<option value="2020">2020</option>
</select>
<input type="submit" value="OK" />
</form>
This is the code you are looking for
Remember to use single quotes (') when including double quotes inside the PHP echo or else if using double quotes use " inside the outer double quotes.
<form id="form-orders-date" onsubmit="selectOrdersDate();">
<select id="orders-day">
<?php
for ($i=1; $i <= 31; $i++) {
if ($i == date("d")) {
echo '<option value="'.$i.'" selected>'.$i.'</option>';
}
else{
echo '<option value="'.$i.'">'.$i.'</option>';
}
}
?>
</select>
<select id="orders-month">
<?php
for ($i=1; $i <= 12; $i++) {
if ($i == date("m")) {
echo '<option value="'.$i.'" selected>'.$i.'</option>';
}
else{
echo '<option value="'.$i.'">'.$i.'</option>';
}
}
?>
</select>
<select id="orders-year">
<?php
for ($i=2012; $i <= 2020; $i++) {
if ($i == date("Y")) {
echo '<option value="'.$i.'" selected>'.$i.'</option>';
}
else{
echo '<option value="'.$i.'">'.$i.'</option>';
}
}
?>
</select>
Can you try like this.
<form id="form-orders-date" onsubmit="selectOrdersDate();">
<select id="orders-day">
<?php
for ($i=1; $i <= 31; $i++) {
if ($i == date("d")) {
echo "<option value='$i' selected='selected'>$i</option>";
} else {
echo "<option value='$i'>$i</option>";
}
}
?>
</select>
<select id="orders-month">
for ($i=1; $i <= 12; $i++) {
if ($i == date("m")) {
echo "<option value='$i' selected='selected'>$i</option>";
} else {
echo "<option value='$i'>$i</option>";
}
}
</select>
<select id="orders-year">
<?php
for ($i=2012; $i <= 2020; $i++) {
if ($i == date("Y")) {
echo "<option value='$i' selected='selected'>$i</option>";
} else {
echo "<option value='$i'>$i</option>";
}
}
?>
</select>
<input type="submit" value="OK" />
</form>
Here's a more compact way of achieving this:
<form id="form-orders-date" onsubmit="selectOrdersDate();">
<select id="orders-day">
<?php for ($d = 1; $d <= 31; $d++) { ?>
<option value="<?php echo $d; ?>" <?php if ($d == date('j')) { echo 'selected'; ?>><?php echo $d; ?></option>
<?php } ?>
</select>
<select id="orders-month">
<?php for ($m = 1; $m <= 12; $m++) { ?>
<option value="<?php echo $m; ?>" <?php if ($m == date('n')) { echo 'selected'; ?>><?php echo $m; ?></option>
<?php } ?>
</select>
<select id="orders-year">
<?php for ($y = 2012; $y <= 2020; $y++) { ?>
<option value="<?php echo $y; ?>" <?php if ($y == date('Y')) { echo 'selected'; ?>><?php echo $y; ?></option>
<?php } ?>
</select>
<input type="submit" value="OK" />
</form>
Have you tried autocomplete="off" for select? It works for me.
I know you said you tried this already, but your sample code doesn't show it. Here's how your tag should be formed:
<select>
<option value="01">01</option>
<option value="02" selected="selected">02</option>
<option value="03">03</option>
...
</select>
Your example above looks very different for the selected element. I'd suggest using whatever inspection tool is built into your web browser to see what exactly was rendered, and that will probably make it the solution obvious.
For reference, see:
Definition of select tag
Definition of option tag

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>

Edit selected value from database

This always selected the last record from my database.
This is my select tag:
<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($t_proyek_kd_proyek == $value->nm_proyek){
$selected = 'selected';
}
?>
<option value="<?php echo $value->kd_proyek; ?>" echo ' selected="selected"'; >
<?php echo $value->nm_proyek; ?>
</option>
<?php }?>
</select>
And this is my controller:
$row = $this->T_cost_model->get_by_id($id);
if ($row) {
$data = array(
'button' => 'Update',
'action' => site_url('konsultan/cost/update_action'),
'kd_cost' => set_value('kd_cost', $row->kd_cost),
'investasi_awal' =>rupiah(set_value ('investasi_awal', $row->investasi_awal)),
'b_operasional' => rupiah(set_value('b_operasional', $row->b_operasional)),
'g_cost' => rupiah(set_value('g_cost', $row->g_cost)),
'disbenfit' => rupiah(set_value('disbenfit', $row->disbenfit)),
'pwc' => set_value('pwc', $row->pwc),
't_proyek_kd_proyek' => set_value('t_proyek_kd_proyek', $row->t_proyek_kd_proyek),
);
$this->load->view('konsultan/t_cost_form', $data);
In your HTML, your last option tag has selected="selected" explicitly set:
option value="<?php echo $value->kd_proyek; ?>" <?php echo 'selected="selected"'; ?>
Wrap that in a condition and the selected option from your loop should work.
option value="<?php echo $value->kd_proyek; ?>" <?php if ($selected) echo 'selected="selected"'; ?>
change your <select> html code value like this
<select name="t_proyek_kd_proyek" id="t_proyek_kd_proyek" class="select-search" />
<option value=""></option>
foreach($t_cost->result() as $value){
$selected= '';
if($t_proyek_kd_proyek == $value->nm_proyek){
$selected = ' selected="selected" ';
}
?>
<option value="<?php echo $value->kd_proyek; ?>" <?= $selected?> >
<?php echo $value->nm_proyek; ?>
</option>
<?php }?>
</select>
hope it will help

Create a PHP Dropdown menu from a for loop?

I am trying to create a drop down menu with the options of 1,2,3 and 4.
The below code is what I am using just now and the dropdown is empty.
Any idea what I am doing wrong?
<select name="years">
<?php
for($i=1; $i<=4; $i++)
{
"<option value=".$i.">".$i."</option>";
}
?>
<option name="years"> </option>
</select>
<input type="submit" name="submitYears" value="Year" />
You are not outputting the option tags.
Try it like this:
<select name="years">
<?php
for($i=1; $i<=4; $i++)
{
echo "<option value=".$i.">".$i."</option>";
}
?>
<option name="years"> </option>
</select>
<input type="submit" name="submitYears" value="Year" />
You basically use html without closing the php syntax.Your code should look like this:
<select name="years">
<?php
for($i=1; $i<=4; $i++)
{
?>
<option value="<?php echo $i;?>"><?php echo $i;?></option>
<?php
}
?>
<option name="years"> </option>
</select>
<input type="submit" name="submitYears" value="Year" />
Or are you trying to echo the option? In that case you forgot the echo statement:
echo "<option value= ".$i.">".$i."</option>";
This worked for me. It populates years as integers from the current year down to 1901:
<select Name='ddlSelectYear'>
<option value="">--- Select ---</option>
<?php
for ($x=date("Y"); $x>1900; $x--)
{
echo'<option value="'.$x.'">'.$x.'</option>';
}
?>
</select>
You forgot something..
Add print / echo before "<option value=".$i.">".$i."</option>";
place an echo in your loop to output your options.
echo "<option value=".$i.">".$i."</option>";
Just echo the <option> tag
echo "<option value=".$i.">".$i."</option>";
<select name="year">
<?php for ($i = 0; $i <= 9; $i++) : ?>
<option value='<?= $i; ?>' <?= $fetchData->year == $i ? 'selected' : '' ?>><?= $i; ?></option>
<?php endfor; ?>
</select>

Categories