Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We're retiring the forums!

The SilverStripe forums have passed their heyday. They'll stick around, but will be read only. We'd encourage you to get involved in the community via the following channels instead:

General Questions /

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

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

Display Images of a Directory using PHP - Editing Page.php


Go to End


6 Posts   16754 Views

Avatar
Wezzlee

Community Member, 28 Posts

15 April 2012 at 8:15pm

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.

Avatar
Willr

Forum Moderator, 5523 Posts

15 April 2012 at 9:07pm

Edited: 16/04/2012 5:55pm

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

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 %>

Avatar
Wezzlee

Community Member, 28 Posts

15 April 2012 at 9:33pm

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 ;)

Avatar
sheadawson

Community Member, 49 Posts

16 April 2012 at 12:08pm

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

Avatar
Willr

Forum Moderator, 5523 Posts

16 April 2012 at 5:56pm

Oh yes, sorry!

Avatar
Wezzlee

Community Member, 28 Posts

17 April 2012 at 6:54am

Edited: 17/04/2012 6:54am

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.