Results 1 to 3 of 3

Thread: include/exclude items from array depending on certain criteria

  1. #1
    Join Date
    Jul 2008
    Location
    Derbyshire, UK
    Posts
    3,033
    Thanks
    25
    Thanked 599 Times in 575 Posts
    Blog Entries
    40

    Default include/exclude items from array depending on certain criteria

    I'm using an auto-listing file script to create a quick content menu in list format. Here it is:


    Code:
    <?php
    $dh = opendir('/home/www/'.$_SERVER['HTTP_HOST']);
    
    echo '<ul><li><a href="index.php">Home</a></li>';
    
    while ($file = readdir($dh)) {
    // pages and directories to ignore
    if ($file!='.' && $file!='..'
    && $file!='template'
    && $file!='scripts'
    && $file!='index.php'
    && $file!='gallery.php'
    && $file!='videos.php'
    ) 
    {
     $file_array[] = $file;  
    }
     }
    
     sort ($file_array);
     foreach($file_array as $value) {
    	echo '<li><a href="'.$value.'">'.$value.'</a></li>'; 
            }
    
    if($gallery_page=='enabled'){echo '<li><a href="gallery.php">Gallery</a></li>'; }
    if($videos_page=='enabled') {echo '<li><a href="videos.php">Videos</a></li>'; }
    
    echo '</ul>';
    closedir($dh);
    ?>

    Within the "// pages and directories to ignore" part, I've set the index.php, gallery.php and videos.php pages to be excluded from the array listing.
    This is because the home page is referenced and echo'd right at the top of the listing and the gallery and videos page is referenced and echo'd at the bottom and are dependant on whether I have them "enabled" in my user config file.

    Everything works fine as it is but I would like to fine-tune the position of some of my list items.

    What I would like to do is include the gallery and videos page in the array, so they are sorted alphabetically along with all the other pages in my home directory, but I dont know how.

    I only need to include them in the array if the config file says "enabled", as per the last few lines. Can somebody please offer some assistance.

    Thanks

    Beverley

  2. #2
    Join Date
    Sep 2008
    Location
    Bristol - UK
    Posts
    842
    Thanks
    32
    Thanked 132 Times in 131 Posts

    Default

    I currently don't have access to an editor, so there may be a few errors, forgive me, but i'm just trying to get across the concept. Could you use this?

    PHP Code:
    <?php
    $dh 
    opendir('/home/www/'.$_SERVER['HTTP_HOST']);

    echo 
    '<ul><li><a href="index.php">Home</a></li>';

    while (
    $file readdir($dh)) {
    // pages and directories to ignore
    if ($file!='.' && $file!='..'
    && $file!='template'
    && $file!='scripts'
    && $file!='index.php'
    && $file!='gallery.php'
    && $file!='videos.php'

    {
     
    $file_array[] = $file;  
    }
     }

    if(
    $gallery_page=='enabled'){ $file_array[] = 'gallery.php'; }
    if(
    $videos_page=='enabled') { $file_array[] = 'videos.php'; }

     
    sort ($file_array);
     foreach(
    $file_array as $value) {
        echo 
    '<li><a href="'.$value.'">'.$value.'</a></li>'
            }

    echo 
    '</ul>';
    closedir($dh);
    ?>
    So you're adding in the gallery and video links before sorting the array, and only doing it if the config says so.

  3. The Following User Says Thank You to Schmoopy For This Useful Post:

    Beverleyh (02-16-2010)

  4. #3
    Join Date
    Jul 2008
    Location
    Derbyshire, UK
    Posts
    3,033
    Thanks
    25
    Thanked 599 Times in 575 Posts
    Blog Entries
    40

    Default

    Totally!

    That's great - thank you so much for your help.

    I did try a few things yesterday fiddling with unset() but I got myself into such a tangle.

    It seems so easy now looking at your example.

    Thanks again

    Beverley

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
  •