Jump to content

Newer to php, if image filepath is null dont print?


pengie

Recommended Posts

Ok, so, I've been using Drupal for quite some time now and am totally comfortable editing themes for nodes and views and so forth. But now the time has come where I actually need to write something to make my images display properly.

 

So, I have a main image, and then some have additional images, and some dont.

 

what I have so far is very different from what I know it should be, just dont know how to get from point A, to point B.

<?php foreach($field_image1 as $key => $value): ?>
<div id="additional_images"><img src="http://www.website.com/test/<?php print $field_image1['filepath'];  ?>"/>
        </div>
        <?php endforeach; ?>

 

So, I used for each because there is (from my understanding) an array, (field_image1[0], field_image1[1], field_image1[2]...)

All of these have their unique filepaths. But some of my nodes dont have additional images at all which is why I'm trying to use php to control if they are printed or not to avoid getting the broken image box.

 

So basically, if field_image's filepath is null, dont print anything, else print file path for each.

HELP!

 

If this didn't make sense, I can retry.

 

 

You just need to add a simple check. Do you actually mean null or just a blank value?

 

Try this

 

<?php
   foreach($field_image1 as $key => $value):
        if(strlen($value)>0): // check if there are any characters in the filepath variable
?>
   <div id="additional_images"><img src="http://www.killerkini.com/test/<?php print $value;  ?>"/></div>
<?php
        endif;
    endforeach;
?>

ok, i mean blank value.

 

still doesn't work. I think I hurt myself more by putting code in there.

 

Ok, I'll put in the stuff I can't figure out.

 

In the foreach statement, ($field_image1 as $key => $value) I don't think that is right, because I don't really know what it means. I am trying to target the filepath value of each item in the field_image1 array.

 

Then this bit, <?php print $field_image1['filepath'];  ?> for it to work, it seems there needs to be a number in there such as <?php print $field_image1[0]['filepath'];  ?> or <?php print $field_image1[1]['filepath'];  ?> But I don't want to have to put the specific Item that I am targeting which is why I am using for each.

 

Ideas....?

dont know if this helps at all... but here is the output.

 

[field_image1] => Array

        (

            [0] => Array

                (

                    [fid] => 7

                   

  • => 1
                        [data] => Array
                            (
                                [description] =>
                                [alt] =>
                                [title] =>
                            )
     
                        [uid] => 1
                        [filename] => bikini_2b.jpg
                        [filepath] => sites/default/files/bikini_2b.jpg
                        [filemime] => image/jpeg
                        [filesize] => 43645
                        [status] => 1
                        [timestamp] => 1270076083
                        [nid] => 6
                        [view] =>
                    )
     
                [1] => Array
                    (
                        [fid] => 8
                       
    • => 1
                          [data] => Array
                              (
                                  [description] =>
                                  [alt] =>
                                  [title] =>
                              )
       
                          [uid] => 1
                          [filename] => bikini_2c.jpg
                          [filepath] => sites/default/files/bikini_2c.jpg
                          [filemime] => image/jpeg
                          [filesize] => 50540
                          [status] => 1
                          [timestamp] => 1270078191
                          [nid] => 6
                          [view] =>
                      )
       
                  [2] => Array
                      (
                          [view] =>
                      )
       
                  [3] => Array
                      (
                          [view] =>
                      )
       
              )

Archived

This topic is now archived and is closed to further replies.

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