By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,831 Members | 2,237 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,831 IT Pros & Developers. It's quick & easy.

If statements for Image Selection

P: 2
Hi...
Fairly new to PHP and trying to work with some images...

I have a page where I need to display a different header image on based on a business type that is coming from MySQL... There are seven total options... is there an easy if statement that can handle this in PHP?
Feb 16 '08 #1
Share this Question
Share on Google+
4 Replies


hsriat
Expert 100+
P: 1,654
Hi...
Fairly new to PHP and trying to work with some images...

I have a page where I need to display a different header image on based on a business type that is coming from MySQL... There are seven total options... is there an easy if statement that can handle this in PHP?
Create an array with each business type as keys and corresponding image columns (or rows) as their values.
[php]query = array("business1"=>"b1image","business2"=>"b2image ");
//other code
$sql = mysql_query("SELECT `".query['businessX']."` FROM `table`");[/php]
Feb 16 '08 #2

ronverdonk
Expert 2.5K+
P: 4,258
Hi...
Fairly new to PHP and trying to work with some images...

I have a page where I need to display a different header image on based on a business type that is coming from MySQL... There are seven total options... is there an easy if statement that can handle this in PHP?
Since I can see no code something like the switch method:
[php]
$business_type=... whatever comes from the database
switch ($business_type) {
case 'a' : $img = 'type1.png';
break;
case 'b' : $img = 'type2.png';
break;
............................
............................
case 'g' : $img = 'type7.png';
break;
default : $img = 'noimage.png';
break;
}
echo "<img src='$img' border='0' />"
--- etc.
[/php]
Another method is to store it in an associative array and get the image name from there:
[php]
$array=array('a'=>'type1.jpg', 'b'=>'type2.jpg', ......... 'g'=>'type7.jpg');
if (isset($array[$business_type]))
$img=$array[$business_type];
else
$img='noimage.png';
echo "<img src='$img' border='0' />"
..... etc ....
[/php]
Ronald
Feb 16 '08 #3

P: 2
Maybe the code will help...

Basically... this was an afterthought. I have a template built that in the body portion is pulling in a "more info" type section for a business listing. Then I realized that in the top of the template i need the main pic to correspond with the business type that's pulling from the database.

The image section is just normal html:
<td align="left" valign="top"><!-- InstanceBeginEditable name="headerpic" --><img src="images/mbw_int_main.jpg" width="821" height="155"><!-- InstanceEndEditable --></td>

My main select and query statement are down in the body.

The business types are golf, accommodations, shopping, nightlife, etc... these are the "business_type" pulling from the database.

The "case a" etc seems the easiest to me, but forgive my ignorance...

Would I place the "case" inside the <td> where the image is now?
Feb 16 '08 #4

ronverdonk
Expert 2.5K+
P: 4,258
Assuming varaible $img holds the image path/name, you can write the image in your statement via PHP, as follows:
[html]
<td align="left" valign="top"><!-- InstanceBeginEditable name="headerpic" --><img src="<?php echo $img ?>" width="821" height="155"><!-- InstanceEndEditable --></td>
[/html]
Ronald
Feb 16 '08 #5

Post your reply

Sign in to post your reply or Sign up for a free account.