SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Evangelist Scott.Botkins's Avatar
    Join Date
    Dec 2004
    Posts
    598
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Show Random Folder?

    I'm developing something, I'm wanting to show a random folder name that is in that directory. I don't want it to list the file names, it just needs to show 1 random folder name. The code below lists the actual folders but I need itto list 1 and change to a different one when the page refreshes. How could I go about doing this?

    PHP Code:
    <?php
    function folderlist(){
     
    $startdir './';
     
    $ignoredDirectory[] = '.'
     
    $ignoredDirectory[] = '..';
      if (
    is_dir($startdir)){
          if (
    $dh opendir($startdir)){
              while ((
    $folder readdir($dh)) !== false){
                  if (!(
    array_search($folder,$ignoredDirectory) > -1)){
                    if (
    filetype($startdir $folder) == "dir"){
                          
    $directorylist[$startdir $folder]['name'] = $folder;
                          
    $directorylist[$startdir $folder]['path'] = $startdir;
                      }
                  }
              }
              
    closedir($dh);
          }
      }
    return(
    $directorylist);
    }

    $folders folderlist();
     foreach (
    $folders as $folder){
       
    $path $folder['path'];
       
    $name $folder['name'];

       echo 
    '' $name '<br />';
     }
    ?>
    Thanks!

  2. #2
    SitePoint Wizard silver trophy
    Join Date
    Mar 2006
    Posts
    6,132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:

    session_start
    ();

    if (empty(
    $_SESSION['folders'])) {
        
    $_SESSION['folders'] = folderlist();
        
    shuffle($_SESSION['folders']);
    }

    $folder array_pop($_SESSION['folders']);
    print_r($folder); 

  3. #3
    SitePoint Evangelist Scott.Botkins's Avatar
    Join Date
    Dec 2004
    Posts
    598
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks but I get a error using that code. Fatal error: Call to undefined function: folderlist()

    Am I suppose to add that to my php coding I listed above or show yours only? I have it showing yours only.

    PHP Code:
    <?php
    if (empty($_SESSION['folders'])) { 
        
    $_SESSION['folders'] = folderlist(); 
        
    shuffle($_SESSION['folders']); 


    echo 
    array_pop($_SESSION['folders']); 

    ?>

  4. #4
    Sell crazy someplace else markl999's Avatar
    Join Date
    Aug 2003
    Location
    Manchester, UK
    Posts
    4,007
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you are using PHP >= 4.3.3 you could do:
    PHP Code:
    session_start();
    if(empty(
    $_SESSION['folder'])) {
      
    $_SESSION['folder'] = '';
    } else {
      
    $folder $_SESSION['folder'];
    }
    $startdir '';
    $folders glob($startdir.'*.*'GLOB_ONLYDIR);
    //make sure we show a different folder than the last one
    while($folder == $_SESSION['folder']) {
      
    $_SESSION['folder'] = $folders[array_rand($folders1)];
    }
    echo 
    $_SESSION['folder']; 

  5. #5
    SitePoint Evangelist Scott.Botkins's Avatar
    Join Date
    Dec 2004
    Posts
    598
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Mark yours gave me a long error about being between 1 and the array

  6. #6
    SitePoint Wizard silver trophy
    Join Date
    Mar 2006
    Posts
    6,132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by shockbotkins

    Am I suppose to add that to my php coding I listed above or show yours only? I have it showing yours only.

    yes, you must define a function before you can use it.

  7. #7
    SitePoint Evangelist Scott.Botkins's Avatar
    Join Date
    Dec 2004
    Posts
    598
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by clamcrusher
    yes, you must define a function before you can use it.
    Alright its working partly now.

    I have

    PHP Code:
    <?php
    function folderlist(){ 
    $startdir './'
    $ignoredDirectory[] = '.'
    $ignoredDirectory[] = '..'
      if (
    is_dir($startdir)){ 
          if (
    $dh opendir($startdir)){ 
              while ((
    $folder readdir($dh)) !== false){ 
                  if (!(
    array_search($folder,$ignoredDirectory) > -1)){ 
                    if (
    filetype($startdir $folder) == "dir"){ 
                          
    $directorylist[$startdir $folder]['name'] = $folder
                          
    $directorylist[$startdir $folder]['path'] = $startdir
                      } 
                  } 
              } 
              
    closedir($dh); 
          } 
      } 
    return(
    $directorylist); 

     

    if (empty(
    $_SESSION['folders'])) { 
        
    $_SESSION['folders'] = folderlist(); 
        
    shuffle($_SESSION['folders']); 


    $folder array_pop($_SESSION['folders']); 
    print_r($folder);

    ?>
    And I have folders 1,2,3 in the directory. It's showing this:
    PHP Code:
    Array ( [name] => [path] => ./ ) 

  8. #8
    SitePoint Wizard silver trophy
    Join Date
    Mar 2006
    Posts
    6,132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    is there a problem with that?

  9. #9
    SitePoint Guru
    Join Date
    Jun 2004
    Location
    Finland
    Posts
    703
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    function get_random_dir($base_dir)
    {
      
    $dirs glob(sprintf('%s/*',$base_dir),GLOB_ONLYDIR);

      return empty(
    $dirs) ? null $dirs[array_rand($dirs)];
    }

    echo 
    get_random_dir('.'); 
    I'll leave it to you to figure out how to get the path and the name of the folder separated. You have to do something yourself or you'll never learn!

  10. #10
    SitePoint Evangelist Scott.Botkins's Avatar
    Join Date
    Dec 2004
    Posts
    598
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ahh, got it guys. Thanks for help, I did this about a year ago and I just forgot but thanks for reminding me.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •