简体   繁体   中英

PHP Displaying current month in option select

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

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>

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM