I am wanting to build an Evernote style note app with PHP and MySQL.
Evernote allows top level folders with note records in them.
I would like to make my app allow folders to be inside of folders so a top level folder can have a child folder which then has note records inside of the child folder.
Like this image below:
I have this PHP function which will build the structure menu shown in the image above...
This code build the menu shown
<?php
function tree($array, $parent, $parts = array(), $step = 0) {
if (!count($array)) {
return '';
}
$tid = ($step == 0) ? 'id="tree"' : '';
$t = '<ul class="unstyled" '.$tid.'>';
foreach ($array as $key => $item) {
if (is_array($item)) {
$open = $step !== false && (isset($parts[$step]) && $key == $parts[$step]);
$t .= '<li class="directory'. ($open ? ' open' : '') .'">';
$t .= '<a href="#" data-role="directory"><i class="glyphicon glyphicon-folder-'. ($open ? 'open' : 'close') .'"></i> ' . $key . '</a>';
$t .= tree($item, "$parent/$key", $parts, $open ? $step + 1 : false);
$t .= '</li>';
} else {
$selected = (isset($parts[$step]) && $item == $parts[$step]);
$t .= '<li class="file'. ($selected ? ' active' : '') .'"><a href="'. $parent .'/'. $item . '">'.$item.'</a></li>';
}
}
$t .= '</ul>';
return $t;
}
?>
The code above build the tree menu by calling _getTree()
protected function _getTree($dir = LIBRARY)
{
$return = array('directories' => array(), 'files' => array());
$items = scandir($dir);
foreach ($items as $item) {
if(preg_match($this->_ignore, $item)) {
if($this->_force_unignore === false || !preg_match($this->_force_unignore, $item)) {
continue;
}
}
$path = $dir . DIRECTORY_SEPARATOR . $item;
if (is_dir($path)) {
$return['directories'][$item] = $this->_getTree($path);
continue;
}
$return['files'][$item] = $item;
}
uksort($return['directories'], "strnatcasecmp");
uksort($return['files'], "strnatcasecmp");
return $return['directories'] + $return['files'];
}
_getTree() above builds an array in the format shown below by scanning directories on server looking for folders and files. It then passes that array into tree(). My goal is to make a function build this same style array but from MySQL Database folder and note records.
I need to write a function that could build array like below from two database tables.
MY database tables will be
- notebooks with column name
parentto denote the parent folder. THe notebook ID will go into parent field if the notebook is a child folder of a parent notebook. 0 == parent level folder. - notes with
parentcolumn to be linked to ID of notebook.
Code:
Array(
[top_level_notebook_1] => Array(
[child_note_of_parent_notebook_1.html] => some_note.html
[child_level_notebook_1] => Array(
[note_1.html] => some_note.html
)
[child_level_notebook_2] => Array(
[note_2] => cache.js
[note_3] => cache.js.md
[note_4] => Confirmation-Dialog.js
)
[child_level_notebook_3] => Array(
[crud.php] => crud.php
[error2mysql.php] => error2mysql.php
[upload_file_from_remote_url.php] => upload_file_from_remote_url.php
)
)
[top_level_notebook_2] => Array(
[note_7.html] => some_note.html
)
[root_level_note.html] => Dev_Bookmarks.html
)
