Howdy,

I am trying to output a folder tree based on a file structure I have.

So here is the way the database is laid out.

directoryID | pageID | nestedID | nestedDepth | name | incname

directoryID is auto incremeted,
pageID is not needed for this help
nestedID is what is looked for to output the correct file structure
nestedDepth is the file depth from the root folder

Here is my code:

mysql_select_db($database_ControlPanel, $ControlPanel);
$query_getDirectorys = "SELECT * FROM cp_directory WHERE nestedID = '0'";
$getDirectorys = mysql_query($query_getDirectorys, $ControlPanel) or die(mysql_error());
$totalRows_getDirectorys = mysql_num_rows($getDirectorys);
if($totalRows_getDirectorys != 0){
	while($fb = mysql_fetch_array($getDirectorys)){
		echo "<li><strong>".$fb['name']."</strong></li>\n";
		echo outputFolders($fb['directoryID']);
	}
}

And here is the function "outputFolders" which is called

			function outputFolders($id){
			global $database_ControlPanel, $ControlPanel;
			mysql_select_db($database_ControlPanel, $ControlPanel);
			$query_f = "SELECT * FROM cp_directory WHERE nestedID = '".$id."'";
			$f = mysql_query($query_f, $ControlPanel) or die(mysql_error());
			$totalRows_f = mysql_num_rows($f);
			$folder = "";
			if($totalRows_f != 0){
				$folder .= "<ul class=\"folders\">\n";
				while($fa = mysql_fetch_array($f)){
					$folder .=  "<li><strong>".$fa['name']."</strong></li>\n";
					mysql_select_db($database_ControlPanel, $ControlPanel);
					$query_n = "SELECT * FROM cp_directory WHERE nestedID = '".$fa['directoryID']."'";
					$n = mysql_query($query_n, $ControlPanel) or die(mysql_error());
					$totalRows_n = mysql_num_rows($n);
					if($totalRows_n != 0){
						$folder .= "<ul class=\"folders\">\n";
						while($fc = mysql_fetch_array($n)){
							$folder .=  "<li><strong>".$fc['name']."</strong></li>\n";
							mysql_select_db($database_ControlPanel, $ControlPanel);
							$query_na = "SELECT * FROM cp_directory WHERE nestedID = '".$fc['directoryID']."'";
							$na = mysql_query($query_na, $ControlPanel) or die(mysql_error());
							$totalRows_na = mysql_num_rows($na);
							if($totalRows_na != 0){
								$folder .= "<ul class=\"folders\">\n";
								while($fd = mysql_fetch_array($na)){
									$folder .=  "<li><strong>".$fd['name']."</strong></li>\n";
								}
								//echo outputFolders($fa['directoryID']);
								$folder .=  "</ul>\n";
							}
						}
						//echo outputFolders($fa['directoryID']);
						$folder .=  "</ul>\n";
					}
				}
				$folder .=  "</ul>\n";
			}
			return $folder;
		}

For the function I basicly have the same code repeated to keep outputing nests.

Now I have tried to just recall the "outputFolders" function inside itself but that doesnt put it under the right folder.

Any help will be great apprieacted.

    wow, this whole function looks way un-necessary... you need to create a function that re-calls itself to go to the next level in order to continuiously get accurate folder layouts...

    I have such a function on my site that you can add readdir() and is_dir() functionality to in order to get a short, simple function to do what you need.

    http://www.phphaven.com/article.php?id=10103

      Ok...

      Thanks for the function...

      Now how do I make that work for my function?

      DO I have format the results into an array before I call them to the function?

        Ok I got it THANK YOU SO MUCH!

        Here is what I had to:

        function debug_array($array){ // outputs array
        		global $database_ControlPanel, $ControlPanel;
        			$out = "<ul>\n";
        			foreach($array as $key=>$val){
        				mysql_select_db($database_ControlPanel, $ControlPanel);
        				$query_getD = "SELECT * FROM cp_directory WHERE directoryID = '".$val."'";
        				$getD = mysql_query($query_getD, $ControlPanel) or die(mysql_error());
        				$row_getD = mysql_fetch_array($getD);
        				$totalRows_getD = mysql_num_rows($getD);
        				$out .= "<li>".$row_getD['name']."</li>\n";
        
        			mysql_select_db($database_ControlPanel, $ControlPanel);
        			$query_f = "SELECT * FROM cp_directory WHERE nestedID = '".$val."'";
        			$f = mysql_query($query_f, $ControlPanel) or die(mysql_error());
        			$totalRows_f = mysql_num_rows($f);
        			$arrayIa = array();
        			while($fc = mysql_fetch_array($f)){
        				$arrayIa[] = $fc['directoryID'];
        			}
        			if(is_array($arrayIa)){ 
        				$out .= debug_array($arrayIa);
        			}
        		}
        		$out .= "</ul>\n";
        		return $out;
        	}
        if($totalRows_getDirectorys != 0){
        			$arrayI = array();
        			while($fb = mysql_fetch_array($getDirectorys)){
        				$arrayI[] = $fb['directoryID'];
        			}
        			echo debug_array($arrayI);
        		}

          you probably want to rename the function to something more within your scope.

          The original function name was for outputing arrays in a more easily readable format for debugging. =O)

          Good job kickin azz and figuring your stuff out dood!!!

            Write a Reply...