Jump to content

[SOLVED] Array presentation


jammesz

Recommended Posts

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

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]

Archived

This topic is now archived and is closed to further replies.

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