Welcome Guest, Not a member yet? Register   Sign In
Date dropdown from array
#1

[eluser]33cent[/eluser]
Hi,

i'm currently working on some project, which has need for date dropdown box. I'd like to "feed" my form_dropdown with array generated by this function, but i haven't much experience with this, so i need your help.

Code:
function dates()
{
$d= date("d");
$m= date("m");
$y= date("Y");
// show last 3 days
$days = "3";
    
for($i=0; $i<=$days; $i++)
{
$d1 = date('Y-m-d',mktime(0,0,0,$m,($d-$i),$y));
$d2 = date('d. m. Y. ',mktime(0,0,0,$m,($d-$i),$y));
}
$data = array ($1 => $d2);
return $data;
}

This part of code returns:
Code:
Array ( [2009-11-26] => 26. 11. 2009. )

But i need complete array, from today to three days in the past. Please help.
P.S. Variable $days must be easy changeable.

Thanks!
#2

[eluser]CroNiX[/eluser]
Try changing
Code:
$data = array ($1 => $d2);

to
Code:
$data[] = array ($1 => $d2);

and putting it inside your loop.

The way you have it now you are only adding the final value from your loop which is why you are only getting one result.
#3

[eluser]33cent[/eluser]
Excellent! Thanks!

But, i got output like this:
Code:
<select name="date">
<optgroup label="0">
<option value="2009-11-30" selected="selected">30. 11. 2009. </option>
</optgroup>
<optgroup label="1">
<option value="2009-11-29">29. 11. 2009. </option>
</optgroup>
<optgroup label="2">
<option value="2009-11-28">28. 11. 2009. </option>
</optgroup>
<optgroup label="3">
<option value="2009-11-27">27. 11. 2009. </option>
</optgroup>
</select>

How can i remove that "optgroup" labels from array?
Because, it looks like this:
Quote:http://i47.tinypic.com/2dqt3yb.png
#4

[eluser]CroNiX[/eluser]
try
$data[$d1] = $d2;
#5

[eluser]33cent[/eluser]
That's it! Thanks a lot! ;-)




Theme © iAndrew 2016 - Forum software by © MyBB