We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 26418
    • 20 Posts
    I created a new snippet I call rotatead1. The PHP function works great outside of modx, but when I put the code in a snippet I get an error.

    PHP error debug 
      Error:  opendir('c:/wamp/www/modx/assets/banners/') [function.opendir]: failed to open dir: [b]No such file or directory[/b]   
      Error type/ Nr.:  Warning - 2   
      File:  C:\wamp\www\MODx\assets\snippets\rotate\readfiles.php   
      Line:  5   
      Line 5 source:  $dir = opendir($banpath);  
    


    SNIPPET
    ---- 
    if(!isset($banpath) || $banpath == '') { return; }
    $RotatePath = $modx->config['base_path'] . 'assets/snippets/rotate/';
    include_once($RotatePath.'readfiles.php');
    $modx->setPlaceholder('banner',$imageoutput);
    $imageoutput = rotate($banpath); 
    return $imageoutput;
    ----
    


    NOTE - I used &placeholder=’1’ &output=’0’ when I tried the placeholde method.

      Scott
      MODx 1.0.2 Rev 6066 // Apache version 2.2.14 (Unix) // PHP version 5.2.11 // MySQL version 5.1.42 // Architecture x86_64 // Operating system: Linux
      • 3749
      • 24,544 Posts
      Does the directory ’c:/wamp/www/modx/assets/banners/’ exist?

      If so, opendir() has some problems on Windows machines:

      http://us2.php.net/function.opendir

      BTW, it appears that you’re setting a placeholder with a variable that hasn’t been assigned yet:

      $modx->setPlaceholder('banner',$imageoutput);
      $imageoutput = rotate($banpath); 
        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
        • 26418
        • 20 Posts
        BobRay,

        Thanks for your help. The snippet seems to be working excpet the image will not display.
        When I view the source of the returned page the place holder is outside of my html and there are extra
        that I did not add.

        Section of my page where I put the snippet code

        <div id="banright" >
        [!rotateads1? &placeholders='1' &output='0' &bannerdir=assets/banners/!]
        <a href="http://www.howtodoc.com"><img class="banrightimg" border="0" src="c:/wamp/www/modx/assets/banners/[+rotateads1.banner+]"/></a>				
        </div>
        


        SNIPPET CODE  (ROTATE() randomly grabs one of the image files)
        $imageoutput=' ';
        $retbanner=' ';
        $bannerdir = (isset($bannerdir))? $bannerdir:'assets/banner/';
        $modx->setPlaceholder('banner',$imageoutput);
        $bannerpath = $modx->config['base_path'] . $bannerdir;
        $rotatepath = $modx->config['base_path'] . 'assets/snippets/rotate/';
        include_once($rotatepath.'readfiles.php');
        $imageoutput = rotate($bannerpath); 
        return $imageoutput;
        
        

        OUTPUT:  MODX returned the OUTPUT and placed it above the HTML line
        <div id="banright" >
        	<br/> sample_ad_003.bmp
        	<a href="http://www.howtodoc.com"><img class="banrightimg" border="0" src="c:/wamp/www/modx/assets/banners/ "/></a>				
        </div>
        

          Scott
          MODx 1.0.2 Rev 6066 // Apache version 2.2.14 (Unix) // PHP version 5.2.11 // MySQL version 5.1.42 // Architecture x86_64 // Operating system: Linux
          • 4041
          • 788 Posts
          Give this setup a try:

          Section of the page where you put the snippet code, note the removal of the snippet name from the placeholder, it has to match what you set as the placeholder name in the snippet, in this case [+banner+].

          <div id="banright" >
          [!rotateads1!]
          <a href="http://www.howtodoc.com"><img class="banrightimg" border="0" src="c:/wamp/www/modx/assets/banners/[+banner+]"/></a>            
          </div>


          SNIPPET CODE (ROTATE() randomly grabs one of the image files)
          <?php
          $imageoutput='';
          $retbanner='';
          $bannerdir = (isset($bannerdir))? $bannerdir : 'assets/banner/';
          // move this down to the bottom since this is the actual value you need to produce the image name
          //$modx->setPlaceholder('banner',$imageoutput);
          $bannerpath = $modx->config['base_path'] . $bannerdir;
          $rotatepath = $modx->config['base_path'] . 'assets/snippets/rotate/';
          include_once($rotatepath.'readfiles.php');
          $imageoutput = rotate($bannerpath);
          
          $modx->setPlaceholder('banner',$imageoutput);  // usage: [+banner+]
          //return $imageoutput;
          ?>


          Hope this helps smiley
            xforum
            http://frsbuilders.net (under construction) forum for evolution
            • 36592
            • 970 Posts
            Use backticks (`) instead of single quote (’) for the snippet parameter.
            [!rotateads1? &placeholders='1' &output='0' &bannerdir=assets/banners/!]

            http://wiki.modxcms.com/index.php/Snippet_call_anatomy
              • 26418
              • 20 Posts
              BobRay,Breezer,
              Many thanks. smiley I think I’m closer to a resolution.

              tkfm,
              thanks for reminding me about backticks. smiley



              Section of my Template where I call my Snippet
              
              <div id="banright" >
              [!rotateads1? &placeholders=`1` &output=`0` &bannerdir=`assets/banners`!]
              <a href="http://www.howtodoc.com"><img class="banrightimg" border="0" src="c:/wamp/www/modx/assets/banners/[+banner+]"/></a>
              </div>
              



              The SNIPPET:  rotate() returns a random file form the banner directory.
              
              $imageoutput=' ';
              $bannerpath = $modx->config['base_path'] . 'assets/banners/';
              $rotatepath = $modx->config['base_path'] . 'assets/snippets/rotate/';
              include_once($rotatepath.'readfiles.php');
              $imageoutput = rotate($bannerpath);
              $modx->setPlaceholder('banner',$imageoutput);
              



              PAGE SOURCE --- OUTPUT from MODX
              
              
              <div id="banright">
              <br/> 
              <a href="http://www.howtodoc.com"><img class="banrightimg" border="0" src="c:/wamp/www/modx/assets/banners/sample_ad_003.bmp"/></a>
              </div>
              


              My link <a href..> above looks correct now. smiley But the image still is not showing.

                Scott
                MODx 1.0.2 Rev 6066 // Apache version 2.2.14 (Unix) // PHP version 5.2.11 // MySQL version 5.1.42 // Architecture x86_64 // Operating system: Linux
                • 36592
                • 970 Posts
                This part
                src="c:/wamp/www/modx/assets/banners/[+banner+]"

                might be...
                src="[(base_url)]assets/banners/[+banner+]"

                  • 26418
                  • 20 Posts
                  tkfm,

                  BINGO!!!!!! That’s the finaly piece. My banner is rotating. w0ot! Yes! smiley

                  src="[(base_url)]assets/banners/[+banner+]"


                  Many Thanks to all for all your help.

                  :)
                    Scott
                    MODx 1.0.2 Rev 6066 // Apache version 2.2.14 (Unix) // PHP version 5.2.11 // MySQL version 5.1.42 // Architecture x86_64 // Operating system: Linux
                    • 3722
                    • 171 Posts
                    It looks like this rotate banners when the page refreshes.
                    Can this be setup to rotate the banner at a set time?