jacko310592 Posted November 11, 2009 Share Posted November 11, 2009 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 (: Quote Link to comment https://forums.phpfreaks.com/topic/181130-solved-glob-data-to-table/ Share on other sites More sharing options...
jacko310592 Posted November 11, 2009 Author Share Posted November 11, 2009 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? Quote Link to comment https://forums.phpfreaks.com/topic/181130-solved-glob-data-to-table/#findComment-955626 Share on other sites More sharing options...
Zane Posted November 11, 2009 Share Posted November 11, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/181130-solved-glob-data-to-table/#findComment-955638 Share on other sites More sharing options...
Psycho Posted November 11, 2009 Share Posted November 11, 2009 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? Quote Link to comment https://forums.phpfreaks.com/topic/181130-solved-glob-data-to-table/#findComment-955641 Share on other sites More sharing options...
jacko310592 Posted November 11, 2009 Author Share Posted November 11, 2009 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) Quote Link to comment https://forums.phpfreaks.com/topic/181130-solved-glob-data-to-table/#findComment-955642 Share on other sites More sharing options...
Psycho Posted November 11, 2009 Share Posted November 11, 2009 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> Quote Link to comment https://forums.phpfreaks.com/topic/181130-solved-glob-data-to-table/#findComment-955648 Share on other sites More sharing options...
jacko310592 Posted November 11, 2009 Author Share Posted November 11, 2009 thanks for the help mjdamato, that worked perfectly (: Quote Link to comment https://forums.phpfreaks.com/topic/181130-solved-glob-data-to-table/#findComment-955651 Share on other sites More sharing options...
Psycho Posted November 11, 2009 Share Posted November 11, 2009 My pleasure. I hope you noticed that you can easily change the number of columns to be used using that first variable. Quote Link to comment https://forums.phpfreaks.com/topic/181130-solved-glob-data-to-table/#findComment-955654 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.