.oO(Brett)
>I had a little problem with the code as you posted it - I was getting
>errors
I admit - it was untested.
> - so after fiddling with it some, I got it to work. I hope I
>didn't do something I shouldn't have. The change I made was in the line
>with 'selected'. I put a closing double quote just before the colon
>and two single quotes after it. It seems to be working just fine. Did
>I do something wrong?
Nope, looks correct. The single quotes were already there, it was just
the double quote before the colon that was missing. So the string wasn't
closed and the parser complained somewhere later.
>Also Micha, I'm not completely clear on how this code works,
>particularly <option value='%02u'%s>%s</option>. If it's not too much
>trouble would you explain how that works?
The printf()/sprintf() functions take a format string as the first
parameter and an arbitrary number of additional arguments. The format
string may contain placeholders, which are then replaced by the actual
values. In the example above there are three placeholders in the string
(the '%' thingies) and three additional arguments passed to the function
after the format string, which will replace the placeholders in the
given order.
There are various types of placeholders. The %s denotes strings, they
are simply replaced without modifications. In this case the first %s
placeholder becomes " selected='selected'" if the condition matches, the
second %s becomes the name of the month, taken from the $months array.
The very first placeholder %u is different, it stands for an unsigned
integer. Whatever you pass as an argument to the function to replace
this %u will be treated as an unsigned integer. In this case it's just
the loop variable $i.
In addition to these various types of placeholders there are some
options you can use. The %u placeholder for example can be extended to
output the value with any arbitrary number of leading spaces or zeros.
The %02u in my second example will output the value $i as an unsigned
integer with at least two digits, left-padded with zeros if necessary.
Probably it sounds more complicated than it actually is.
In short: printf()/sprintf() are really powerful functions when it comes
to the creation of strings with a lot of embedded variables or, as in
the examples above, even more complex expressions. The alternative would
be to use a lot of string concatenation, but usually these functions
lead to better readable code and are easier to maintain or extend. Have
a look at the PHP manual for more options and examples.
http://www.php.net/sprintf
>Thanks again.
You're welcome.
Micha