Jump to content

Need help optimizing this little bit of code...


shiny_spoon

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:

[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
?>[/code]

Thanks! :)
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.