Jump to content

How to display something else if query is empty?


JamesWood

Recommended Posts

Hi guys

I'm super new to PHP, so bear with me!  But I have a super simple code here that will display all images in a folder, and print the 'header' IPTC metadata and then print the filename.  My query is this -- it currently repeats the last available IPTC header, even if the next image doesn't have any metadata stored in it.  So how can I tell the code to simply not print anything at all if the IPTC data isn't stored in the file?

Thanks a lot

James  (here's the code)

 

<?php
$files = glob("*.*");

    for ($i=0; $i<count($files); $i++) {
        $image = $files[$i];

        echo '<img src="'.$image .'" title="'.$image .'" alt="'.$image .'" width="5%"/>'."<br />";

            $picinfo = array();
            getimagesize($image, $picinfo);

            if(isset($picinfo['APP13']))
            {
            $iptc = iptcparse($picinfo["APP13"]);
            }

            if (is_array($iptc)) {
            $description = $iptc['2#105'][0]; /* IPTC 'Header' metadata */
            }

            print_r($description);
            print "<br />"; print $image ."<br /><br />"; /* Print filename */
    }
?>

Link to comment
Share on other sites

You already have something that will print information if it's available. What's more, it makes sense: if you have $iptc data then print what's in it. Logical. Obviously, that then means that if you don't have $iptc data then it won't print. Also logical.

So then, if you don't want it to print anything, you need to make sure that "you don't have $iptc data" is true. Thoughts on how you can make that happen?

Link to comment
Share on other sites

12 hours ago, requinix said:

You already have something that will print information if it's available. What's more, it makes sense: if you have $iptc data then print what's in it. Logical. Obviously, that then means that if you don't have $iptc data then it won't print. Also logical.

So then, if you don't want it to print anything, you need to make sure that "you don't have $iptc data" is true. Thoughts on how you can make that happen?

Ahh I figured it out with an 'if else' statement :) Just wasn't sure how to phrase it correctly, but it's working now.

 

 

<?php
$files = glob("*.*");

    for ($i=0; $i<count($files); $i++) {
        $image = $files[$i];

        echo '<img src="'.$image .'" title="'.$image .'" alt="'.$image .'" width="5%"/>'."<br />";

            $picinfo = array();
            getimagesize($image, $picinfo);

            if(isset($picinfo['APP13']))
            {
            $iptc = iptcparse($picinfo["APP13"]);
            }

            if (empty($picinfo["APP13"])) {
            $description = "";
            } else {
            $description = $iptc['2#105'][0]; /* IPTC 'Header' metadata */
            print_r($description);
            print "<br />";
            }
            
            print $image ."<br /><br />"; /* Print filename */
    }
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.