OK, first let me describe what exactly I am doing...
I have three tables in a MySQL database (for which I will use hypothetical names for simplicity):
Folders (
FolderID (primary key)
FolderName
....
);
ChildTable (
FileID (primary key)
FileName
....
);
FolderAndFileLinks (
ParentFolderID
FolderID
FileID
);
I am linking the files and the folders much like any file system for an operating system, except that folders and files can be inside multiple folders. Note that, in the links table, both FolderID and FileID cannot be assigned at the same time in one row (one is always NULL while the other refers to a file or folder, respectively)
I have a function that I use to return a hierarchial view of this structure:
function createHierarchy($ID) {
$query = mysql_query("SELECT FileID, FolderID FROM FolderAndFileLinks WHERE ParentFolderID = $ID");
if (mysql_num_rows($query) {
$Level++;
if ($ID = 0)
$Level = 0;
while ($row = mysql_fetch_assoc($query)) {
if ($row["FolderID"]) {
$Folder_array[] = $FolderID;
createHierarchy($FolderID)
}
else if ($row["FileID"]) {
$File_array[] = $FileID;
}
}
}
else
$Level--;
}
...anyway, that's my pseudo code, and it works perfect....except that it's pretty slow when there are a lot of files and folders. Obviously, this is because it makes a query for every file and folder.
A few notes: I do not want to combine the folder and file tables (not yet anyway), and I cannot remove the links table. I have too many scripts using this function all over the place that I can't afford to change the structure right now.
So, is there any better way I can be doing this, perhaps using only a small number of queries and assigning things to arrays, or maybe using OOP, or basically anything else, as long as it's faster? 😉
Thanks!