Welcome to WebmasterWorld Guest from 54.145.235.72

Forum Moderators: open

having problem with IF ELSE to display content

   
3:54 pm on Jul 12, 2010 (gmt 0)

5+ Year Member



Hi - having a little problem with this one. I have a results page which shows data from a mysql db table, but I want to be able to "switch off" certain elements in the results page table using IF...ELSE and can't quite work out how to do this.
For example, some of the listings on the results page have a logo, some don't - I have a dynamically linked placeholder for the logo, but rather than having a red cross or other missing image symbol, I want to be able to wrap this element in a statement which basically says "if the logo field is empty, don't display this item" -I'm sure it's pretty straightforward but I can't quite get my head round it and would be grateful for an idiot-proof statement of how to achieve this. Thanks in advance...
4:08 pm on Jul 12, 2010 (gmt 0)

WebmasterWorld Senior Member demaestro is a WebmasterWorld Top Contributor of All Time 10+ Year Member



hoom,

Depending on your data there are a few ways to do this.

If the image field is null then you can use coalesce.

select coalesce(img_field, '/images/placeholder') as img_field from table

If the image field is an empty string then you may want to use a case/switch statement or an if.

select
case img_field
when (img_field == '') then '/images/placeholder'
else img_field
end
from table


or

select
if (img_field=='','/images/placeholder', img_field)
from table

My syntax might be a little off, I am going from memory. but if you search for "sql case" or "sql coalesce" or "sql if else" you will find lots of good examples.
4:22 pm on Jul 12, 2010 (gmt 0)

WebmasterWorld Administrator lifeinasia is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month



Another option is to handle that part of the logic in your web code instead of the DB SELECT statement. Especially since you're going to have to check in the code anyway to see if you need to use an <img> tag or not for each listing.
5:22 pm on Jul 12, 2010 (gmt 0)

5+ Year Member



Thanks to both of you for the responses. I'm not sure I got across exactly what I'm trying to do here - I have wrapped the dynamic placeholder in an IF ELSE statement as follows:

<?php if(($row_WADAbodyskinhaircare['logo'] != NULL) && ($row_WADAbodyskinhaircare['logo'] != "")) { ?>
<img src="<?php echo $row_WADAbodyskinhaircare['logo']; ?>" alt="" name="logoplaceholder" id="logoplaceholder" />
<?php } else { echo "&nbsp;"; } ?>

and I'm obviously doing something wrong somewhere because although the value in the 'logo' column in the db table is NULL, I'm still getting a missing image icon showing up. And I can't see how to tweak this so that it doesn't.
5:33 pm on Jul 12, 2010 (gmt 0)

WebmasterWorld Senior Member demaestro is a WebmasterWorld Top Contributor of All Time 10+ Year Member



When you view the source of the page what is rendered in the img tag scr?

View source on a broken image and tell us what is being put in the src, alt and id.
5:53 pm on Jul 12, 2010 (gmt 0)

5+ Year Member



Seems to be this:

<img src="logos/NULL" alt="" name="logoplaceholder" id="logoplaceholder" />
6:06 pm on Jul 12, 2010 (gmt 0)

WebmasterWorld Senior Member demaestro is a WebmasterWorld Top Contributor of All Time 10+ Year Member



Ah... It looks like the value is the string 'NULL' not a NULL object.

Try this:
<?php if(($row_WADAbodyskinhaircare['logo'] != NULL) && ($row_WADAbodyskinhaircare['logo'] != "") && ($row_WADAbodyskinhaircare['logo'] != "NULL"))
6:10 pm on Jul 12, 2010 (gmt 0)

WebmasterWorld Senior Member demaestro is a WebmasterWorld Top Contributor of All Time 10+ Year Member



Actually wait......

do you have /logos/ hard coded in your code but not in your example?

Do you have this?
<img src="<?php echo $row_WADAbodyskinhaircare['logo']; ?> />

Or This:
<img src="/logos/<?php echo $row_WADAbodyskinhaircare['logo']; ?>

Where did the /logos/ path come from? is that part of the $row_WADAbodyskinhaircare['logo'] value?

If so then you need to check:

