Jump to content

Recommended Posts

hey guys

 

i need some help with a piece of code i have which is supposed to be for using the glob function to retrieve directories within the current directory, then displaying the results within a table of 3 columns wide by how ever many rows the glob function requires (depending on how many directories it finds)

 

the following is my code so far, im really new to most of the PHP functions, so i hope i havnt gone too far off track:

 

 

<table border="0" cellpadding="0">
<?php $galleryDir = glob("{*}", GLOB_BRACE);
   for ($i = 0; $i < count($galleryDir) - 0; $i++)
if (stristr($galleryDir[$i], "index.php")===FALSE)
   {
      if ( (($i - 0) % 3) == 0)
         echo ' <tr>' . "\n\r";

        $dirLocation = $galleryDir[$i];
	$dirName = str_replace("_"," ",$dirLocation);
         echo "\t" . '<td>'.$dirName.'</td>' . "\n\r";

      if ( (($i - 2) % 3) == 0 || $i == (count($files) - 2) )
         echo '</tr>' . "\n\r";
   }
?>
</table>

 

 

can anyone please correct me wherever ive gone wrong

 

thanks guys (:

Link to comment
https://forums.phpfreaks.com/topic/181130-solved-glob-data-to-table/
Share on other sites

i think the problem with my code is with the following part:

 

if (stristr($galleryDir[$i], "index.php")===FALSE)

 

which as you can see, it is telling it to ignore a file which would also get caught by the glob function, the problem is that it doesnt stop the code from creating a place for where it would go within the table.

 

 

Can anyone think if theres a way to solve this?

If had to take a shot in the dark at what you were actually doing..(which is what you should have posted in the first place) I would say that you are creating a table off all the files within the current directory EXCEPT for index.php.  Am I correct?

 

If that's correct, then all you really need to do is this

</pre>
<table border="0" cellpadding="0">
   foreach($galleryDir as $i=>$file) {
       if($gallerDir[$i] != "index.php") { //           $dirLocation = $galleryDir[$i];
          $dirName = str_replace("_"," ",$dirLocation);
          $newRow = (($i % 3) == 0  && $i != 0)? true : false;
  if($newRow) echo "\n";
     echo "$dirName\n";
       }
   }
?>
</table

Yeah, I'm not really sure what you are trying to accomplish as what you state above does not correlate witht he code you posted. You state that you want a list of directories. BUt the code you posted would get all the directories and files within a single folder and then looks like it would display a record in the table for each folder and file in the directory.

 

Can you explain a little more exactly what you are trying to accomplish?

zanus, sorry if i didnt make myself clear within my 1st post, but yeah, thats basically it, appart from all im looking for within the directory is subdirectories, and yes i am trying to exclude the index.php from showing up.

 

 

your code look great-  but it doesnt stop the index.php from being displayed :/

any ideas?

 

 

btw, thanks for the reply (:

 

 

and mjdamato, all i have witin this directory is subdirectories and a single index.php file, sorry i didnt make that bit clear.    basically all i need the code to do is show all the sub directories within a table with 3 columns per row, also thanks for the reply (Y)

Try this. There is no need to use GLOB_BRACE since you only have one parameter in the braces. Instead you should use GLOB_ONLYDIR to get only the directories. Then there is no need to check for the index.php file.

 

<?php

$max_columns = 3;

$directories = glob('*', GLOB_ONLYDIR);

//Create table output
$current_column = 1;
foreach ($directories as $folder)
{
    if ( $current_column == 1)
    {
        $tableOutput .= "\t<tr>\n";
    }

    $folder = str_replace('_', ' ', $folder);
    $tableOutput .= "\t\t<td>{$folder}</td>\n";

    if ( $current_column++ == $max_columns)
    {
        $tableOutput .= "\t</tr>\n";
        $current_column = 1;
    }
}

//Close last row if needed
if ($current_column!=1)
{
    for(; $current_column<=$max_columns; $current_column++)
    {
        $tableOutput .= "\t\t<td> </td>\n";
    }
    $tableOutput .= "\t</tr>\n";
}

?>
<html>
<head></head>
<body>

<table border="1" cellpadding="0">
<?php echo  $tableOutput; ?>
</table>

</body>
</html>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.