Jump to:

23373 Posts in 18175 Topics by 2865 members

General Questions

SilverStripe Forums » General Questions » Display Images of a Directory using PHP - Editing Page.php

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1
Go to End
Author Topic: 8773 Views
  • Wezzlee
    Avatar
    Community Member
    28 Posts

    Display Images of a Directory using PHP - Editing Page.php Link to this post

    Hi guys,

    I am working on a website that has images displayed on each page. The images are page related - So www.example.com/elephants
    should display only images of elephants.

    I thought I could simply solve this by creating a image directory named "Elephants" and add $ThemeDir/images/$URLSegment/image5.jpg to page.ss. This works fine, but it does not work when you have only 3 images for another directory. In that case 5 thumbs will displayed when you only have 3 images in the directory.

    So now I was thinking of using PHP using the following code:

    function ShowImages()
       {
       
       // Open a known directory, and proceed to read its contents
       $files = glob("images/icons/*.*");
       for ($i=1; $i<count($files); $i++)
       {
          $num = $files[$i];
          echo '<a href="$ThemeDir/images/photos/$URLSegment/image5.jpg" rel="prettyPhoto[pp_gal]" title="$Title"><img src="'.$num.'" alt="random image" border="0" width="48" height="48" alt="Elephants"></a>'."&nbsp;&nbsp;";
          }
       
       }

    This shows exactly all the images of a certain directory. But somehow I cannot use the dynamic Silverstripe values like $ThemeDir, $URLSegment etc. What am I missing? This might be a dumb question, but I am not really into PHP and I really trying hard to understand it, so if one of you could answer I would be very grateful.

    Or if you guys know another workaround that would also be great.

    (see current project here: http://www.wezzbite.nl/Silverstripe/zwangerschapsyoga/) images right side.

    Thanks.
       

  • Willr
    Avatar
    Forum Moderator
    5489 Posts

    Re: Display Images of a Directory using PHP - Editing Page.php Link to this post

    First off, you'll get 10 lashings for mixing html into php and using echo!

    You should get the data you want and pass it back to the template.

    function ShowImages() {
    $icons = new DataObjectSet();
    $files = glob("images/icons/*.*");
    for ($i=1; $i<count($files); $i++)
    {
    $icons->push(new ArrayData(array(
    'File' => $files[$i]
    )));
    }

    return $icons;
    }

    Then in your template output the HTML by using a <% control %> statement over your function

    <% control ShowImages %>
    <a href="$ThemeDir/images/photos/$URLSegment/image5.jpg" rel="prettyPhoto[pp_gal]" title="$Title">
    <img src="'$File'" alt="random image" border="0" width="48" height="48" alt="Elephants">
    </a>'."&nbsp;&nbsp;";
    <% end_control %>

  • Wezzlee
    Avatar
    Community Member
    28 Posts

    Re: Display Images of a Directory using PHP - Editing Page.php Link to this post

    Thank you very much!

    I have just tested this and the following error appeared:

    Fatal error: Call to a member function push() on a non-object in /Applications/MAMP/htdocs/vanleeuwen/mysite/code/Page.php on line 46

    What Am i doing wrong?

    And I promise when your PHP will work I will never mixing html into php using echo ;)

  • SheaDawson
    Avatar
    Community Member
    45 Posts

    Re: Display Images of a Directory using PHP - Editing Page.php Link to this post

    In function ShowImages() - Try $icons->push instead of $files->push

  • Willr
    Avatar
    Forum Moderator
    5489 Posts
  • Wezzlee
    Avatar
    Community Member
    28 Posts

    Re: Display Images of a Directory using PHP - Editing Page.php Link to this post

    Guys thank you very much! It works now, but somehow I cannot change the directory or use $URLSegment, $Title in the HTML.

    Now the HTML output for :

    <% control ShowImages %>
        <a href="$ThemeDir/$File" rel="prettyPhoto[pp_gal]" title="$Title">
    <img src="$ThemeDir/$File" border="0" width="48" height="48" alt="PHP test">
    </a>
       <% end_control %>

    Is:

    <a href="themes/blackcandy/images/icons/icon-jpg.gif" rel="prettyPhoto[pp_gal]" title="">
    <img src="themes/blackcandy/images/icons/icon-jpg.gif" border="0" width="48" height="48" alt="van Leeuwen PHP test">
    </a>

    but should be:

    <a href="$ThemeDir/images/photos/$URLSegment/image1.jpg" rel="prettyPhoto[pp_gal]" title="$Title">
    <img src="$ThemeDir/images/photos/$URLSegment/thumb_image1.jpg" border="0" width="48" height="48" alt="$Title" />
    </a>

    $Themes = themes/blackcandy
    $URLSegment = http://www.wezzbite.nl/Silverstripe/zwangerschapsyoga/
    $Title = Header title

    I thought I could simply alter the directory of $files = glob("images/icons/*.*"); but somehow other directories are not recognized.

    8773 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.