Jump to content


Photo

Need help optimizing this little bit of code...


  • Please log in to reply
1 reply to this topic

#1 shiny_spoon

shiny_spoon
  • Members
  • PipPip
  • Member
  • 26 posts
  • LocationCanada

Posted 07 October 2006 - 09:33 PM

So I've got a fairly simple piece of code here, and it works like a charm. The problem is that it looks really inefficient to me.

The script is used in a virtual file browser to display a directory tree from the root folder to whatever you're currently browsing. It gets the directory you're currently browsing, then the directory in which THAT directory is located and so on... until it reaches the root directory (0) and stores all of that in an array. Since it's all done backwards, it has to be spit out in the correct order with a loop.

Maybe I'm tired, maybe there's absolutely no way of making the code anymore efficient than it is because of the way the DB is structured. Either way, I thought I'd ask if anyone had any ideas with this thing. Is there a better way of achieving the same result?

Here's the abbreviated code:

<?php
$i = 1;
$cwd = $_GET['dirid']; // 0 = Root

// Store dir tree in an array (backwards!)
while($cwd != 0){
	$querySQL = mysql_query('SELECT dirid, name FROM dirdb WHERE id="'.$cwd.'" LIMIT 1') or die(mysql_error());
	while ($resultSQL = mysql_fetch_array($querySQL) )
	{
		$pathArray[$i] = '<a href="index.php?dirid='.$cwd.'">'.$resultSQL['name'].'</a>';
		$cwd = $resultSQL['dirid'];
		$i++;
	}
}

// Spit it all out in the correct order
$fullPath = '<a href="index.php?dirid=0">Root</a>';
for($i = count($pathArray); $i != 0; $i--)
{ $fullPath .= ' &raquo; '.$pathArray[$i]; }

echo $fullPath
?>

Thanks! :)

#2 shiny_spoon

shiny_spoon
  • Members
  • PipPip
  • Member
  • 26 posts
  • LocationCanada

Posted 09 October 2006 - 03:20 PM

Bump? ???




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users