PHP Displaying current month in option select - php

How to make the current month selected by default in option select using php
Here is what i have tried so far.
$curmonth = date("F");
And to display the entire month
<select>
<?php
for($i = 1 ; $i <= 12; $i++)
{
$allmonth = date("F",mktime(0,0,0,$i,1,date("Y")))
?>
<option value="<?php
echo $i;
if($curmonth==$allmonth)
{
echo 'selected';
}
?>"
>
<?php
echo date("F",mktime(0,0,0,$i,1,date("Y")));
}
?>
</option>
And according to the above code, I am assigning the current month as $curmonth, and inside loop assigning the $allmonth for entire, month.
And inside the Value
<option value="<?php
echo $i;
if($curmonth==$allmonth)
{
echo 'selected';
}
?>"
>
<?php
echo date("F",mktime(0,0,0,$i,1,date("Y")));
}
?>
</option>
for checking if the current month equals all month and displaying the selected to make it select. But i am not getting result.. What i am getting is all the items are being displayed in the option select.
What i am missing ?

What you have missed is,
You are trying to display the selected inside the value
What you need to do is
<option value="<?php
echo $i;
?>"
<?php
if($allmonth==$curmonth)
{
echo ' selected';
}
?>
>
<?php
echo $allmonth;
}
?>
</option>
So, the result will be
<select >
<option value="1">
January<option value="2">
February<option value="3">
March<option value="4">
April<option value="5">
May<option value="6">
June<option value="7">
July<option value="8">
August<option value="9" selected>
September<option value="10">
October<option value="11">
November<option value="12">
December</option>

You're not correctly closing the <option> tags you are creating. Indenting you're code makes these issues more apparent:
<select>
<?php
for($i = 1 ; $i <= 12; $i++)
{
$allmonth = date("F",mktime(0,0,0,$i,1,date("Y")))
?>
<option value="<?php
echo $i;
if($curmonth==$allmonth)
{
echo 'selected';
}
?>"
>
<?php
echo date("F",mktime(0,0,0,$i,1,date("Y")));
//Close tag inside loop
?>
</option>
<?php
}

Hope you are wrong here
<option value="<?php
echo $i; ?>"
<?php
if($curmonth==$allmonth)
{
echo 'selected';
}
?>"
>
<?php
echo date("F",mktime(0,0,0,$i,1,date("Y")));
}
?>
</option>
You have missed closing quotes for the value

Take a look on this example:
$current = date('F');
for($i = 1 ; $i <= 12; $i++) {
$month = date("F",mktime(0,0,0,$i,1,date("Y")));
if( $current == $month )
echo $month . " - Selected \r\n";
else
echo $month . "\r\n";
}
IN HTML FORMAT:
echo "<select>";
$current = date('F');
for($i = 1 ; $i <= 12; $i++) {
$month = date("F",mktime(0,0,0,$i,1,date("Y")));
if( $current == $month )
echo "<option value='$month' selected='selected'>" $month . "</option>";
else
echo "<option value='$month'>" $month . "</option>";
}
echo "</select>";
DEMO

You have error with closing quotes in the value of options in the line
<option value="<?php
echo $i; if($curmonth==$allmonth)
{
echo 'selected';
}
?>"
^ // This is the quotes opened for value and is not properly closed
>
Here you have to close the quotes. As per your code it will display as:
<option value="9 selected" >
Also you are not closing <option> properly. Change your code to :
<select>
<?php
for($i = 1 ; $i <= 12; $i++)
{
$allmonth = date("F",mktime(0,0,0,$i,1,date("Y")))
?>
<option value="<?php echo $i;?>" <?php if($curmonth==$allmonth){echo 'selected';}?> >
^ // close quotes here
<?php
echo $allmonth;?>
</option>
}
</select>

Related

Set default value for select dropdown in Php

