INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Special Fields Turned on or off with Conditional Statements

Special Fields Turned on or off with Conditional Statements

Special Fields Turned on or off with Conditional Statements

(OP)
I've used some special fields which I would like to call up only if they have a record against them. At the moment all I have is the echo for them - I'm unsure how to wrap them in a conditional statement i.e. if the record exists then echo it, if not then don't show anything. Here is the code currently:

CODE

    

<div class="logbookpositionl">
      <?php the_time('d F, Y') ?><br><br>
      <?php $key="leg"; $value =get_post_meta($post->ID, $key, true); echo "".$value;?><br><br>      
      <?php $key="lat"; $value =get_post_meta($post->ID, $key, true); echo "".$value;?><br>
      <?php $key="lon"; $value =get_post_meta($post->ID, $key, true); echo "".$value;?>
    </div>
<div class="logbookpositionr">
     <?php $key="vessel"; $value =get_post_meta($post->ID, $key, true); echo "Vessel: ".$value;?><br><br>
      <?php $key="distance"; $value =get_post_meta($post->ID, $key, true); echo $value." nautical miles";?> <br><br>
      <?php $key="weather"; $value =get_post_meta($post->ID, $key, true); echo "Weather: ".$value;?>       
    </div>

You can see in the second section I have included some descriptive text. At the moment this is being echoed irrespective of the fact there isn't a record in there.

I'm pretty new to PHP so any help would be gratefully received.

Thanks in advance.

RE: Special Fields Turned on or off with Conditional Statements


You should be able to prefix your echo statements with an if statement, something like this:

CODE

<?php
    $key = "distance";
    $value = get_post_meta($post->ID, $key, true);
    if ($value != "") echo $value . " nautical miles";
?>

Hope this helps,
Dan

 

Coedit Limited - Delivering standards compliant, accessible web solutions

Dan's Page @ Code Couch: http://www.codecouch.com/dan/

Code Couch Tech Snippets & Info: http://www.codecouch.com/
 

RE: Special Fields Turned on or off with Conditional Statements

(OP)
Hi Dan,

Thanks for your reply. I'll be trying this out this afternoon.

RE: Special Fields Turned on or off with Conditional Statements

(OP)
Sorry Dan, forgot to mention this worked well for me. Thanks.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close