if(($row_WADAbodyskinhaircare['logo'] != 'logos/NULL')
6:31 pm on Jul 12, 2010 (gmt 0)

5+ Year Member



OK - when you said (earlier) what's rendered in source view for img, I realised that I hadn't got the path correct - I just have the logo name in the db table, and usually drop in the "logos/.." before the code so that they show up, so before I replied I did just that.
So...I have this:
<img src="/logos/<?php echo $row_WADAbodyskinhaircare['logo']; ?>
6:45 pm on Jul 12, 2010 (gmt 0)

WebmasterWorld Senior Member demaestro is a WebmasterWorld Top Contributor of All Time 10+ Year Member



Ok, I was just making sure you had it hard coded in.

Then this should work:

<?php if(($row_WADAbodyskinhaircare['logo'] != NULL) && ($row_WADAbodyskinhaircare['logo'] != "") && ($row_WADAbodyskinhaircare['logo'] != "NULL"))

You could also make them ORs instead of ANDs. I think it is more efficient.

<?php if(($row_WADAbodyskinhaircare['logo'] != NULL) || ($row_WADAbodyskinhaircare['logo'] != "") || ($row_WADAbodyskinhaircare['logo'] != "NULL"))
6:54 pm on Jul 12, 2010 (gmt 0)

WebmasterWorld Senior Member demaestro is a WebmasterWorld Top Contributor of All Time 10+ Year Member



If you want, you can create a generic image and put it in the /logos/ folder.

Call it something like logo_not_found.jpg and just make it a picture of a something that would represent logo not found. For example you can make an image with the word LOGO and put a red circle around it with a line through it... like a no smoking sign.

then do this:

<?php if(($row_WADAbodyskinhaircare['logo'] != NULL) || ($row_WADAbodyskinhaircare['logo'] != "") || ($row_WADAbodyskinhaircare['logo'] != "NULL"))
<img src="logos/<?php echo $row_WADAbodyskinhaircare['logo']; ?>" alt="" name="logoplaceholder" id="logoplaceholder" />
<?php } else { <img src="logos/logo_not_found.jpg" alt="" name="logoplaceholder" id="logoplaceholder" /> } ?>

This way if there is no logo it will use the "Not Found Logo".

I do this so that there is always an image even if the image is telling you there is no image.
7:05 pm on Jul 12, 2010 (gmt 0)

5+ Year Member



Many thanks for the help, very much appreciated. I now have this fixed and working as I wanted, the only problem is that my logos column in the db (in all 6 tables) have neither nothing nor NULL in, but a single space, which means a major find and replace job looms...
Thanks again.
7:16 pm on Jul 12, 2010 (gmt 0)

WebmasterWorld Senior Member demaestro is a WebmasterWorld Top Contributor of All Time 10+ Year Member



hoom,

That stinks.

You can clean up your db like this:

update table_name set logo = '' where logo = ' ' or logo = 'NULL' or logo is NULL


This will make it an empty string if there is a space, or if it is Null or if it = 'NULL'
7:27 pm on Jul 12, 2010 (gmt 0)

5+ Year Member



Fantastic, thanks so much, really quick solution!
7:32 pm on Jul 12, 2010 (gmt 0)

WebmasterWorld Senior Member demaestro is a WebmasterWorld Top Contributor of All Time 10+ Year Member



No prob,

If you run into anything else post back.
12:40 am on Jul 13, 2010 (gmt 0)

WebmasterWorld Senior Member rocknbil is a WebmasterWorld Top Contributor of All Time 10+ Year Member



Well, there's a more encompassing solution. What if, for whatever reason, the actual file gets deleted but it's still in the db? If you just check for the file existence, you don't need to worry about empty or null.

$no_img = '/images/no-image.jpg'; // URL to your default image

Store whatever's in DB in $img in your while loop, then

$display_img = (is_file("/full_path_to/$img"))?"/customer_images/$img":$no_img;

<img src="$display_img" alt="$display_img">

Should really use GD/ImageMagick and add the width/height of the image, but not "required."
 

Featured Threads

My Threads

Hot Threads This Week

Hot Threads This Month