Loop Problems


<?php 
session_start(); 
$edit_page = $_SESSION['edit_page'];
require 'approve.php';
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <title>Delete Page</title>
    <link href="style.css" type="text/css" rel="stylesheet">
    
</head>

<body>
<?php include_once $_SERVER['DOCUMENT_ROOT'] . '/admin/inc/head.php'; ?>
  <?php echo '<form action="delete-page.php" method="post">' ?>
    <div class="warning">
    <p>Note: When you hit submit, the page you have selected will be gone forever.</p>

<p>Make sure you have selected the correct page, because once it's gone it's gone!</p>
    </div>
   <table summary="" cellpadding="0" cellspacing="0" border="0" width="100%">

      <tr>
        <td><b>Delete Page</b></td>
                
                <td>
                
<select name="delete_line" is="delete_line">    
<?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') {
                  $file = ereg_replace('-',' ',$file);
              $file = ereg_replace('.php','',$file);
              $file = ucwords($file);
                  echo '<option>'.$file.'</option>';
                }
    }
    closedir($handle);
}

?> 
</select>    
</td>

        <td>
                <input type="submit" value="Delete" class="red-button">
        </td>
      </tr>
    </table>
        
  </form>
<?php include_once $_SERVER['DOCUMENT_ROOT'] . '/admin/inc/foot.php'; ?>    
</body>
</html>

In the above code, where it says:


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

…I want to look at a directory called:

/category-menus/

…and I want to exclude every file that is listed in there (as well as sitemap.php and index.php, which I want excluded as well).

How can I add a loop to this line to exlude the files in that directory?

//Working code (finished but need to exclude the category files that the first block outputs).

:shifty:


<?php
//First Block (unfinished)
$dir = $_SERVER['DOCUMENT_ROOT'] . '/category-menus/';
$excludeArray = array();
if ($handle = opendir($dir))
{
    while($file = readdir($handle))
    {
        clearstatcache();
        if (is_file($dir.'/'.$file) && $file != 'home.php') {
            // I need an array here, or something for $file
            $excludeArray[] = $file;
        }
    }
    closedir($handle);
}
//Working code (finished but need to exclude the category files that the first block outputs).
$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' && !in_array($file, $excludeArray)) {
                  $file = ereg_replace('-',' ',$file);
              $file = ereg_replace('.php','',$file);
              $file = ucwords($file);
                  echo '<option>'.$file.'</option>';
                }
    }
    closedir($handle);
}

?>

<?php
//First Block (unfinished)
$dir = $_SERVER['DOCUMENT_ROOT'] . '/category-menus/';
if($handle = opendir($dir))
{
    while($file = readdir($handle))
    {
        clearstatcache();
                if(is_file($dir.'/'.$file) && $file != 'home.php') {
                  //I need an array here, or something for $file
                }
    }
    closedir($handle);
}
//Working code (finished but need to exclude the category files that the first block outputs).
$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 = ereg_replace('-',' ',$file);
              $file = ereg_replace('.php','',$file);
              $file = ucwords($file);
                  echo '<option>'.$file.'</option>';
                }
    }
    closedir($handle);
}

?> 

This is as far as I can get… am I getting there?

I am really struggling with this. I don’t undertand arrays. Can someone please give me just a little hint. I am looking up arrays and I just don’t get it. I have done arrays before and I never get it. There’s just something about arrays, I don’t get it :frowning:

I am very good with lots of things, like I was in school. I was an A+ student, except for when it came to maths. I didn’t get it :frowning:

Arrays is my maths in web development.

Add another directory handling block of code to your script (before the current one), in which you loop through the files of this other directory, and instead of unlinking or echoing them, you put them in an array.

Then, inside the ‘if’ you quoted, add an in_array() check to see if the file has to be excluded.

I used this code and thought – what’s the deal? It’s listing all the pages but not the categpories. Then I tried taking the ! off:

&& !in_array($file, $excludeArray))

…and it works just fine!

Thanks heaps… I was doing my head in tryig to figure that out. Now my form works properly :slight_smile:

That was the hardest part of the whole script!