doushu5805 2015-04-21 15:50
浏览 17
已采纳

如何构建MySQL数据库以在父目录类型结构下显示PHP中的链接记录?

I am trying to save some Website Links into a Database table and then print them to screen with PHP.

The part I need help with is that some of the Link records will act as parent records and then child records will be under it;s parent.

The goal is to generate something like the image below but pulling the link records from a MySQL Database with PHP.

I am not sure how best to structure the database for this. AS a start I have added a column called parent in which I figured that each link record could have the ID of the Parent record.

A Parent record will act as a Folder/Directory.

enter image description here

My Databse structure as of right now...

id
title
description
url
permission
notes
active
sort_order
parent (will have the ID number of the parent folder/link of this link)

Based on this, how could I pull the records with PHP and make sure that the links stay under the parent link records like in the image?

Does my Database need to be changed to do this?

  • 写回答

2条回答 默认 最新

  • drmg17928 2015-04-21 16:25
    关注

    Actually, you database is perfect, you'll only have to work on how retrieve those informations in PHP.

    I can give you a little hint, you'll have to use a recursive function, like this one :

    function arrayLinks($parent) {
      // Your SQL query for retrieving all links with $parent
      $query = query("SELECT * FROM link WHERE parent = ".$parent);
    
      // For each row, just return another arrayLinks function
      $arrayLinks = array();
      foreach($query as $row) {
        $arrayLinks[] = array(
           'row' => $row,
           'child' => arrayLinks($row['id'])
        );
      }
      return $arrayLinks;
    }
    
    $arrayLinks = arrayLinks(0);
    

    This will give you an array like this one :

    [{
      row: {
        id: 1,
        title: 'Link 1'
      },
      child: [{
       row: {
         id: 3,
         title: 'Link 1-1'
       },
       child: []
      }, {
       row: {
        id: 4,
        title: 'Link 1-2'
       }
      }]
    }, {
      row: {
        id: 2,
        title: 'Link 2'
      },
      child: []
    }]
    

    In this exemple, you have those links :

    • Link 1
      • Link 1-1
      • Link 1-2
    • Link 2
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化