Skip to content

Instantly share code, notes, and snippets.

@jakewtaylor
Last active November 19, 2018 16:47
Show Gist options
  • Save jakewtaylor/255b708c748cdc8c75b2e87441fed529 to your computer and use it in GitHub Desktop.
Save jakewtaylor/255b708c748cdc8c75b2e87441fed529 to your computer and use it in GitHub Desktop.
help me pls

Folders

There is a table that contains data about a set of folders. Each folder can be a child of another. If folder_id is null, then it's a root level folder. If folder_id is a number, it is a child of the folder with that number.

id name folder_id
1 images null
2 css null
3 home 1
4 about 1
5 includes 2

Each folder is instanced as a Folder class, with its children on the folders relation:

$folder = new RootFolder(); // The root folder (id 'null').
$folder->folders // All folders that are children of root, i.e. WHERE folder_id = null

The Problem

I need to generate an array of all possible folder paths, so for the table above, something like:

[
  '/', // Root folder (id: null)
  '/images', // id: 1
  '/images/home', // id: 3
  '/images/about', // id: 4
  '/css', // id: 2
  '/css/includes', // id: 5
];

How?!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment