Home     Articles & Projects     Products & Web Services

Displaying different images based on an if statement

Hi,

I am trying to get a php web page to show a different image based on if a field in the database is yes or no.

I have created this code but it shows the same image regardless of the setting. Please can someone tell me where i have gone wrong

<?php
if ($row_ful_site['TouringPitches']='Yes') {
echo
"<img src=http://www.gocaravanning.com/siteicons/touring_caravans.jpg";
}
else {
echo
"<img src=http://www.gocaravanning.com/siteicons/blank.jpg";
}
?>

Many thanks in advance

Simon
www.4theUK.com

Hey Simon, I don't know what

Hey Simon,

I don't know what SQL you are using to get the yes or no. nor do i know if the Yes or No is an option for each member.
So here is my best code, I can think of.

While writting the code below a found one small error, when you want to do a TRUE statement in an IF statement the best way is to use =="YES" as for some reason I have not google'ed it but ="YES" does not work.

Also if I may make a suggestion when it comes to databases codes, I like to use number from 0 - 10 this way I am not relying on words like Yes for e.g. I use 9 and 10 as Yes and No, while this is not a hard code rule, nor do many people follow this rule, it is just my way of protecting my database, if I get hacked, as the information a hacker would collect would look like 0 and 1's

for e.g. a persons email address gets converted to a random string of 01010110101 and then stored in the sites database, while another database on another company server hosts what they string equals. While I am sure many developers call that going to far, I call it the SUPER SAFE SOURCE!

<?php
$sql
= "SELECT * FROM tbl_pitches";
$result = mysql_query($sql) or die ("Error Searching");
$row = mysql_fetch_assoc($result);
extract ($row);

if (
$TouringPitches=="Yes") {
print
"<img src=http://www.gocaravanning.com/siteicons/touring_caravans.jpg";
}
else {
print
"<img src=http://www.gocaravanning.com/siteicons/blank.jpg";
}
?>

Thanks so much. That did the

Thanks so much.

That did the trick perfectly

Simon
============
www.4theuk.com

Having got that working I

Having got that working I would now like to show a picture (the link is in a field in the database) if there is a link there.

I am using this code

<?php
if ($row_ful_site['Imageurl2']='%http%') {
echo
"<img scr=row_ful_site['Imageurl2]; ?>
";
}
else {
echo "<img src=http://www.gocaravanning.com/siteicons/blank.jpg";
}
?>

What I hoped this would do is display the picture in field Imageurl2 in the database if the field is not empty.

Can anyone help on this.

Many thanks

Simon

Hi Simon, Probably best just

Hi Simon,

Probably best just to test for an empty string - the %....% notation is SQL specific where % means wildcard. There were also unbalanced quote and brackets in the code you posted... - try something like this;

<?php
if ($row_ful_site['Imageurl2']) {
echo
"<img scr='".$row_ful_site["Imageurl2"]."' />";
}
else {
echo
"<img src='http://www.gocaravanning.com/siteicons/blank.jpg' />";
}
?>

Cheers,
David.

Hi David, Neither the

Hi David,

Neither the picture or the blank image display with the revised code

See under the gallery tab on

http://www.gocaravanning.com/test/detail.php?Name=Ashfield Caravan Park&Urn=2627

Any ideas what I am doing wrong

Many thanks

Simon

============
www.4theuk.com

Hi Simon, ooops - I went to

Hi Simon,

ooops - I went to view the source on your page and it revealed a typo in my code above, I had persisted scr instead of src... the code should be:

<?php
if ($row_ful_site['Imageurl2']) {
echo
"<img src='".$row_ful_site["Imageurl2"]."' />";
}
else {
echo
"<img src='http://www.gocaravanning.com/siteicons/blank.jpg' />";
}
?>

Cheers,
David.

p.s. Russell - thank you for helping out above...

Thanks David and

Thanks David and Russell.

Bingo - works now.

Many thanks for your help

Simon

Sorry to be a pain. What

Sorry to be a pain. What would i need to add to get a alt tag showing the 'Name' field and also to set the width to say 200.

I have tried playing about with a few ideas but nothing works for me.

Many thanks

Simon

Hi Simon, Based on the code

Hi Simon,

Based on the code above, assuming that the "Name" field is present even when an image URL is not, try something like this:

<?php
if ($row_ful_site['Imageurl2']) {
echo
"<img src='".$row_ful_site["Imageurl2"]."' width='200' alt='".$row_ful_site["Name"]."' />";
}
else {
echo
"<img src='http://www.gocaravanning.com/siteicons/blank.jpg' width='200' alt='".$row_full_site["Name"]."' />";
}
?>

Cheers,
David.

Hey guys, That's cool, David

Hey guys,
That's cool, David has told me so much, and help me so many times, always willing to help if I can.

Here is another tip for anyone using PHP.

If you are stuck, look at the code you have, I know that I have over 50,000 lines of code, and I sometimes find the answer in that.

<?php
if ($row_ful_site['Imageurl2']) {
echo
"<img src='".$row_ful_site["Imageurl2"]."' width='200' alt='".$row_ful_site["Name"]."' />";
}
else {
echo
"<img src='http://www.gocaravanning.com/siteicons/blank.jpg' width='200' alt='".$row_full_site["Name"]."' />";
}
?>

The the example above, this is how I work.

say I want to add the ID next to the number in the MySQL database or any database for that matter, I look and find the right table and the right colume. Most of the time I call ID, ID. So i know when I am doing my PHP code all i need to do is add $row_ful_site["ID"]; for e.g.

<?php
if ($row_ful_site['Imageurl2']) {
print
"".$row_ful_site["ID"]."<img src='".$row_ful_site["Imageurl2"]."' width='200' alt='".$row_ful_site["Name"]."' />";
}
else {
echo
"<img src='http://www.gocaravanning.com/siteicons/blank.jpg' width='200' alt='".$row_full_site["Name"]."' />";
}
?>

So in PHP101
Your row name equals the name you will place between the $row_ful_site[ and the ];
I hope that helps you in the future.

Thanks guys for all your

Thanks guys for all your help.

Simon

============
www.4theuk.com