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:
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?
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" />
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?
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" />
ASKER
Neither of these are working? The 0 needs to be inserted AFTER the underscore like this MLNumber_04.jpg?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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...
I did try but must of screwed up a ) or something...
ASKER
super fast help and great replies, thanks!
Open in new window
sprintf("%02d",rawurlencod
will add a leading zero for numbers with 1 character.