Jump to content

Help to add "where" to php code to return images


Colink

Recommended Posts

Hi

Firstly I am a "copy and paste" coder, not a real coder.

The following code is on an AMP HTML page. It displays all images from a specific folder in an AMP carousel.

I know the AMP part is not relevant but I copy all of the code (below) from his section of the page to show context.

What currently happens:

All images in folder "images/image4" are displayed in the Carousel

What I would like to happen:

Return all images WHERE file name starts with "thisword" eg 

thisword1.jpg

thisword2.jpg

Ignore 

thisotherword1.jpg

Any suggestions would be appreciated.

ColinK

 

Existing code

    <amp-carousel class="xs-12 bannerimg4 " width="16" height="9"  layout="responsive" type="slides" controls loop autoplay delay="3000">
                    <?php
                $dir = "images/image4";
                $handle = opendir(dirname(realpath(__FILE__)).'/images/image4');
                        while($file = readdir($handle)){
                            if($file !== '.' && $file !== '..'){
                              //  echo '<img src="images/banner-1040/'.$file.'" border="0" />';
                                echo '<amp-img src="images/image4/'.$file.'" layout="responsive" class="cover"  width="16" height="9" alt="images/banner-1040/'.$file.'"></amp-img>';

                            }
                        }
                ?>
                </amp-carousel>

 

Link to comment
Share on other sites

Thanks for your prompt response. Unfortunately my "copy and paste" coding skills were not good enough for this.

All images in the folder still display not just those with file names that start with "thisword"

This is what I tried (old $dir line commented out, modified version below in bold)

<amp-carousel class="xs-12 bannerimg4 " width="16" height="9"  layout="responsive" type="slides" controls loop autoplay delay="3000">
                    <?php
                //  OLD directory code                 $dir = "images/image4";
                $images = glob($dir."/images/image4/thisword*.*");
                $handle = opendir(dirname(realpath(__FILE__)).'/images/image4');
                        while($file = readdir($handle)){
                            if($file !== '.' && $file !== '..'){
                              //  echo '<img src="images/banner-1040/'.$file.'" border="0" />';
                                echo '<amp-img src="images/image4/'.$file.'" layout="responsive" class="cover"  width="16" height="9" alt="images/banner-1040/'.$file.'"></amp-img>';

                            }
                        }
                ?>
                </amp-carousel>

I did try adding thisword*.* to the handle line, but this hides all images

Any further help would be appreciated.

Colin K 

Link to comment
Share on other sites

  • 2 weeks later...

First of all a thank you to all those who replied, the experience is much more friendly than the usual stack overflow attitude - "If you have not 90% solved your issue and if you do not have expertise "DON'T ASK""

I am still not much further on.

eg I cannot see how this code (from the link @barand gave) can be modified to work with his suggestion of "$images = glob($dir."/thisword*.*");" which @barand gave or how to include code for an array as suggested by @dalecosp and make it all work for my AMP carousel.

for each (glob("*.txt") as $filename) {
    echo 
"$filename size " filesize($filename) . "\n";

=====

I really am just a copy and paste coder so need something closer to my needs to edit.

I do understand the philosophy of learn vs being given the answer but the "clues" have not been sufficient for my skills.

If further guidance is not available, thanks again for the answers thus far.

ColinK

Link to comment
Share on other sites

I"m not sure why all the uses of path names in vars and in the glob call but I think this (with a little thought) should work:

	$dir = "something here ending with a \ ";
	$mask = "thisword*.*";
	$images = glob ($dir. $mask);
	if (count($images) == 0)
	   echo "No images matching $dir.$mask";
	else
	{
	   // example lifted from the Manual under Glob()
	  foreach ($images as $filename) 
	  {
           echo "$filename size: " . filesize($filename) . "\n";
        }
	}
	
Edited by ginerjm
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.