Link to home
Start Free TrialLog in
Avatar of pda4me
pda4me

asked on

PHP echo help

i have a <img tag that uses 2 column values from a database to define the source.  This is working great as it provides the unique id and the photo number - a value of 1 to show the last <img in the image tag.

There is a discrepancy however in the PhotoCount and the actual <img name when the PhotoCount value is less than 10.  


Here is the current code:

<img width="200" height="150" src="/images/<?php echo(rawurlencode($row['ListingID'])); ?>_<?php echo(rawurlencode($row['PhotoCount']-1)); ?>.jpg" alt="<?php echo(rawurlencode($row['ListingID'])); ?>" align="left" vspace="3" hspace="3" />

Open in new window



The problem arrives is when the PhotoCount is a 4 for example.  The <img tag would show up with a source of ListingID_4.jpg

The problem is that the actual file name is ListingID_04.jpg

I cannot change the database values to update the PhotoCount colum to a 04 so that the filename matches the PhotoCount returned ...how do I allow for this within PHP to insert a 0 if the value of PhotoCount is < 10?
Avatar of Rik-Legger
Rik-Legger
Flag of undefined image

<img width="200" height="150" src="/images/<?php echo(sprintf("%02d",rawurlencode($row['ListingID']))); ?>_<?php echo(rawurlencode($row['PhotoCount']-1)); ?>.jpg" alt="<?php echo(sprintf("%02d",rawurlencode($row['ListingID']))); ?>" align="left" vspace="3" hspace="3" />

Open in new window


sprintf("%02d",rawurlencode($row['ListingID']))
will add a leading zero for numbers with 1 character.
You can also use str_pad

<img width="200" height="150" src="/images/<?php echo(str_pad($row['ListingID'], 2, '0', STR_PAD_LEFT)); ?>_<?php echo(rawurlencode($row['PhotoCount']-1)); ?>.jpg" alt="<?php echo(str_pad($row['ListingID'], 2, '0', STR_PAD_LEFT)); ?>" align="left" vspace="3" hspace="3" />

Open in new window

Avatar of pda4me
pda4me

ASKER

Neither of these are working?  The 0 needs to be inserted AFTER the underscore like this MLNumber_04.jpg?
ASKER CERTIFIED SOLUTION
Avatar of Rik-Legger
Rik-Legger
Flag of undefined image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of pda4me

ASKER

@Rik...LOL, yes but then I would have wanted some of the points back, which is not fair to you ;-)

I did try but must of screwed up a ) or something...
Avatar of pda4me

ASKER

super fast help and great replies, thanks!