如何从MySQL数据库记录中生成PHP数组以实现多级文件夹结构

ocebsuys  于 2024-01-05  发布在  Mysql
关注(0)|答案(1)|浏览(151)

我想用PHP和MySQL构建一个Evernote风格的笔记应用程序。
Evernote允许顶级文件夹中包含笔记记录。
我想让我的应用程序允许文件夹内的文件夹,使顶级文件夹可以有一个子文件夹,然后有注意到记录内的子文件夹。
就像下面这张图片:


的数据
我有这个PHP函数,它将建立在上面的图像所示的结构菜单.
这段代码构建了如下菜单:

  1. <?php
  2. function tree($array, $parent, $parts = array(), $step = 0) {
  3. if (!count($array)) {
  4. return '';
  5. }
  6. $tid = ($step == 0) ? 'id="tree"' : '';
  7. $t = '<ul class="unstyled" '.$tid.'>';
  8. foreach ($array as $key => $item) {
  9. if (is_array($item)) {
  10. $open = $step !== false && (isset($parts[$step]) && $key == $parts[$step]);
  11. $t .= '<li class="directory'. ($open ? ' open' : '') .'">';
  12. $t .= '<a href="#" data-role="directory"><i class="glyphicon glyphicon-folder-'. ($open ? 'open' : 'close') .'"></i>&nbsp; ' . $key . '</a>';
  13. $t .= tree($item, "$parent/$key", $parts, $open ? $step + 1 : false);
  14. $t .= '</li>';
  15. } else {
  16. $selected = (isset($parts[$step]) && $item == $parts[$step]);
  17. $t .= '<li class="file'. ($selected ? ' active' : '') .'"><a href="'. $parent .'/'. $item . '">'.$item.'</a></li>';
  18. }
  19. }
  20. $t .= '</ul>';
  21. return $t;
  22. }
  23. ?>

字符串
上面的代码通过调用_getTree()构建树菜单

  1. protected function _getTree($dir = LIBRARY)
  2. {
  3. $return = array('directories' => array(), 'files' => array());
  4. $items = scandir($dir);
  5. foreach ($items as $item) {
  6. if(preg_match($this->_ignore, $item)) {
  7. if($this->_force_unignore === false || !preg_match($this->_force_unignore, $item)) {
  8. continue;
  9. }
  10. }
  11. $path = $dir . DIRECTORY_SEPARATOR . $item;
  12. if (is_dir($path)) {
  13. $return['directories'][$item] = $this->_getTree($path);
  14. continue;
  15. }
  16. $return['files'][$item] = $item;
  17. }
  18. uksort($return['directories'], "strnatcasecmp");
  19. uksort($return['files'], "strnatcasecmp");
  20. return $return['directories'] + $return['files'];
  21. }


上面的_getTree()通过扫描服务器上的目录来查找文件夹和文件,以下面所示的格式构建一个数组。然后将该数组传递给tree()。我的目标是让一个函数构建这个相同风格的数组,但来自MySQL数据库文件夹和笔记记录。
我需要写一个函数,可以从两个数据库表构建如下数组。
我的数据库表将是

*notebooks,列名parent表示父文件夹。如果笔记本是父笔记本的子文件夹,则笔记本ID将进入父字段。0 ==父级文件夹。
*notesparent列链接到笔记本ID。

代码:

  1. Array(
  2. [top_level_notebook_1] => Array(
  3. [child_note_of_parent_notebook_1.html] => some_note.html
  4. [child_level_notebook_1] => Array(
  5. [note_1.html] => some_note.html
  6. )
  7. [child_level_notebook_2] => Array(
  8. [note_2] => cache.js
  9. [note_3] => cache.js.md
  10. [note_4] => Confirmation-Dialog.js
  11. )
  12. [child_level_notebook_3] => Array(
  13. [crud.php] => crud.php
  14. [error2mysql.php] => error2mysql.php
  15. [upload_file_from_remote_url.php] => upload_file_from_remote_url.php
  16. )
  17. )
  18. [top_level_notebook_2] => Array(
  19. [note_7.html] => some_note.html
  20. )
  21. [root_level_note.html] => Dev_Bookmarks.html
  22. )

qvk1mo1f

qvk1mo1f1#

除非你想让你的查询变得非常复杂,否则我会沿着获取所有记录并用你的脚本构建一个数组的路线做更多的事情。类似于这样:
脚本现在包括数据库的东西(更新):

  1. //Change these credentials!
  2. $mysqli = new mysqli("localhost", "my_user", "my_password", "world");
  3. if ($mysqli->connect_errno) {
  4. printf("Connect failed: %s\n", $mysqli->connect_error);
  5. exit();
  6. }
  7. $query = "SELECT nb.`name` AS 'notebook', no.`name` AS 'note', (SELECT `name` FROM `notebooks` WHERE `id` = nb.`parent`) AS 'nbparent', no.`parent` AS 'noparent' FROM `notebooks` nb RIGHT JOIN `notes` no ON no.`parent` = nb.`id` WHERE (nb.`active` = 1 OR nb.`active` IS NULL) AND (no.`active` = 1 OR no.`active` IS NULL)";
  8. $result = $mysqli->query($query);
  9. $myArray = array();
  10. while($row = $result->fetch_assoc()){
  11. if($row['nbparent']){
  12. if($row['note']) $myArray[$row['nbparent']][$row['notebook']][] = $row['note'];
  13. } else {
  14. if($row['note']) {
  15. if($row['notebook']) $myArray[$row['notebook']][] = $row['note'];
  16. else $myArray[] = $row['note'];
  17. }
  18. }
  19. }
  20. var_dump($myArray);

字符串
这应该可以工作!我在本地开发环境中测试了它,并得到了一组测试数据的结果:

  1. array (size=4)
  2. 'Parent Notebook1' =>
  3. array (size=6)
  4. 0 => string 'Note1' (length=5)
  5. 1 => string 'Note2' (length=5)
  6. 2 => string 'Note5' (length=5)
  7. 'Child Notebook1' =>
  8. array (size=1)
  9. 0 => string 'Note6' (length=5)
  10. 'Child Notebook2' =>
  11. array (size=1)
  12. 0 => string 'Note7' (length=5)
  13. 'Child Notebook3' =>
  14. array (size=1)
  15. 0 => string 'Note8' (length=5)
  16. 'Parent Notebook2' =>
  17. array (size=1)
  18. 0 => string 'Note3' (length=5)
  19. 'Parent Notebook3' =>
  20. array (size=1)
  21. 0 => string 'Note4' (length=5)
  22. 0 => string 'Note With No Parent :(' (length=22)


这样做的目的是循环遍历每个结果。当然,你需要将列名(比如在$row 'notebook']中)更改为实际列的名称。它将在一级深度工作。如果你需要更多,你必须调整一下。这是总体思路,但是,应该从你的结构中构建一个数组。

快速说明

上面的代码不会是严格友好的。如果你关心警告,上面的代码会给你给予关于未定义键的警告。它会为你创建它们,因为我们都对PHP爱恨交加。如果你想避免键警告,你可以很容易地用if(array_key_exists())弹出一些键检查。

潜在的'Gotcha'

这适用于名称。如果您允许“notebooks”使用相同的名称,我建议您更进一步,通过id而不是基于文本的列(如notebook的标题)构建数组。从那里,您需要在每个notebook中添加一个新的键。我复制了您在制作此脚本时在示例中提供的数组,但我确实想绝对肯定你理解这种方法所涉及的潜在(令人沮丧的)“gotcha”。
让我知道如果你有任何问题,希望这能有所帮助。

展开查看全部

相关问题