Welcome Guest, Not a member yet? Register   Sign In
organizing folders and images pulled from scandir()
#1

[eluser]brian88[/eluser]
I have a function that gets all the folders and files inside of those folders.
Now I want to display them as:
Category Name
- file
- file
- file
Category Name
- file
- file
etc..

controller
Code:
public function index() {
  // get categories
   $folder = FCPATH . "assets/images/clipart/";
  $data['files'] = $this->main_mod->getFoldersAndTheirFiles($folder);

  print_r($data['files']);

  /* print_r() returns
  Array (
  [0] => animals
  [1] => birthdays
  [2] => sports
  [3] => bike.jpg
  [4] => badger.gif
  [5] => bear.gif
  [6] => bee.gif
  [7] => tiger.gif )
  
  that function can also return the whole filepath. but its ugly, so i took off the begining of it.
  Array (
  [0] => C:\Program Files (x86)\wamp\www\clipart\assets/images/clipart//animals
  [1] => C:\Program Files (x86)\wamp\www\clipart\assets/images/clipart//birthdays
  [2] => C:\Program Files (x86)\wamp\www\clipart\assets/images/clipart//sports
  [3] => C:\Program Files (x86)\wamp\www\clipart\assets/images/clipart//sports/bike.jpg
  [4] => C:\Program Files (x86)\wamp\www\clipart\assets/images/clipart//birthdays/badger.gif
  [5] => C:\Program Files (x86)\wamp\www\clipart\assets/images/clipart//birthdays/bear.gif
  [6] => C:\Program Files (x86)\wamp\www\clipart\assets/images/clipart//birthdays/bee.gif
  [7] => C:\Program Files (x86)\wamp\www\clipart\assets/images/clipart//birthdays/tiger.gif )

  */

  // views
  $this->load->view('header_view', $data);
  $this->load->view('main_view', $data);
  $this->load->view('footer_view');
} // end function

model - this is a function i found on the internet
Code:
function getFoldersAndTheirFiles($dir) {
$path = '';
$stack[] = $dir;
while ($stack) {
    $thisdir = array_pop($stack);
    if ($dircont = scandir($thisdir)) {
        $i=0;
        while (isset($dircont[$i])) {
            if ($dircont[$i] !== '.' && $dircont[$i] !== '..') {
                $current_file = "{$thisdir}/{$dircont[$i]}";
                if (is_file($current_file)) {
                    $path[] = "{$thisdir}{$dircont[$i]}"; // i removed {$thisdir} to just get "bear.gif" instead of the full file path
                } elseif (is_dir($current_file)) {
                     $path[] = "{$thisdir}{$dircont[$i]}"; // i removed {$thisdir} here too
                    $stack[] = $current_file;
                }
            }
            $i++;
        }
    }
}
return $path;
}

view
Code:
<ul id="categories">
   &lt;?php foreach($files as $cat): ?&gt;

      // if its a directory then show it here
    &lt;?php if( is_dir($cat) ): ?&gt;
     <li><a href="&lt;?php echo base_url('assets/images/clipart' . $cat); ?&gt;">&lt;?php echo $cat; ?&gt;</a></li>

       // returns no output, does not recognize it is a directory. but it will if I use the full file path. But thats a bad link.
    &lt;?php endif; ?&gt;

       //if its a file then show it under its category
    &lt;?php if( is_file($cat) ): ?&gt;

     <li><a href=""><img src="&lt;?php echo base_url('assets/images/clipart' . $cat); ?&gt;" /><span>&lt;?php echo $cat;  ?&gt;</span></a></li>

       // returns no output, does not recognize it is a file. but it will if I use the full file path. But thats a bad link.
    &lt;?php endif; ?&gt;
    
   &lt;?php endforeach; ?&gt;
</ul>
#2

[eluser]brian88[/eluser]
is there a way to do this without a database? I need to link files to a specific folder.




Theme © iAndrew 2016 - Forum software by © MyBB