homepage Welcome to WebmasterWorld Guest from 54.237.98.229
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Home / Forums Index / Code, Content, and Presentation / Databases
Forum Library, Charter, Moderator: open

Databases Forum

    
having problem with IF ELSE to display content
hoom

5+ Year Member



 
Msg#: 4168600 posted 3:54 pm on Jul 12, 2010 (gmt 0)

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...

 

Demaestro

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



 
Msg#: 4168600 posted 4:08 pm on Jul 12, 2010 (gmt 0)

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.

LifeinAsia

WebmasterWorld Administrator lifeinasia us a WebmasterWorld Top Contributor of All Time 5+ Year Member



 
Msg#: 4168600 posted 4:22 pm on Jul 12, 2010 (gmt 0)

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.

hoom

5+ Year Member



 
Msg#: 4168600 posted 5:22 pm on Jul 12, 2010 (gmt 0)

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.

Demaestro

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



 
Msg#: 4168600 posted 5:33 pm on Jul 12, 2010 (gmt 0)

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.

hoom

5+ Year Member



 
Msg#: 4168600 posted 5:53 pm on Jul 12, 2010 (gmt 0)

Seems to be this:

<img src="logos/NULL" alt="" name="logoplaceholder" id="logoplaceholder" />

Demaestro

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



 
Msg#: 4168600 posted 6:06 pm on Jul 12, 2010 (gmt 0)

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"))

Demaestro

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



 
Msg#: 4168600 posted 6:10 pm on Jul 12, 2010 (gmt 0)

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')

hoom

5+ Year Member



 
Msg#: 4168600 posted 6:31 pm on Jul 12, 2010 (gmt 0)

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']; ?>

Demaestro

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



 
Msg#: 4168600 posted 6:45 pm on Jul 12, 2010 (gmt 0)

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"))

Demaestro

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



 
Msg#: 4168600 posted 6:54 pm on Jul 12, 2010 (gmt 0)

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.

hoom

5+ Year Member



 
Msg#: 4168600 posted 7:05 pm on Jul 12, 2010 (gmt 0)

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.

Demaestro

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



 
Msg#: 4168600 posted 7:16 pm on Jul 12, 2010 (gmt 0)

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'

hoom

5+ Year Member



 
Msg#: 4168600 posted 7:27 pm on Jul 12, 2010 (gmt 0)

Fantastic, thanks so much, really quick solution!

Demaestro

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



 
Msg#: 4168600 posted 7:32 pm on Jul 12, 2010 (gmt 0)

No prob,

If you run into anything else post back.

rocknbil

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



 
Msg#: 4168600 posted 12:40 am on Jul 13, 2010 (gmt 0)

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."

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Databases
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved