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

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.