We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 19419
    • 4 Posts
    Hi,
    I’ve the following snippets. They get the six most recent files in a directory and create a mini image gallery. The work fine as functions out of modX (with the necesary modifications), but not inside. Can anybody help me?.

    -----[[gallery]]-----
    <?php<br />$path = $modx->config[’base_url’] . ’assets/templates/tmplts/images’;<br />$params[’folder’] = $path;<br />$files = array();<br />$files = $modx->runSnippet(’lastfiles’, $params);<br />echo $path;<br /><br />for ($i=0;$i<=5;$i++) {<br />      $numimg= $i+1;<br />      echo ’<img id="imgBlueScoop’. $numimg .’" alt=""<br />                 src="’ . $files[$i] .’"<br />                 style="width: 75px; height: 75px;"<br />                 class="PopBoxImageSmall"<br />                 pbShowPopText=false<br />                 pbShowPopImage=false<br />                 pbShowPopBar=false<br />                 pbShowRevertBar=false<br />                 pbShowRevertImage=false<br />                 pbShowRevertText=false<br />                 onclick="Pop(this,50,\’PopBoxImageLarge\’);"><br />      ’;<br />}<br />return ’’;

    -----[[end of gallery]]-----


    -----[[lastfiles]]-----

    <?php<br />$numimages = 6;<br />$images = array();<br />foreach (scandir($folder) as $node) {<br />    $nodePath = $folder . ’/’ . $node;<br />    if (!is_dir($nodePath) && strcmp($node,"base.png")!=0){<br />        $images[$nodePath] = filemtime($nodePath);<br />    }<br />}<br />arsort($images);<br />$i=0;<br />$newest = array();<br />foreach ($images as $key => $val) {<br />print($key);<br />    if ($i <= $numimages) {<br />       $newest[$i] = $key;<br />       $i++;<br />    } <br />}<br />return $newest;

    -----[[end of lastfiles]]-----
      • 3749
      • 24,544 Posts
      Do you have this in your template so the image paths will be correct?

      <base href="[[!++site_url]]" />


      FWIW, you should change:

      echo x;
      

      to

      $output .= x;
      // etc.
      return $output;


      though I don’t think it relates to your problem.
        Did I help you? Buy me a beer
        Get my Book: MODX:The Official Guide
        MODX info for everyone: http://bobsguides.com/modx.html
        My MODX Extras
        Bob's Guides is now hosted at A2 MODX Hosting
        • 19419
        • 4 Posts
        <base href="[[!++site_url]]" />


        is a new adition, I don’t need it. I’ve change the original and more elegant code for this one trying to make it run, without success.

        Running my code I get references to six images. The contents of the array in Gallery snippet is

        $files = {[0]=>"A", [1]=>"r", [2]=>"r", [3]=>"a", [4]=>"y", [5]>""}