Displaying an image with two variables

Hi There,

I am trying to post an image on a php document with the information pulling from a sql database, I have the img src pulling the correct picture but I have the same picture in different colors and I want the appropriate color to show in the proper place.

[php]ECHO “

<img src=“http://www.brickbybricks.ca/php/images/".$row[‘PartID’].".gif”>
”;[/php]

I want to add an extra directory using a variable so if ColorID = Red then it would go to directoy

[php]<img src=“http://www.brickbybricks.ca/php/images/red/".$row[‘PartID’].".gif”>";[/php][/php]

Well, tons of ways to do that. Depends on how you tell it to know the color change. Let’s say it is a value
in a field in the database. So, you pull that along with the row’s picture name. Then, just alter the echo to
handle it. Something loosely like:

$color = “red”;
ECHO “

<img src=“http://www.brickbybricks.ca/php/images/”. $color . “/” . $row[‘PartID’].”.gif">
";

You just have to add in the “red” folder name somehow… Depends on where you get that from. You need to
get it into the “img” tag…

I really don’t know the end game you are after, but you can handle it like this:

[php]
$colors = array(
‘red’,
‘blue’,
‘green’,
);

foreach ( $colors as $color ){
if ( file_exists("/php/images/$color/{$row[‘PartID’]}.gif" ){
echo “<img src=’/php/images/$color/{$row[‘PartID’]}.gif’>”;
}
}[/php]

Yes! More elegant than my sample! Really depends on how he determines what color it needs to be!

And, CNGHL, Note that Astonecipher checked if the file exists first. I use that all the time. If it does not
exist, I display a generic image that shows something like “image not available” stamped on it so the user
realizes it is missing. All good ideas for you!

Thank you all for your help! It worked

Glad you solved it… See you in the next puzzle…

I will mark this one solved!

Sponsor our Newsletter | Privacy Policy | Terms of Service