We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 7253
    • 97 Posts
    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 smiley

    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?
      • 7253
      • 97 Posts
      I’ve been assuming that my problem is something to do with my code, but here’s my setup, just in case:

      MODx 0.9.5, default install, nothing added or removed, except my code above and a couple of templates and pages.
      Apache/2.2.3 (Win32)
      PHP Version 5.2.0
      Windows Xp Pro

      Everything else in MODx seems to work fine - the manager, templates, creating pages etc... it’s just this snippet.

      Help me, MODx community, you’re my only hope...