-4

need php help

I need to add the if statement inside the html tag

if( get_field($image) ):
    ?><img src="<?php the_field($image); ?>" alt="" /><?php
endif;

I want to add the if statement inside the html tag below in place of img and a, is that possible?

 echo 

    "<$html class=\"" .(!empty($this->options['class']) ? trim($thesis->api->esc($this->options['class'])) : ''). "\">  

 // I want this if statement to work inside here...how do i escape the html to make it work?

if( get_field($image) ):
        ?><img src="<?php the_field($image); ?>" alt="" /><?php
    endif;


    <img src=\"" .get_field($image)."\" alt=\"\" /> 
     <a href=\"".get_field($download). "\"> Download File</a> 

    </$html>\n";
hakre
  • 193,403
  • 52
  • 435
  • 836
brodster
  • 77
  • 1
  • 2
  • 12
  • 5
    The most unreadable code, I have ever seen. Is this obfuscated? – Leri Aug 14 '13 at 08:08
  • the bottom code works just fine, I need to add the if statement and only display the image if it exists – brodster Aug 14 '13 at 08:12
  • WordPress with Types? – Rolice Aug 14 '13 at 08:12
  • 2
    I can't figure out what your question is about, but I think it's related to the [Escaping from HTML](http://es1.php.net/manual/en/language.basic-syntax.phpmode.php) chapter. Please have a look at it. – Álvaro González Aug 14 '13 at 08:12
  • Yes you can bring that together. What you do here is that you nest if-clauses. However even this works, take care. Your code is already hard to read and nesting hard to read code into other hard to read code will result in even harder to read code - a development nightmare. Instead try to write software without a single if statement (just as an exercise). – hakre Aug 14 '13 at 08:16
  • yes its about escaping the html, i need to add an if statement to the img, Like in the example above.. How do i do that inside the <$html>? – brodster Aug 14 '13 at 08:17
  • how can i not display the img tag if it does not exist? without using the if statement – brodster Aug 14 '13 at 08:20
  • without using if statement is some kind of software design that would go beyond answering the question, see here how that works: [If-less programming (basically without conditionals)](http://stackoverflow.com/q/7264145/367456) – hakre Aug 14 '13 at 08:23
  • This shouldn't be closed. It's badly worded but he shows understanding of the subject and it's obvious what he's asking. – Glitch Desire Aug 14 '13 at 09:05

1 Answers1

2

Putting PHP operators in HTML

First off, don't use the echo statement to spit out huge chunks of HTML, it makes code very hard to maintain and re-use. Isn't this easier?

<a href='<?php echo $some_variable; ?>'>

Using PHP logic in HTML blocks (general)

You're looking for something like this:

<?php if(!empty($image)): ?>
    <img src='<?php echo $image; ?>' alt='Some Stuff'>
<?php endif; ?>

This is a short-hand equivelant called a ternary operator which may be easier to read in code:

<?php echo empty($image) ? null : "<img src='$image' alt='Some Stuff'>"; ?>

This will echo an image tag if $image has a value, and nothing if it doesn't.

Let's clean up & fix the code in the original post...

Your code looks like it has been deliberately obfuscated to confuse people. Learn to indent, don't embed logic within logic. Prioritize readability and your code will be a lot easier to maintain.

if(!empty($text)) 
        echo 

"<$html class=\"" .(!empty($this->options['class']) ? trim($thesis->api->esc($this->options['class'])) : ''). "\">  

<img src=\"" .get_field($image)."\" alt=\"\" />  " .get_field($text)." 
<a href=\"".get_field($download). "\"> Download File</a> 

</$html>\n";

There is a lot that can be improved here. First of all, separate business logic out from display logic as much as possible:

Business logic

<?php
    // This should be in another FILE ideally...
    $this->divClass = empty($this->options['class']) ? null : trim($thesis->api->esc($this->options['class']));
    $this->image    = the_field($image);
    $this->download = the_field($download);
    $this->text     = // I dont know how you're setting this.
?>

Display logic

Next, lose the get_field functions, add a null return to the_field if it's not found, that way you have cleaner code. Then, just use something like this:

<?php if(!isset($this->text)): ?>
    <div class='<?php echo $divClass; ?>'>
    <?php if(!isset($this->image) && !isset($this->download)): ?>
        <img src='<?php echo $this->image; ?>'>
        <a href='<?php echo $this->download; ?>'>Download File</a>
    <?php endif; ?>
    </div>
<?php endif; ?>

The <?php> tags are there to help you, they allow you to cleanly interpolate PHP code with HTML code in a way that most languages have to resort to ugly external tempating for. Use them, keep your code readable and understandable, don't take shortcuts because they will come back to bite you.

Glitch Desire
  • 14,632
  • 7
  • 43
  • 55
  • nice try but no, you are missing the the_field($image) – brodster Aug 14 '13 at 08:29
  • @brodster: Please don't complain that you're not well with the *basic syntax* of the PHP language, e.g. how to write the syntax (not saying it's totally easy, however it's not the answers fault, but yours). You find it outlined here, it's basic PHP: http://www.php.net/langref - read this chapter top to bottom and do all the examples your own. – hakre Aug 14 '13 at 08:34
  • @brodster I'm not doing your work for you, you can add your own functions in the code in place of my generic PHP functions. My job is to help everyone who has these issues and if I use your functions, I will end up confusing people who will think `the_field` is a PHP core function. Besides, you haven't even included the code for that function so I have no idea what it will return. – Glitch Desire Aug 14 '13 at 08:40
  • i am not complaining, and i dont want you do my work for me all i need help is secaping the if statement ...................................... maybe lie this ". (if( get_field($text) ): ''). "\"\"". endif ." – brodster Aug 14 '13 at 08:52
  • @brodster `` would be how I do that (though I'd store `the_field($text)` in a variable, that way I can just plug it straight into the string and not need ugly concatenation. Having said that, you don't need your `get_field` function, you can just check if `the_field($var)` is set. – Glitch Desire Aug 14 '13 at 09:01
  • @brodster I have edited my answer. I hope it helps. – Glitch Desire Aug 14 '13 at 09:10
  • you forgot to close the ing tag – brodster Aug 14 '13 at 09:20
  • @brodster In HTML5 `` tags don't need to be explicitly closed any more, if you're using an XHTML, they do. – Glitch Desire Aug 14 '13 at 09:25
  • you are rigt! the last example did not work unfortunately.. its needs to be in this format,,, if( get_field('field_name') ): ?> – brodster Aug 14 '13 at 09:58
  • @brodster My last example wont work unless you fix the `the_field` function like I suggested. :P – Glitch Desire Aug 14 '13 at 10:08
  • $text is a variable ... $text = !empty($this->options['text']) ? $this->options['text'] : ''; – brodster Aug 14 '13 at 10:12
  • @brodster You should use `null` instead of the empty string (`''`) for readability. – Glitch Desire Aug 14 '13 at 10:16
  • i see that, but this one has a default of div $html = !empty($this->options['html']) ? $this->options['html'] : 'div'; – brodster Aug 14 '13 at 10:38