21309 Posts in 5738 Topics by 2603 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, Howard, Sean, Ryan M., biapar, Willr, Ingo, swaiba, simon_w
|
Page:
1
|
Go to End | |
| Author | Topic: | 3067 Views |
-
Display Images of a Directory using PHP - Editing Page.php

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>'." ";
}
}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.
-
Re: Display Images of a Directory using PHP - Editing Page.php

15 April 2012 at 9:07pm Last edited: 16 April 2012 5:55pm
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>'." ";
<% end_control %> -
Re: Display Images of a Directory using PHP - Editing Page.php

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 ;)
-
Re: Display Images of a Directory using PHP - Editing Page.php

16 April 2012 at 12:08pm
In function ShowImages() - Try $icons->push instead of $files->push
-
Re: Display Images of a Directory using PHP - Editing Page.php

16 April 2012 at 5:56pm
Oh yes, sorry!
-
Re: Display Images of a Directory using PHP - Editing Page.php

17 April 2012 at 6:54am Last edited: 17 April 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 titleI thought I could simply alter the directory of $files = glob("images/icons/*.*"); but somehow other directories are not recognized.
| 3067 Views | ||
|
Page:
1
|
Go to Top |



