Richard Lynch wrote:
> On Thu, October 26, 2006 8:31 am, Jochem Maas wrote:
>> bunch of space wasters ;-)
>>
>> <?php foreach (range(1, 31) as $d) echo '<option value="',$d,'"',($d =
>> $selDay?'
>> selected="selected"':''),'>',$d,'</option>'; ?>
> 
> You messed up. :-)

dang :-P try again:

function genDayOptionTags($c = 31, $sv = 0, $eco = 1)
{
        $o = '';
        foreach (range(1, $c) as $d)
                $o .= '<option value="',$d,'"',
                      ($d == $sv?' selected="selected"':''),
                      '>',$d,'</option>';

        if ($eco) echo $o; return $o;
}

genMonthSelectTag($m = 1, $sv = 0, $attrs = array(), $eco = 1)
{
        // allowed attribs
        static $dattr = array('name' => 1, 'id' => 1, 'class' => 1, 'name' => 
1,);
        // speedy literal for looking up number of days
        static $h = array(1,2,3,4,5,6,7,8,9,10,11,12);
        // 'normalize' month (13 -> 1, 14 -> 2, 15 -> 3, etc)
        $m -= ((intval($m) % 12) * 12);

        // php5.1.0RC1 or higher for array_intersect_key
        // otherwise STW for a userland variant (PEAR)?
        $attrs = array_intersect_key($attrs, $dattrs);
        foreach ($attrs as $k => $v) $attrs[$k] = $v ? "{$k}=\"{$v}\"": '';

        $o = '<select name="'.join(' ', 
array_filter($attrs)).'>'.genDayOptions($h[$m], $sv, 0).'</select>';

        if ($eco) echo $o; return $o;
}

the point being that if your going to put things like this into a function
then you might as well do it properly and completely generalize the generation 
of
a select tag and it's options. (as opposed to the above banality :-)

> 
> $d == $selDay would be the correct expression in the middle of that.
> 
> My personal choice is:
> 
> $last_day = 31; //calculated from date()/mktime() etc
> for ($day = 1; $day <= $last_day; $day++){
>   $selected = $chosen_day == $ ? 'selected="selected"' : '';

$ ? ;-)

>   echo "  <option $selected>$day</option>\n";
> }

lately I have been fond of dropping braces around single if/foreach expressions,
I like the fact it makes the code compacter.

> 
> I don't *think* the w3c requires/recommends a value= in there, if the
> label *IS* the value, but can live with it either way...
>   echo "  <option value=\"$day\" $selected>$day</option>\n";
> is fine.
> 
> I used to be distracted by \" but I've grown so used to it that it no
> longer bothers me in my reading flow, unless it's excessive and the
> expression spans multiple lines.

I know that feeling. :-)

> 
> Separation of logic and presentation is all very well, but this isn't
> even business logic.  It's "presentation logic" which, to me,
> shouldn't be crammed into the complex business portion of my
> application.

here, here.

> 
> I'd much rather have the complex business logic focus on the
> application needs than the minutae of date formatting.

quite, why would business logic need to deal with anything other
than a timestamp (+zone) ?

> 
> It's all down to personal choice, though.



> 

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to