开发者

PHP: Using opendir to populate JS slideshow

I guess I'm having trouble mushing the scripts together. I can print the file names of the content in the folder, no problem. I've got the slideshow working when img urls are listed directed into the div. NOW I need to get the file name, and echo a .... somehow.

          <script type="text/javascript">

      function slideSwitch() {
          var $active = $('#slideshow DIV.active');

          if ( $active.length == 0 ) $active = $('#slideshow DIV:last');

          // use this to pull the divs in the order they appear in the markup
          var $next =  $active.next().length ? $active.next()
              : $('#slideshow DIV:first');

          // uncomment below to pull the divs randomly
          // var $sibs  = $active.siblings();
          // var rndNum = Math.floor(Math.random() * $sibs.length );
          // var $next  = $( $sibs[ rndNum ] );


          $active.addClass('last-active');

          $next.css({opacit开发者_Go百科y: 0.0})
              .addClass('active')
              .animate({opacity: 1.0}, 1000, function() {
                  $active.removeClass('active last-active');
              });
      }

      $(function() {
          setInterval( "slideSwitch()", 5000 );
      });

      </script>


       <div id="slideshow">
       <?php
      $dir = "images/featured/";
      $dh = opendir($dir);
      while (($file = readdir($dh)) !== false) {
      echo "";
      }
      closedir($dh);
      ?>
      </div>


It looks like you're confusing your JS and PHP as far as which does what. PHP is server-side while JS is client side (for your case). Once the content reaches the browser, PHP's job is done and JS's begins.

Your PHP code outputs nothing at all. Here's how it's being executed

  1. You open the directory
  2. Read the filenames one at a time
  3. Echo an empty string

If you want to pass data from PHP to JS, you need to do something like

var phpData = <?php echo $dataIWantToAccessInJs; ?>;

Finally, you don't need to prefix your regular JS variables with the $ sign. It is required in PHP though, but keeping the $ for PHP (and jQuery objects) will cut down on the aforementioned confusion between JS and PHP.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