shiny_spoon Posted October 7, 2006 Share Posted October 7, 2006 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 .= ' » '.$pathArray[$i]; }echo $fullPath?>[/code]Thanks! :) Quote Link to comment Share on other sites More sharing options...
shiny_spoon Posted October 9, 2006 Author Share Posted October 9, 2006 Bump? ??? Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.