I'm a absolute newby for php and i'm struggling with just a basic thing.
I want to set a default value for a select box i have. Default value should be 2
My code
<div class="selector-wrapper">
<select id="adults" class="form-control" onchange="toggleSetGuests()">
<?php
for($i = 0; $i <= 20; $i++):
?>
<option value="<?php echo $i ?>" <?php if($i == 0){ echo('selected="true"'); } ?>>
<?php echo $i ?> Adults
</option>
<?php
endfor;
?>
</select>
</div>
So in this select box i get options from 0 - 20. I want to set the default value as 2 instead of 0
My approach was like below
<option value="<?php echo $i ?>" <?php if($i == 2){ echo('selected="true"'); } ?>>
But did not work.
How do i set the default value to be selected as the value 2
<option value="<?php echo $i ?>" <?php if($i == 2){ echo ' selected'; } ?>>
should work.You don't need selected=true

PHP getting value from a dynamic select list

I've got a dynamic select list, which is generated from MySQL. I've got no problem listing them, but I cant seem to get it's value when I submit the form. Here's my script for the Select:
<div class="form-group">
<select class="form-control" id="make" name="make">
<option value="">Make</option>
<?php
if ($result->num_rows > 0) {
$x = 1;
While($row = $result->fetch_assoc()) {
?>
<option value="<?php $row[fmake];?>" <?php if($_POST['make'] == $row[fmake]) echo 'selected="selected" '; ?>><?php echo $row[fmake];?></option>
<?php
$x = $x + 1;
}
}?>
</select>
</div>
And here's the script to get it's value:
if ($_POST["submit"]) {
$make = $_POST['make'];
when I do an echo for $make, I don't get anything at all. What went wrong? All help appreciated. Thanks
You forgot to echo $row['fmake'] and quotation marks mistake.
change this
<option value="<?php $row[fmake];?>" <?php if($_POST['make'] == $row[fmake]) echo 'selected="selected" '; ?>><?php echo $row[fmake];?></option>
by
<option value="<?php echo $row['fmake'];?>" <?php if($_POST['make'] == $row['fmake']) echo 'selected="selected" '; ?>><?php echo $row['fmake'];?></option>
shouldn't it be $row['fmake'] instead of $row[fmake]
<option value="<?php $row['fmake'];?>" <?php if($_POST['make'] == $row['fmake']) echo 'selected="selected" '; ?>><?php echo $row['fmake'];?></option>
Believe you're missing an echo.
value="<?php $row[fmake];?>"
Should be:
value="<?php echo $row[fmake];?>"

Drop down menu doesnt work

My Drop down menu to choose a date does not work unless I hold down left click on my mouse.
Only the year drop down works correctly but the month and days only work if you hold your mouse button down.
Here is the website it is on: www.cipslimoshuttle.com/tickets
Here is the code:
<label>
Date of travel:
<select name="ticketYear">
<?php
$i = date('Y');
while($i <= date('Y')+5){
?>
<option <?php if(date('Y') == $i) echo 'selected' ?> value="<?php echo $i ?>"><?php echo $i ?></option>
<?php
$i++;
}
?>
</select>
<select name="ticketDay">
<?php
$i = 1;
while($i <= 31){
?>
<option <?php if(date('j') == $i) echo 'selected' ?> value="<?php echo $i ?>"><?php echo $i ?></option>
<?php
$i++;
}
?>
</select>
<select name="ticketMonth">
<?php
$months = array(
'01-January',
'02-February',
'03-March',
'04-April',
'05-May',
'06-June',
'07-July',
'08-August',
'09-September',
'10-October',
'11-November',
'12-December'
);
foreach($months as $month) {
$month = explode('-',$month);
?>
<option <?php if(date('m') == $month[0]) echo 'selected' ?> value="<?php echo $month[0] ?>"><?php echo $month[1] ?></option>
<?php
}
?>
</select>
</label>
The problem is with your label tag You need to end before the select box.Check this it will work
<label>
Date of travel: </label>
<select name="ticketYear">
<?php
$i = date('Y');
while($i <= date('Y')+5){
?>
<option <?php if(date('Y') == $i) echo 'selected' ?> value="<?php echo $i ?>"><?php echo $i ?></option>
<?php
$i++;
}
?>
</select>
<select name="ticketDay">
<?php
$i = 1;
while($i <= 31){
?>
<option <?php if(date('j') == $i) echo 'selected' ?> value="<?php echo $i ?>"><?php echo $i ?></option>
<?php
$i++;
}
?>
</select>
<select name="ticketMonth">
<?php
$months = array(
'01-January',
'02-February',
'03-March',
'04-April',
'05-May',
'06-June',
'07-July',
'08-August',
'09-September',
'10-October',
'11-November',
'12-December'
);
foreach($months as $month) {
$month = explode('-',$month);
?>
<option <?php if(date('m') == $month[0]) echo 'selected' ?> value="<?php echo $month[0] ?>"><?php echo $month[1] ?></option>
<?php
}
?>
</select>

How to make only current year appear once?

I am have this query below that is a drop down for year (from 1900-current). I am using a $variable to show a previously chosen year first in the drop down for an edit form. It works except that for all other years 2011 is also repeated with them. How do i tweak it so that $y shows first and then all years underneath that?
Code:
<select name="year">
<?PHP for($i=date("1900"); $i<=date("2011"); $i++)
if($year == $i)
echo "<option value='$y' selected>$y</option>
<option value='$i' selected>$i</option>";
else
echo "<option value='$y' selected>$y</option>
<option value='$i'>$i</option>";
?>
</select></td>
</tr>
Keep it simple:
<select name="year">
<?php for($i=1900; $i<=date('Y'); $i++) {
if($i == date('Y'))
echo "<option value='$i' selected='selected'>$i</option>";
else
echo "<option value='$i'>$i</option>";
}
?>
</select></td>
</tr>
Or even better:
<select name="year">
<?php for($i=1900; $i<=date('Y'); $i++)
echo "<option value='$i' ".(($i == date('Y')?'selected="selected"':'')).">$i</option>";
?>
</select></td>
</tr>
And backwards
<select name="year">
<?php for($i=date('Y'); $i>=1900; $i--)
echo "<option value='$i' ".(($i == date('Y')?'selected="selected"':'')).">$i</option>";
?>
</select></td>
</tr>
<select name="year">
<?PHP
echo "<option value='$year' selected>$year</option>
for($i=1900; $i<=2011; $i++)
if($year != $i)
echo "<option value='$i'>$i</option>";
<option value='$i'>$i</option>";
?>
</select></td>
</tr>
Not sure what $y is, but I guess $year is the current year:
<select name="year">
<?php
for($i=date("1900"); $i<=date("2011"); $i++)
{
if($year == $i)
echo '<option value="'.$i.'" selected="selected">'.$i.'</option>';
else
echo '<option value="'.$i.'">'.$i.'</option>';
}
?>
</select>

How to set selected item when looping through php array

I have an html dropdown box. Then I use an array to fill the items in it.
The keys in this for each loop is just a number from 0 - 9. My problem now is how can I control what shows up as the default choice:
<?php foreach($cat_r as $k=>$c){ ?>
<option name="<?php echo $k + 1; ?>" value="<?php echo $k + 1; ?>" selected="<?php if($k==1){ echo "selected"; } ?>"><?php echo $k + 1; ?></option>
<?php } ?>
In this code, you can see that I'm attempting to make the 2nd item as the default choice.
But it seems like I'm always brought to the last array item whatever number I type as the condition.
Try this:
<?php foreach($cat_r as $k=>$c){ ?>
<option value="<?php echo $k + 1; ?>" <?php if($k==1){ echo 'selected="selected"'; } ?>><?php echo $k + 1; ?></option>
<?php } ?>
Or, this format works too
<option value="foo" selected />
<?php foreach($cat_r as $k=>$c){ ?>
<option name="<?php echo $k + 1; ?>" value="<?php echo $k + 1; ?>"
<?php if($k==1){ echo "selected=\"selected\""; } ?>>
<?php echo $k + 1; ?>
</option>
<?php } ?>
I came here for finding solution to set options selected by user before as selected , above question was in different context and I ended up in scratching my head to this thing...
foreach($options as $k){
echo "<option value=".$k." ";
foreach($selectedoptions as $m){
if($k==$m)
echo "selected='selected'";
}
echo ">".$k."</option>";
}
This code was functional for me, thanks..
foreach($options as $k){
echo "<option value=".$k." ";
foreach($selectedoptions as $m){
if($k==$m)
echo "selected='selected'";
}
echo ">".$k."</option>";
}

Categories