Select option value using php - php

I need select option like below format using php
<select>
<option value="2011-2012">2011-2012</option>
<option value="2012-2013">2011-2012</option>
<option value="2013-2014">2011-2012</option>
<option value="2014-2015">2011-2012</option>
<option value="2015-2016">2011-2012</option>
</select>

You can easily use foreach to achieve this:
<select>
<?php $years = array('2011-2012', '2012-2013', '2013-2014', '2014-2015');
foreach ($years as $year) { ?>
<option value="<?php echo $year; ?>"><?php echo $year; ?></option>
<?php } ?>
</select>

$years = range(date("Y"), 2006);
$yearsplus = range(date("Y", strtotime('+1 years')), 2006);
$yearrange = new MultipleIterator();
$yearrange->attachIterator(new ArrayIterator($years));
$yearrange->attachIterator(new ArrayIterator($yearsplus));
foreach($yearrange as $values) {
$result[] = $values;
}
echo "<select>";
foreach ($result as $res) {
echo '<option value="'.$res[0].'-'.$res[1].'">'.$res[0].'-'.$res[1].'</option>';
}
echo "</select>";

Related

How do i selected the value from drop down if value match with the function?

How do I select the value from the drop-down if the value matches the function generated values? Unable to select the value for example if $match value is 08:00 condition should select the value from the drop-down list?
PHP Function
function create_time_range($start, $end, $interval = '15 mins', $format = '12') {
$startTime = strtotime($start);
$endTime = strtotime($end);
$returnTimeFormat = ($format == '12')?'H:i':'H:i';
$current = time();
$addTime = strtotime('+'.$interval, $current);
$diff = $addTime - $current;
$times = array();
while ($startTime < $endTime) {
$times[] = date($returnTimeFormat, $startTime);
$startTime += $diff;
}
$times[] = date($returnTimeFormat, $startTime);
return $times;
}
$times= create_time_range('00:00', '23:45', '15 mins');
$match = '08:00';
Html
<select name="time_picker">
<option value="">Select Time</option>
<?php foreach($times as $key=>$val){ ?>
<?php
if (in_array($match, $times))
{
$selected = 'selected';
}
?>
<option <?php echo $selected ?> value="<?php echo $val; ?>"><?php echo $val; ?></option>
<?php } ?>
</select>
Update your html part to following
<select name="time_picker">
<option value="">Select Time</option>
<?php foreach($times as $key=>$val){
$selected = $match===$val ? 'selected' : '';
?>
<option <?php echo $selected ?> value="<?php echo $val; ?>"><?php echo $val; ?></option>
<?php } ?>
</select>

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>

Option Menu with Date Shows as 1969-12-31

I have an option menu that works properly when there are records to display. I sometimes have a new item that still does not have records and I would like the option menu not to display any dates. Instead it displays 1969-12-31.
<select name="selDate" id="selDate" onchange="formDate.submit()">
<option value="%">all dates</option>
<?php
do {
?>
<option value="<?php echo $row_RecordsetDate['date']?>"<?php if
($varDate_Recordset1 == $row_RecordsetDate['date']) {echo 'selected';} ?>><?
php $dates=date('l, F d, Y',strtotime($row_RecordsetDate['date']));
?></option>
<?php
} while ($row_RecordsetDate = mysql_fetch_assoc($RecordsetDate));
$rows = mysql_num_rows($RecordsetDate);
if($rows > 0) {
mysql_data_seek($RecordsetDate, 0);
$row_RecordsetDate = mysql_fetch_assoc($RecordsetDate);
}
?>
</select>
How can I have the option menu display blank if no records or just state no date available?
<select name="selDate" id="selDate" onchange="formDate.submit()">
<option value="%">all dates</option>
<?php
while($row = mysql_fetch_assoc($RecordsetDate)){
if(isset($row['date'])){
if(strlen($row['date'])){
echo '<option value="'.$row['date'].'"';
if($varDate_Recordset1 == $row['date']){
echo 'selected';
}
$date = date('l, F d, Y',strtotime($row['date']));
echo '>'.$date.'</option>';
}
}
}
?>
</select>
Through trial and error, I came up with the following solution. I placed an if else statement in the option value.
<option value="<?php echo $row_RecordsetDate['date']?>"<?php if ($varDate_Recordset1 == $row_RecordsetDate['date']) {echo 'selected';} ?>><?php $dates=date('l, F d, Y',strtotime($row_RecordsetDate['date'])); $rows = mysql_num_rows($RecordsetDate); if(!empty($rows)){
echo $dates;
}else{
echo ""; }?></option>
Below is the complete solution.
<select name="selDate" id="selDate" onchange="formDate.submit()">
<option value="%">all dates</option>
<?php
do {
?>
<option value="<?php echo $row_RecordsetDate['date']?>"<?php if ($varDate_Recordset1 == $row_RecordsetDate['date']) {echo 'selected';} ?>><?php $dates=date('l, F d, Y',strtotime($row_RecordsetDate['date'])); $rows = mysql_num_rows($RecordsetDate); if(!empty($rows)){
echo $dates;
}else{
echo ""; }?></option>
<?php
} while ($row_RecordsetDate = mysql_fetch_assoc($RecordsetDate));
$rows = mysql_num_rows($RecordsetDate);
if($rows > 0) {
mysql_data_seek($RecordsetDate, 0);
$row_RecordsetDate = mysql_fetch_assoc($RecordsetDate);
}
?>
</select>

