Jump to content

if query


kaz
 Share

Recommended Posts

Funny, how an if statement behaves. I wanna check whether a field has content.
So I asked about the field:

if ($article->gallery_images)

If yes, I only want to output only the first image as a preview in an summary page, that's why I only retrieve these first image:

if ($article->gallery_images) {
echo "
<div class='' uk-grid>
        <div class='uk-width-1-5@m uk-width-1-1@s'>
                <img src='{$article->gallery_images->first->url}' alt='{$article->gallery_images->first->description}'>
        </div>
        <div class='uk-width-4-5@m uk-width-1-1@s'>
        $content
</div>
</div>";
} else {
<div class='uk-width-1-1'>
        $content
</div>";
}

The query does not work, even if no data is uploaded to the gallery_images field, is else not displayed. Now the funny part: When I add ->first to the if query, else is displayed, the query will work:

if ($article->gallery_images->first) {
echo "
<div class='' uk-grid>
        <div class='uk-width-1-5@m uk-width-1-1@s'>
                <img src='{$article->gallery_images->first->url}' alt='{$article->gallery_images->first->description}'>
        </div>
        <div class='uk-width-4-5@m uk-width-1-1@s'>
        $content
</div>
</div>";
} else {
<div class='uk-width-1-1'>
        $content
</div>";
}

I have hardly any PHP experience, so perhaps I don't understand why this is the case, but: it surprised me in terms of logic.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...