Jump to content


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


Need help optimizing this little bit of code...

Recommended Posts

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:

$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'];

// 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! :)

Share this post

Link to post
Share on other sites


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.