I am working on a media library that lets a user organize files into folders. I have a table for files that tracks the parent id of the folder it belongs to. I also have a folders table that also allows for a parent id – so a folder can be in another folder. The problem becomes when I want to show a view that lists both the files and folders that a parent folder contains. Up until this point I have taken the cheap way out and have just done 2 separate queries for the folders, then the files – and then just listed one right after the other. However, now that the lists have gotten long – I want to paginate them – so I need to LIMIT my query. How can I combine 2 queries to get one data array of files and folders I can loop through?
For example, I can do this:
SELECT *
FROM `files` LEFT JOIN `folders` ON `files`.`folder_id` = `folders`.`id`
WHERE `folders`.`id` = 198
That will give me a list of all the files in that folder.
Then this:
SELECT * FROM `folders` WHERE `parent` = 198
That will give me a list of all the child folders.
Is there a way I can combine the two to give me a single data array to loop through?
Try something like this: