Ask a Question related to PHP Development, Design and Development.
-
Ryan Sullivan #1
PHP Newbie - Question
Hello,
I am working with a script that a friend of mine wrote. The script
traverses a directory and outputs the files and folders to a table.
What I'd like to do is to have the folders expand and collapse with
little plus/minus signs beside them. Can anyone point me in the right
direction? An example of the output can be seen here (in the yellow
box) --> [url]http://rpatrick.homedns.org/faculty/mason_b/index.php[/url]
The script is as follows:
<?php
// Put this file in a directory. And then create a subdirectory in that
directory called "Useful Links".
// then put whatever you want into the Useful Links directory. Files,
folders, whatever. Then access
// this program in a web browser. It shoudl search the "Useful Links"
directory and output what it finds.
// I did this a sort of challenge to myself, so if you don't use it
don't worry.
// if you like the idea, I can change the bulleted list output to
something more visually appealing (like tables).
// this function is used to return all characters in a haystack that
occur before the needle
function strrrchr($haystack,$needle)
{
return substr($haystack,0,strpos($haystack,$needle));
}
// this function tries to find all occurences of the word "AND" and
replace with with "&"
function replace_and($str){
return str_replace(array("AND", "And", "and"), "&", $str);
}
// this is a recursive function to traverse a directory structure and
return a one-dimensional array
function get_dir_array($dir) {
STATIC $dir_array;
$current_dir = "";
if(!($dh = @opendir($dir))){
echo "<!-- Directory not found $dir -->";
return false;
}
while($fname = readdir($dh)) {
if($fname!='.' && $fname!='..') {
if(is_dir("$dir/$fname")) {
get_dir_array("$dir/$fname");
} else {
if($current_dir == ""){
$current_dir = $dir;
}
$dir_array[$current_dir][]=$current_dir."/".$fname;
}
}
}
closedir($dh);
return $dir_array;
}
function cmp($a, $b) {
$a = strrrchr(basename($a), ".");
$b = strrrchr(basename($b), ".");
return strnatcasecmp($a, $b);
}
// this function takes a one-dimensional array and converts it into a
multi-dimensinal array using "/" as a delimmiter in the array key
function convert_array($old_array){
$new_array = array();
if(is_array($old_array)){
foreach($old_array AS $key=>$filename_array){
if(is_array($filename_array)){
usort($filename_array, "cmp");
foreach($filename_array AS $filename){
$explode_array = explode("/", $key);
$temp_array = '$new_array';
foreach($explode_array AS $new_key=>$new_val){
$temp_array .= '[\''. $new_val .'\']';
}
$temp_array .= '[]=\''.$filename.'\';'."\n";
eval($temp_array);
}
}
}
}
return $new_array;
}
// this function takes a converted array and builds a formated
bulleted list
function display_links($links_array){
STATIC $i = 0;
$buffer = "";
if(is_array($links_array)){
$buffer .= "\n<table border=0 cellspacing=0 cellpadding=2>";
foreach($links_array AS $title=>$link){
if(!is_numeric($title) && $i > 0){
$buffer .= "\n <tr>\n\t<td colspan=2
NOWRAP>".replace_and($title)."</td>\n </tr>";
}
if(!is_array($link)){
$buffer .= "\n <tr>\n\t<td NOWRAP> <a target='_blank'
href='".str_replace(" ", "%20",
$link)."'>".replace_and(strrrchr(basename($link), "."))."</a></td>\n
</tr>";
}else{
$i++;
$buffer .= "\n <tr>\n\t<td>\n<table border=0 cellspacing=0
cellpadding=2>\n
<tr>\n\t<td> </td>\n\t<td>".display_links($link)."</td>\n
</tr>\n</table>\n\t</td>\n </tr>";
}
}
$buffer .= "\n</table>";
}
$i++;
return $buffer;
}
// directory containing the "Useful Links", assumes the location is
relative to the current script.
$start_directory = "Useful Links";
// now get an unformatted list of links, and then convert it into a
multi-dimensional array
$useful_links_array = convert_array(get_dir_array($start_directory));
// not take that list and change it into something to display on the
screen
$display_links = display_links($useful_links_array);
?>
<?php
// now "echo" that list of links to the screen
echo $display_links;
?>
--
To reply, please remove your robe.
Ryan Sullivan Guest
-
A newbie with a newbie question
Good afternoon everyone, My Name is Dusty I am new to this forum and pretty new to Acrobat. I have Acrobat 9 running on an IMAC running 10.5.2 I... -
newbie question,,,
I converted an AVI to FLV in the encoder. The resulsting file only opens a blank flash 8 player. I can't even get it to play within the flash app.... -
Newbie Question: Biz Card Template Question
Hi, I got the Pagemaker PlugIn - I am using one of the templates for Business Cards - the elements appear to be grouped (bound box all around when I... -
Pen Tool Use Question. (Embarrassingly Newbie Question)
I'm currently using Flash MX and whenever I choose the Pen Tool instead of the pen nib with the small "x" beside it that supposed to show up on... -
Newbie OO question
In article <EbkRa.331382$fC.2436421@news.easynews.com>, "Ed W" <dodgynewsgroups@ewildgoose.demon.co.uk> wrote: perldoc perltoot it's all you... -
Ryan Sullivan #2
PHP Newbie - Question
Hello,
I am working with a script that a friend of mine wrote. The script
traverses a directory and outputs the files and folders to a table.
What I'd like to do is to have the folders expand and collapse with
little plus/minus signs beside them. Can anyone point me in the right
direction? An example of the output can be seen here (in the yellow
box) --> [url]http://rpatrick.homedns.org/faculty/mason_b/index.php[/url]
The script is as follows:
<?php
// Put this file in a directory. And then create a subdirectory in that
directory called "Useful Links".
// then put whatever you want into the Useful Links directory. Files,
folders, whatever. Then access
// this program in a web browser. It shoudl search the "Useful Links"
directory and output what it finds.
// I did this a sort of challenge to myself, so if you don't use it
don't worry.
// if you like the idea, I can change the bulleted list output to
something more visually appealing (like tables).
// this function is used to return all characters in a haystack that
occur before the needle
function strrrchr($haystack,$needle)
{
return substr($haystack,0,strpos($haystack,$needle));
}
// this function tries to find all occurences of the word "AND" and
replace with with "&"
function replace_and($str){
return str_replace(array("AND", "And", "and"), "&", $str);
}
// this is a recursive function to traverse a directory structure and
return a one-dimensional array
function get_dir_array($dir) {
STATIC $dir_array;
$current_dir = "";
if(!($dh = @opendir($dir))){
echo "<!-- Directory not found $dir -->";
return false;
}
while($fname = readdir($dh)) {
if($fname!='.' && $fname!='..') {
if(is_dir("$dir/$fname")) {
get_dir_array("$dir/$fname");
} else {
if($current_dir == ""){
$current_dir = $dir;
}
$dir_array[$current_dir][]=$current_dir."/".$fname;
}
}
}
closedir($dh);
return $dir_array;
}
function cmp($a, $b) {
$a = strrrchr(basename($a), ".");
$b = strrrchr(basename($b), ".");
return strnatcasecmp($a, $b);
}
// this function takes a one-dimensional array and converts it into a
multi-dimensinal array using "/" as a delimmiter in the array key
function convert_array($old_array){
$new_array = array();
if(is_array($old_array)){
foreach($old_array AS $key=>$filename_array){
if(is_array($filename_array)){
usort($filename_array, "cmp");
foreach($filename_array AS $filename){
$explode_array = explode("/", $key);
$temp_array = '$new_array';
foreach($explode_array AS $new_key=>$new_val){
$temp_array .= '[\''. $new_val .'\']';
}
$temp_array .= '[]=\''.$filename.'\';'."\n";
eval($temp_array);
}
}
}
}
return $new_array;
}
// this function takes a converted array and builds a formated
bulleted list
function display_links($links_array){
STATIC $i = 0;
$buffer = "";
if(is_array($links_array)){
$buffer .= "\n<table border=0 cellspacing=0 cellpadding=2>";
foreach($links_array AS $title=>$link){
if(!is_numeric($title) && $i > 0){
$buffer .= "\n <tr>\n\t<td colspan=2
NOWRAP>".replace_and($title)."</td>\n </tr>";
}
if(!is_array($link)){
$buffer .= "\n <tr>\n\t<td NOWRAP> <a target='_blank'
href='".str_replace(" ", "%20",
$link)."'>".replace_and(strrrchr(basename($link), "."))."</a></td>\n
</tr>";
}else{
$i++;
$buffer .= "\n <tr>\n\t<td>\n<table border=0 cellspacing=0
cellpadding=2>\n
<tr>\n\t<td> </td>\n\t<td>".display_links($link)."</td>\n
</tr>\n</table>\n\t</td>\n </tr>";
}
}
$buffer .= "\n</table>";
}
$i++;
return $buffer;
}
// directory containing the "Useful Links", assumes the location is
relative to the current script.
$start_directory = "Useful Links";
// now get an unformatted list of links, and then convert it into a
multi-dimensional array
$useful_links_array = convert_array(get_dir_array($start_directory));
// not take that list and change it into something to display on the
screen
$display_links = display_links($useful_links_array);
?>
<?php
// now "echo" that list of links to the screen
echo $display_links;
?>
--
To reply, please remove your robe.
Ryan Sullivan Guest
-
David Gillen #3
Re: PHP Newbie - Question
An noise sounding like Ryan Sullivan said:
Eh, you'd probably want to look at generating the whole directory tree and> Hello,
> I am working with a script that a friend of mine wrote. The script
> traverses a directory and outputs the files and folders to a table.
> What I'd like to do is to have the folders expand and collapse with
> little plus/minus signs beside them. Can anyone point me in the right
> direction? An example of the output can be seen here (in the yellow
> box) --> [url]http://rpatrick.homedns.org/faculty/mason_b/index.php[/url]
>
then displaying it dynamically with javascript used to expand/contract
sections of it.
David
David Gillen Guest



Reply With Quote

