Hello!
I’m quite new to MODX, but loving it so far. I’m having trouble getting my snippet to work though and I was hoping someone could point me in the right direction
This is my snippet code:
<?php
include_once($modx->config['base_path'].'assets/snippets/logic/random_image_loader.php');
$ril = new ril_RandomImageLoader;
return $ril->ril_GetRandomImages($image_folder, 5);
//echo '<h1>Hello!</h1>';
?>
and this is the snippet call:
<td>
[[RandomImageLoader? &image_folder=`./frontpage`]]
</td>
and this is the contents of the ’random_image_loader.php’ file:
<?php
/*
Loads random images from a folder.
Returns the text containing image tags with the images in.
$ril_folder - folder to pull image from
$image_count - number of images to return
*/
class ril_RandomImageLoader {
var $ril_folder = '';
function ril_GetRandomImages ($folder, $image_count = 1) {
$tmp = '';
$this->ril_folder = $folder;
$ril_images = array();
$ril_dir = opendir($this->ril_folder);
while ($ril_file = readdir($ril_dir)) {
if (($ril_file != '.') && ($ril_file != '..') && !(is_dir($this->ril_folder.'/'.$ril_file))
&& (strtolower($ril_file) != 'pspbrwse.jbf') && (strtolower($ril_file) != 'thumbs.db')
&& ((strrchr($ril_file, '.') == '.jpg') || (strrchr($ril_file, '.') == '.jpeg'))
)
{
// Add background image to $backgrounds array
$ril_images[] = $this->ril_folder.'/'.$ril_file;
}
}
// Found some images. Randomly pick $image_count images to return.
// If $image_count is greater than the number of images in the folder, reduce it.
if ($image_count > count($ril_images)) {
$image_count = count($ril_images);
}
// randomise the image list
shuffle($ril_images);
// return the first $image_count images
for ($count = 0; $count < $image_count; $count++) {
$tmp .= '<img src="' . $ril_images[$count] . '" />' . "\n";
}
//return $tmp;
return '<h1>Returned from ril_GetRandomImages</h1>';
}
}
?>
This class works fine if tested with another PHP page, outside of MODX, it’s just that the page that uses the template which includes the snippet call, doesn’t work. I get this error:
[tt]
Fatal error: Out of memory (allocated 1145569280) (tried to allocate 227577219 bytes) in C:\Documents and Settings\Dunc\My Documents\Web stuff\modx\logic\manager\includes\document.parser.class.inc.php on line 1296
[/tt]
If I comment out the [tt]return[/tt] line in the snippet and uncomment the [tt]echo[/tt] line, the page works and prints ’Hello!’, as I would expect.
I’ve searched the forums and google and racked my brains, but I can’t see where the problem is. I know I should be using placeholders and chunks and whatnot for snippet output, but I’m just trying to get the basic snippet to work first.
I’m sure it’s something simple - can anyone help?