current years in dropdown select box

I have this function for print current year in select box but option value not equal with option text. i.e :current years in selected value is 2013 but html text output is 2012. how to fix this?
PHP:
<select name="year">
<?php
for($i=date("Y")-5;$i<=date("Y");$i++) {
$sel = ($i == date('Y')) ? 'selected' : '';
echo "<option value=".$i." ".$sel.">".date("Y", mktime(0,0,0,0,1,$i))."</option>";
}
?>
</select>
Output:
<select name="year">
<option value=2008 >2007</option>
<option value=2009 >2008</option>
<option value=2010 >2009</option>
<option value=2011 >2010</option>
<option value=2012 >2011</option>
<option value=2013 selected>2012</option>
</select>
Maybe not a good wayŁˆ But just Add +1 for $i:
<select name="year">
<?php
for($i=date("Y")-5;$i<=date("Y");$i++) {
$sel = ($i == date('Y')) ? 'selected' : '';
echo "<option value=".$i." ".$sel.">".date("Y", mktime(0,0,0,0,1,$i+1))."</option>"; // change This Line
}
?>
</select>
Online Demo Here
Problem with your value printing
<select name="year">
<?php
for($i=date("Y")-5;$i<=date("Y");$i++) {
$sel = ($i == date('Y')) ? 'selected' : '';
echo "<option value=".$i." ".$sel.">".$i."</option>"; // here I have changed
}
?>
</select>
<?php
$yearArray = range(2015, 2050);
?>
Year
<select name="year" id="year">
<option value="">Select Year</option>
<?php
foreach ($yearArray as $year) {
$selected = ($year == 2017) ? 'selected' : '';
echo '<option '.$selected.' value="'.$year.'">'.$year.'</option>';
}
?>
</select>

convert the values from the json to drop down

i would like to extract all the values that correspond to the organisations to come under the drop down menu as options for organisation,but now only the last value of the oraganisation is showing up in the drop down as an option
here is my code
<?php
$items = json_decode('[{"location":[{"building": ["Building1"],"name":"Location1"}],"name":"Organization1"},{"location":[{"building":["Building2"],"name":"location2"}],"name":"Organisation2"},{"location":[{"building":["Building3"],"name":"Location3"}],"name":"Organization3"}]');
foreach( $items as $each ){
echo $each->location[0]->building[0];
echo $each->location[0]->name;
echo $each->name;
}
$org=$each->name;
$arr=array($org);
reset($arr);
//print_r($org);
//$result = count($org);
//echo $result;
while(list(,$value)=each($arr)){
//echo "value:$value<br/>\n";
//$_SESSION['organisation']=$value;
//echo $_SESSION['organisation'];
}?>
<select name="category_id">
<option value=""></option>
<?php
$keys = array_keys($arr);
$count1=count($keys);
echo $count1;
for($i=0; $i<count($arr); $i++)
{?>
<option value="<?php echo $keys[$i]; ?>"><?php echo $arr[$i]; ?></option>
<?php
}
?>
</select>
Make use of that first foreach rather than starting an entire new loop. Tested and this works:
<?php
$items = json_decode('[{"location":[{"building": ["Building1"],"name":"Location1"}],"name":"Organization1"},{"location":[{"building":["Building2"],"name":"location2"}],"name":"Organisation2"},{"location":[{"building":["Building3"],"name":"Location3"}],"name":"Organization3"}]');
echo '<select name="category_id"><option value=""></option>';
$stepper = 0;
foreach($items as $each) {
$building = $each->location[0]->building[0];
$name = $each->location[0]->name;
$final_name = $each->name;
echo '<option value="'.$stepper.'">'.$final_name.'</option>';
$stepper++;
}
echo '</select>';
?>
Use this code
<?php
$org = array();
$items = json_decode('[{"location":[{"building": ["Building1"],"name":"Location1"}],"name":"Organization1"},{"location":[{"building":["Building2"],"name":"location2"}],"name":"Organisation2"},{"location":[{"building":["Building3"],"name":"Location3"}],"name":"Organization3"}]');
foreach( $items as $each ){
$org[]=$each->name;
}
?>
<select name="category_id">
<option value=""></option>
<?php
foreach($org as $key=>$val)
{?>
<option value="<?php echo $key; ?>"><?php echo $val; ?></option>
<?php
}
?>
</select>

Categories