Ty44ler Posted June 2, 2009 Share Posted June 2, 2009 I'm displaying a bunch of folder names on a page, but want to sort it out and have them listed in sections... I'm thinking have 26 different pieces of php code that will search for folders beginning with the individual letter so I can put my horizontal rules and anchor tags between each letters php code. How do I just display the folder names that just start with a certain letter??? Here's my current code... <?php while(false != ($file = readdir($dir))) { if($file!="." && $file!=".." && $file!="FTPCss.css" && $file!="images" && $file!="index.php" &&$file!="a+") { echo(" <p><a href=\"$file\">$file </a></p>"); } } ?> Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted June 2, 2009 Share Posted June 2, 2009 <?php if (strpos($file, 'a') === 0) { // do something } That spark any ideas? Quote Link to comment Share on other sites More sharing options...
Ty44ler Posted June 3, 2009 Author Share Posted June 3, 2009 I've been doing this php thing for only the past few days so there aren't many ideas to spark yet. This is what I think you were meaning for me to do? I'm not getting any output though... <?php while(false != ($file = readdir($dir))) { if(strpos($file, 'a') === 0) { echo(" <p><a href=\"$file\">$file </a></p>"); } } ?> Quote Link to comment Share on other sites More sharing options...
Ty44ler Posted June 3, 2009 Author Share Posted June 3, 2009 Nevermind, that's perfect! I'm really starting to love php. So many cool things you can do with it. BTW I wasn't getting an output because I didn't capitalize the A... Quote Link to comment Share on other sites More sharing options...
nrg_alpha Posted June 3, 2009 Share Posted June 3, 2009 I'm thinking glob with the GLOB_ONLYDIR flag... Some examples: function directoryFetch($letter){ foreach(glob($_SERVER['DOCUMENT_ROOT'].'/'."$letter*", GLOB_ONLYDIR) as $val){ // change $_SERVER['DOCUMENT_ROOT'] to correct path echo basename($val) . "<br />\n"; } } $letter = 'a'; directoryFetch($letter); Or simply get all directories, store them into an array, and then fetch the appropriate alphabetical section from there: $dirNames = glob($_SERVER['DOCUMENT_ROOT'].'/'."*", GLOB_ONLYDIR); $dirNames = array_map('basename', $dirNames); // array containing all directory names function directoryFetch($letter, $arr){ foreach($arr as $val){ if (strpos($val, $letter) === 0) { echo $val . "<br />\n"; } } } $letter = 'g'; directoryFetch($letter, $dirNames); Quote Link to comment Share on other sites More sharing options...
Ty44ler Posted June 3, 2009 Author Share Posted June 3, 2009 Yeah, I'm gonna have to use your way, nrg_alpha. I tried to place the Ken's code in each section on the same page, but only the folders listed with A are showing up. <?php $dirNames = glob($_SERVER['.'].'/'."*", GLOB_ONLYDIR); $dirNames = array_map('basename', $dirNames); // array containing all directory names function directoryFetch($letter, $arr){ foreach($arr as $val){ if (strpos($val, $letter) === 0) { echo $val . "<br />\n"; } } } $letter = 'A'; directoryFetch($letter, $dirNames); ?> Am I doing something wrong with this code? There is no output and the index.php file is in the same folder as all the folders with the names I'm trying to list. I have a feeling I didnt do the server thing right. Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted June 3, 2009 Share Posted June 3, 2009 You can create an array and loop through it. I'm sorry I didn't write the entire code for you like nrg_alpha did. Quote Link to comment Share on other sites More sharing options...
Ty44ler Posted June 3, 2009 Author Share Posted June 3, 2009 I appreciate the fast responses. I'm still having some trouble and I don't mean to sound like I'm asking you to do the code for me, but I don't really know how to write an array and then use the code above and loop through it. I tried messing around with a for loop, but only parse errors and blank outputs came from that... Quote Link to comment Share on other sites More sharing options...
Andy-H Posted June 3, 2009 Share Posted June 3, 2009 <?php $dirNames = glob($_SERVER['.'].'/'."*", GLOB_ONLYDIR); $dirNames = array_map('basename', $dirNames); // array containing all directory names $dirNames = sort($dirNames); function directoryFetch($letter, $arr) { if ( is_array($letter) ) { forEach ( $letter as $v ) { foreach( $arr as $val ) { if (strpos($val, $v) === 0) { $str .= $val . "<br /> "; } } $str .= '<br /><hr width="75%" />'; } } else { foreach( $arr as $val ) { if (strpos($val, $letter) === 0) { $str .= $val . "<br /> "; } } $str .= '<br /><hr width="75%" />'; } return $str; } $letter = array('A', 'a', 'B', 'b', 'C', 'c'); //etc... echo directoryFetch($letter, $dirNames); ?> Quote Link to comment Share on other sites More sharing options...
Ty44ler Posted June 3, 2009 Author Share Posted June 3, 2009 Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\Project Files\index.php on line 84 That's the error I'm getting. It occurs in this line of code: $letter = array('A', 'a', 'B', 'b', 'C', 'c'); Quote Link to comment Share on other sites More sharing options...
nrg_alpha Posted June 3, 2009 Share Posted June 3, 2009 Im thinking also along these lines: $dirNames = glob($_SERVER['DOCUMENT_ROOT'].'/'."*", GLOB_ONLYDIR); $dirNames = array_map('basename', $dirNames); // array containing all directories function directoryFetch($letter, $arr){ $letter = chr($letter); $section = false; foreach($arr as $val){ if (stripos($val, $letter) === 0) { if(!$section){ $section = true; echo "<br />" . 'Directories » ' . $letter . "<br />\n"; } echo $val . "<br />\n"; } } } for ($alpha = 0x61 ; $alpha < 0x7b ; $alpha++) { //0x61 = a, 0x7b = { (which is right after z) directoryFetch($alpha, $dirNames); } The odd thing, is when I originally had the for loop's $alpha values go from 'a' to '{' (which is the character after z), [and didn't use chr($letter) in the function, as we are already dealing with letters]: for ($alpha = 'a' ; $alpha < '{' ; $alpha++) .... My system always hung.. if I changed '{' to 'z' it worked (but only matched up the y obviously, as the for loop only counted up to a value less than z (which makes sense). So, I ended up declaring hex values instead, and with the use of chr in the function, it worked.. Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted June 3, 2009 Share Posted June 3, 2009 nrg_alpha, 'z'++ == 'aa', not '{'. Where did you get '{' from? Just curious. Quote Link to comment Share on other sites More sharing options...
nrg_alpha Posted June 3, 2009 Share Posted June 3, 2009 Where did you get '{' from? Just curious. { comes after z in the ascii table. EDIT - Yeah, I tested the z++ afterwards and realized the outcome.. but initially, I tested it within the for loop, and the system didn't like it. Quote Link to comment Share on other sites More sharing options...
Ty44ler Posted June 3, 2009 Author Share Posted June 3, 2009 Im thinking also along these lines: $dirNames = glob($_SERVER['DOCUMENT_ROOT'].'/'."*", GLOB_ONLYDIR); $dirNames = array_map('basename', $dirNames); // array containing all directories function directoryFetch($letter, $arr){ $letter = chr($letter); $section = false; foreach($arr as $val){ if (stripos($val, $letter) === 0) { if(!$section){ $section = true; echo "<br />" . 'Directories » ' . $letter . "<br />\n"; } echo $val . "<br />\n"; } } } for ($alpha = 0x61 ; $alpha < 0x7b ; $alpha++) { //0x61 = a, 0x7b = { (which is right after z) directoryFetch($alpha, $dirNames); } The odd thing, is when I originally had the for loop's $alpha values go from 'a' to '{' (which is the character after z), [and didn't use chr($letter) in the function, as we are already dealing with letters]: for ($alpha = 'a' ; $alpha < '{' ; $alpha++) .... My system always hung.. if I changed '{' to 'z' it worked (but only matched up the y obviously, as the for loop only counted up to a value less than z (which makes sense). So, I ended up declaring hex values instead, and with the use of chr in the function, it worked.. That works, but like before my root is off. I changed it to: $dirNames = glob($_SERVER['xampp\htdocs'].'/'."*", GLOB_ONLYDIR); and $dirNames = glob($_SERVER['.'].'/'."*", GLOB_ONLYDIR); and $dirNames = glob($_SERVER['c:\xampp\htdoc\project files'].'/'."*", GLOB_ONLYDIR); I keep getting the same folder names listed (folders are from the c:/ drive). Why isn't the root changing? This may be a very dumb question.... :-\ Quote Link to comment Share on other sites More sharing options...
nrg_alpha Posted June 3, 2009 Share Posted June 3, 2009 Don't set the path as you have done: $_SERVER['xampp\htdocs'] or $_SERVER['c:\xampp\htdoc\project files']. $_SERVER['DOCUMENT_ROOT] should represent your website's root. So depending on where in your site you want to check the directories, you will want to keep $_SERVER['DOCUMENT_ROOT] (which will represent something like 'C:/xampp/htdocs' or something along those lines if you install it in the root of your c drive. If anything, you'll have to modify the '/' part after it.. and seeing how you have project files, perhaps something like: $dirNames = glob($_SERVER['DOCUMENT_ROOT'].'/project files/'."*", GLOB_ONLYDIR); ? Quote Link to comment Share on other sites More sharing options...
Ty44ler Posted June 3, 2009 Author Share Posted June 3, 2009 Wow, I have a lot to learn, but anyway that was the trick! Thank you all very much. I don't think I've ever had such fast and effective responses on a forum like this before. I definitely have this site bookmarked. Quote Link to comment Share on other sites More sharing options...
nrg_alpha Posted June 3, 2009 Share Posted June 3, 2009 Wow, I have a lot to learn, but anyway that was the trick! Thank you all very much. I don't think I've ever had such fast and effective responses on a forum like this before. I definitely have this site bookmarked. Some days go smoother than others Glad it worked. You can flag this thread as resolved (I only mention this as I noticed you are relatively new here.. so just incase you weren't aware). Quote Link to comment 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.