Loop inside a loop?

Discussion in 'PHP' started by MrLeN, Dec 11, 2010.

  1. #1
    I have this code which reads a directory and lists all the files/file names (for deletion). The file names represent "pages" and "categories" in a flat file CMS. ie: Some page names are pages. Some are categories.:

    
    <?php
    
    $dir = $_SERVER['DOCUMENT_ROOT'] . '/pages/';
    if($handle = opendir($dir))
    {
        while($file = readdir($handle))
        {
            clearstatcache();
            if(is_file($dir.'/'.$file) && $file != 'sitemap.php' && $file != 'index.php' [color=red]&& $file != $from_loop[/color]) {
    				  $file = ereg_replace('-',' ',$file);
    		      $file = ereg_replace('.php','',$file);
    		      $file = ucwords($file);
    				  echo '<option>'.$file.'</option>';
    				}
        }
        closedir($handle);
    }
    
    ?> 
    
    Code (markup):
    I have added a new part in red. I want to put a loop inside a loop. I want the part in red to read "another" directory that contains files. Then, I want all of those file names to be excluded from the list above.

    The above code works (without the red part). I don't know how to put another loop inside the current loop.

    Above is enough explanation, but if you require more...

    Basically the current loop is getting a list of page names (they are all based of file names in directories). However, some of them are also "categories". I can pinpoint the categories, because I have another directory which contains all the same names (but only the ones that are categories). So I can hopefully add them to a loop inside a loop (the red part), and exclude them from the list.

    The end effect is a dropdown list to be able to delete pages and not list the categories with the regular pages. ie: Get a list that contains pages "and" categories (in one loop). Then, in another loop (the loop inside the loop) read a directory that contains "just" category names, and strip them out -- because I don't want "category" page names listed for deletion with the "regular page" page names.
     
    MrLeN, Dec 11, 2010 IP
  2. shofstetter

    shofstetter Well-Known Member

    Messages:
    178
    Likes Received:
    7
    Best Answers:
    1
    Trophy Points:
    120
    #2
    you could create a function that returns true or false and use it like this

    if(is_file($dir.'/'.$file) && $file != 'sitemap.php' && $file != 'index.php' && $file != fblack_list($file))

    function fblack_list($bad_name){
    $retvalue = FALSE;
    $my_list = new array("contact.php","notme.php",members.php");
    foreach($my_list as $file_name){
    if($file_name === $bad_name){
    $retvalue = TRUE;
    }
    return $retvalue;
    }
     
    shofstetter, Dec 12, 2010 IP
  3. MrLeN

    MrLeN Well-Known Member

    Messages:
    406
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    120
    #3
    This looks like a reasonable solution, but how about:

    $my_list = new array("contact.php","notme.php",members.php");

    The list is dynamic. The list comes from reading a directory. I can't specify the document names manually.

    The function would have to have a loop in it.

    I don't know how to create the line above with a loop.

    I am having trouble with loops as it is, let alone trying to insert arrays into loop on top of that.

    Can you show me how I can get the files in the array from a loop and then create a function out of it and then put the function in my original loop?

    I am really struggling with this problem.

    I am trying very hard and I have been for weeks, but I just can't do it.
     
    MrLeN, Dec 13, 2010 IP
  4. shofstetter

    shofstetter Well-Known Member

    Messages:
    178
    Likes Received:
    7
    Best Answers:
    1
    Trophy Points:
    120
    #4
    You do not have to use an array, you can use a loop. I was using an array in the example just to give a basic idea of how it could work. The important part is the return value. I used foreach instead of a loop. I don't know how you are acquiring the list to check against. You could check it with a regular loop inside the function in a similar fashion
    
    function fblack_list($bad_name, $dir){
            $retvalue = FALSE;
            if($handle = opendir($dir))
            {
                   while($file = readdir($handle))
                   {
                          if($file === $bad_name){
                                $retvalue = TRUE;
                          }
                   }
                   closedir($handle);
             }else{
                     die("Could not open directory");
             }
             return $retvalue;
    }
    
    Code (markup):
     
    shofstetter, Dec 13, 2010 IP
  5. MrLeN

    MrLeN Well-Known Member

    Messages:
    406
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    120
    #5
    I think we are getting somewhere.

    Here is the code I have so far. The directory I am ready from that contains the category filenames is /category-menus

    
    <?php
    $dir = $_SERVER['DOCUMENT_ROOT'] . '/category-menus/';
    function fblack_list($bad_name, $dir){
            $retvalue = FALSE;
            if($handle = opendir($dir))
            {
                   while($file = readdir($handle))
                   {
                          if($file === $bad_name){
                                $retvalue = TRUE;
                          }
                   }
                   closedir($handle);
             }else{
                     die("Could not open directory");
             }
             return $retvalue;
    }
    
    $dir = $_SERVER['DOCUMENT_ROOT'] . '/pages/';
    if($handle = opendir($dir))
    {
        while($file = readdir($handle))
        {
            clearstatcache();
            if(is_file($dir.'/'.$file) && $file != 'sitemap.php' && $file != 'index.php' && $file != fblack_list($file)) {
    				  $file = ereg_replace('-',' ',$file);
    		      $file = ereg_replace('.php','',$file);
    		      $file = ucwords($file);
    				  echo '<option>'.$file.'</option>';
    				}
        }
        closedir($handle);
    }
    
    ?> 
    
    Code (markup):
    The problem is that when I load the page that has that code, it seems to go belly up right at the select box.

    The page renders fine right up until the option box. The option box is blank, and the rest of the page fails to load.

    Somehow the page is having a coronary right at the option box (the dropdown), due to the above code.

    The 2 directories in the above code exist, so it can't the that.

    I can't work out what is wrong with the code.

    No error message is displayed.
     
    MrLeN, Dec 13, 2010 IP
  6. shofstetter

    shofstetter Well-Known Member

    Messages:
    178
    Likes Received:
    7
    Best Answers:
    1
    Trophy Points:
    120
    #6
    you are missing a parameter in your function call. What files are you wanting to compare with? I wrote that as a basic example.

    
    <?php
    $dir = $_SERVER['DOCUMENT_ROOT'] . '/category-menus/';
    [COLOR="red"][B]function fblack_list($bad_name, $dir)[/B][/COLOR]{
            $retvalue = FALSE;
            if($handle = opendir($dir))
            {
                   while($file = readdir($handle))
                   {
                          if($file === $bad_name){
                                $retvalue = TRUE;
                          }
                   }
                   closedir($handle);
             }else{
                     die("Could not open directory");
             }
             return $retvalue;
    }
    
    $dir = $_SERVER['DOCUMENT_ROOT'] . '/pages/';
    if($handle = opendir($dir))
    {
        while($file = readdir($handle))
        {
            clearstatcache();
            if(is_file($dir.'/'.$file) && $file != 'sitemap.php' && $file != 'index.php' && $file != [COLOR="red"][B]fblack_list($file)[/B][/COLOR]) {
    				  $file = ereg_replace('-',' ',$file);
    		      $file = ereg_replace('.php','',$file);
    		      $file = ucwords($file);
    				  echo '<option>'.$file.'</option>';
    				}
        }
        closedir($handle);
    }
    
    ?>
    
    
    Code (markup):
     
    shofstetter, Dec 13, 2010 IP
  7. MrLeN

    MrLeN Well-Known Member

    Messages:
    406
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    120
    #7
    I am sorry, I don't understand. What is supposed to go in place of $bad_name?
     
    MrLeN, Dec 13, 2010 IP
  8. shofstetter

    shofstetter Well-Known Member

    Messages:
    178
    Likes Received:
    7
    Best Answers:
    1
    Trophy Points:
    120
    #8
    $badname is ok, but I added a parameter "$dir" which is the directory to search for $badname in. if $badname is found in $dir the function returns TRUE if not it returns FALSE. I did not know what you were wanting to check $badname against so I added a directory parameter instead of using a bad list like in my first example. I can taylor the function to whatever you need, but I need to know what you are trying to compare the file to.

    function fblack_list(string badname, string "directory to search for bad name")
     
    shofstetter, Dec 13, 2010 IP
  9. MrLeN

    MrLeN Well-Known Member

    Messages:
    406
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    120
    #9
    in the directory is a bunch of filenames like:

    a-page.php
    another-page.php
    yet-another-page.php

    I want all of them in the list (so they can be excluded from the dropdown). That category contains only the names of category files. So if it's listed in that directory, I don't want it showing on the dropdown menu.

    The directory is:

    $dir = $_SERVER['DOCUMENT_ROOT'] . '/category-menus/';
     
    MrLeN, Dec 13, 2010 IP
  10. shofstetter

    shofstetter Well-Known Member

    Messages:
    178
    Likes Received:
    7
    Best Answers:
    1
    Trophy Points:
    120
    #10
    Try This:
    
    <?php
    function fblack_list($bad_name, $dir){
            $retvalue = FALSE;
            if($handle = opendir($dir))
            {
                   while($file = readdir($handle))
                   {
                          if($file === $bad_name){
                                $retvalue = TRUE;
                          }
                   }
                   closedir($handle);
             }else{
                     die("Could not open directory");
             }
             return $retvalue;
    }
    
    $dir = $_SERVER['DOCUMENT_ROOT'] . '/pages/';
    if($handle = opendir($dir))
    {
        while($file = readdir($handle))
        {
            clearstatcache();
            if(is_file($dir.'/'.$file) && $file != 'sitemap.php' && $file != 'index.php' && $file != fblack_list($file,$_SERVER['DOCUMENT_ROOT'].'/category-menus/')) {
    				  $file = ereg_replace('-',' ',$file);
    		      $file = ereg_replace('.php','',$file);
    		      $file = ucwords($file);
    				  echo '<option>'.$file.'</option>';
    				}
        }
        closedir($handle);
    }
    
    ?>
    Code (markup):
     
    shofstetter, Dec 13, 2010 IP
  11. MrLeN

    MrLeN Well-Known Member

    Messages:
    406
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    120
    #11
    wow, it works perfectly!

    Thanks heaps, I have been trying to make that work for weeks.

    This was one of the very last problems I had to make my CMS bug free.

    People were deleting "Categories" because they were listed in the "delete pages" dropdown (due to reasons which will be confusing to explain lol).

    Anyway, it's all working now so no more problems!

    I really appreciate your help, thanks so much!
     
    MrLeN, Dec 13, 2010 IP