0

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); 
} 
?> 
3
Contributors
4
Replies
20
Views
2 Years
Discussion Span
Last Post by diafol
0

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"?

Edited by minitauros

2

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.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.