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 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了