jammesz Posted March 29, 2007 Share Posted March 29, 2007 I need some help with this script im doing. Basically there is an array: Array ( [0] => 1alskdjf_rgtg20070319123216.jnws [1] => 2aaaaaa20070319131249.jnws [2] => 2zzzzzzzz20070320104051.jnws [ff] => Array ( [0] => 2zzzzzzzz20070320104051.jnws [New Folder] => Array ( [0] => 2zrrrrrz20070320104051.jnws ) [zz] => ) ) this array is a directory tree. I need to present this in a select option box with the array inside but only with the directorys ( no files ). Any help would be appreciated and if u need more info just ask. Link to comment https://forums.phpfreaks.com/topic/44757-solved-array-presentation/ Share on other sites More sharing options...
btherl Posted March 29, 2007 Share Posted March 29, 2007 I would convert it all to strings, like "ff", "ff/New Folder". Then you can convert back when you receive that data back from the form. Is this what you are trying to do? Link to comment https://forums.phpfreaks.com/topic/44757-solved-array-presentation/#findComment-217321 Share on other sites More sharing options...
jammesz Posted March 29, 2007 Author Share Posted March 29, 2007 i need the folders to look like this in the select options box: dir1 |_subdir1 |_ subdir2 dir2 dir3 |_subdir1 |_ subdir2 like the print_r function but customized Link to comment https://forums.phpfreaks.com/topic/44757-solved-array-presentation/#findComment-217323 Share on other sites More sharing options...
jammesz Posted March 30, 2007 Author Share Posted March 30, 2007 OK i solved it. Since this was hard to figure out how to do, i presume that there is someone else out there who needs this code as well so im posting it here. <? $html->header(); $html->top_nav(); echo '<select name=""><option>News Categories</option>'; function retrieveTree($path){ $delim = strstr(PHP_OS, "WIN") ? "\\" : "/"; if($dir=@opendir($path)){ $i=0; while (($element=readdir($dir))!== false){ if(is_dir($path.$delim.$element) && $element!= "." && $element!= ".."){ $c=substr_count($path.$delim.$element, $delim); $array['d'.$c.$element] = retrieveTree($path.$delim.$element); }elseif($element!= "." && $element!= ".."){ $i++; $array['f'.$i] = $element; } } closedir($dir); } return (isset($array) ? $array : false); } function constructTree($branch){ foreach($branch as $key=>$value){ $key_type=substr($key,0,1); $key_indent=substr($key,1,1); $key=substr($key,2); if($key_type=='d'){ echo '<option>'; for($i=1;$i<$key_indent;$i++){ echo ' |- '; } echo ' '.$key.'</option>'; if(is_array($value)){ constructTree($value); } } } } $files=retrieveTree($news_dir); $list=constructTree($files); echo ' </select> <hr> <pre>'; print_r($files); echo ' </pre> '; $html->footer(); ?> Attached is what the script looks like (the news categories are directories). PS. this is not using any databases [attachment deleted by admin] Link to comment https://forums.phpfreaks.com/topic/44757-solved-array-presentation/#findComment-218040 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.