--- In php-list@yahoogroups.com, James Keeline <[EMAIL PROTECTED]> wrote:
>
> --- [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote:
>
> > $number = 0 + date('n');
> > if($number > 6) {$number = $number - 6;}
> > echo('<img src="images/topbanner' . $number
> > . '.jpg" />');
> >
> > you were missing the . before jpg
>
> Given the way that $number is used, it is probably not necessary to
force it to be a number by adding zero.  It's a good trick though.
Here's another:
>
> $number = (date('n') % 6) + 1;
>
> The modulus (%) operator says divide by this number and return the
remainder.  Since we want values from 1..6 rather than 0..5, I add one
to the result.
>
> The hard-coded 6 could be a variable with a count of the number of
banner items you have so if you make more, they will be added to the
loop.  It's not very random, of course, but I think you knew that from
the start.
>
> HTML will allow you to use single or double quotes around
properties.  However, when using print or echo, single quotes around
the whole thing does not allow variable names to be replaced with
their values.  This will work:
>
> print "<img src='images/topbanner$number.jpg' />";
>
> but it is a little hard to read.  The printf() function is one worth
mastering.  Since we know we want a number, we can use %d as the
substitution format placeholder.  Normally %s is fine as well:
>
> printf("<img src='images/topbanner%d.jpg' />", $number);
>
> At all times, make your code easy to read.  When you have to look at
it again in 6 months, you'll appreciate readable code.  Backslashes
and excessive string concatenation can be hard to read and it is easy
to miss a tiny piece of punctuation.
>
> James
>
---------------------------
Yes, the client did not need the banners to be random, he just wanted
them to change automatically.  I had him setup where all he had to do
was open a banner in an image editor, decide that this is the one that
he wants to use now for this month, save it as currentTopBanner.jpg
and he was set.  Well, that was too complicated for him!!  He didn't
have a simple image editor.  He got confused about how to just rename
from within Windows Explorer.  So, he was asking if I knew of any
banner rotation code out there.  At first I thought that was overkill,
then I thought of how easy it would be to write a php script.

Ok, I'm a bit confused on the single and double quotes.  If single
quotes does not replace the variable, then how would this work:
echo('<img src="images/topbanner' . $number. '.jpg" />');
The first choice from below?   Would that work with the single quotes?
I see that this would work:
print "<img src='images/topbanner$number.jpg' />";

I thought I was going to have to worry about escaping the < > and "
characters but none of these solutions require that.  Why?
Thanks,
Bruce

Reply via email to