Advertisement
Guest User

Untitled

a guest
Jul 28th, 2014
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.23 KB | None | 0 0
  1. <?php
  2.  
  3.     class Banner {
  4.        
  5.         // Set variables
  6.         public $directory;
  7.         protected $defaultPage = "index";
  8.  
  9.         public function checkDirectory($page) {
  10.             // Set directory & check it exists
  11.             $this->directory = 'assets/img/transitions/'.$page.'/';
  12.             if(!is_dir($this->directory)) {
  13.                 // If it doesn't, set default directory
  14.                 $this->directory = 'assets/img/transitions/'.$this->defaultPage.'/';
  15.             } return true;
  16.  
  17.         }
  18.  
  19.         public function pullImages($page) {
  20.             // Open the directory
  21.             if($handle = opendir($this->directory)) {
  22.                 $images = array();
  23.  
  24.                 while(false !== ($entry = readdir($handle))) {
  25.                     if($entry != "." && $entry != "..") {
  26.  
  27.                         // Insert image into array
  28.                         $images[] = '/'.$this->directory.$entry;
  29.                     }
  30.                 }
  31.  
  32.                 // Close directory
  33.                 closedir($handle);
  34.  
  35.                 // Sort images & return array
  36.                 sort($images);
  37.                 return $images;
  38.             }
  39.         }
  40.  
  41.         public function display($page) {
  42.             // Check that page variable is being sent through
  43.             if(isset($page)) {
  44.  
  45.                 // Check the directory exists
  46.                 if($this->checkDirectory($page)) {
  47.  
  48.                     // Pull images from folder
  49.                     $images = $this->pullImages($page);
  50.  
  51.                     // Set Counter & HTML Return
  52.                     $counter = 0;
  53.                     $html = "";
  54.  
  55.                     // Loop throuh images
  56.                     foreach($images as $image) {
  57.                         $counter++;
  58.  
  59.                         // Add them into HTML Return
  60.                         $html .= '<img src="'.$image.'" class="banner__slide slide--'.$counter.'" />';
  61.                     }
  62.  
  63.                     if(!empty($html)) {
  64.                         return $html;
  65.                     }
  66.                 }
  67.             }
  68.         }
  69.  
  70.     }
  71.  
  72.     // Initialize Banner
  73.     $banner = new Banner;
  74.  
  75. ?>
  76.  
  77. <div class="inner-slider">
  78.     <?= $banner->display($page); ?>
  79. </div>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement