i want to set a default value of option box and that value is fetch from database. here is my code it works fine with number but my month are given in alphabetical order like "Jan","Feb".. here is the code I try

<?php 
$month=array("Jan","Feb","Mar","Apr","May","June","July","Aug","Sep","Oct","Nov","Dec"); foreach($month as $m)
{ 
printf('<option value="%d"%s>%d</option>', $m, $birth[1] == $m ? 'selected="selected"' : '', $m); 
} 
?> 

its an array contain month name like jan, mar

Try this:

<?php 
$month=array("Jan","Feb","Mar","Apr","May","June","July","Aug","Sep","Oct","Nov","Dec"); foreach($month as $m)
{ 
    printf('$m = %s, $birth[1] = %s, same? %s', $m, $birth[1], $m == $birth[1] ? 'yes' : 'no');
} 

Is there any record that says "yes"?

Member Avatar

diafol

WHy are you using %d when $m is a string? Use %s as suggested by minitauros.

$birth = [10,'June',1964];
$month=array("Jan","Feb","Mar","Apr","May","June","July","Aug","Sep","Oct","Nov","Dec");
foreach($month as $m)
    printf("<option value='%s' %s>%s</option>", $m, (strcasecmp($birth[1], $m) == 0) ? "selected='selected'" : "", $m);

The date month formats aren't standard. Usually they revolve around a 3-character short version, i.e. Jun and Jul
You mentioned that $birth[1] was a month, but as lowercase. In this scenario, you need to compare the strings in a case-insenstive manner.