Jump to content

[SOLVED] Display folder name starting with certain letters?


Ty44ler

Recommended Posts

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>");

}

}
?>

Link to comment
Share on other sites

I've been doing this php thing for only the past few days so there aren't many ideas to spark yet.  ;D

 

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>");

}

}
?>

Link to comment
Share on other sites

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);

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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...

Link to comment
Share on other sites

<?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);

?>

Link to comment
Share on other sites

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..

 

 

Link to comment
Share on other sites

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.... :-\

 

 

 

Link to comment
Share on other sites

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);

 

?

Link to comment
Share on other sites

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).

Link to comment
Share on other sites

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.