# PHP+MySQL樹狀圖

## 產出MySQL

首先因為我們是要動態產出這些選單，所以我們要先長好我們的database，大概長這樣：

![](/files/-LxP_xZQBwG24LPBVwHB)

## 產出PHP

由於我是用laravel這個framework以下全部都會是laravel的code，我們現在必須先連接到那個table：

```php
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class NewClass extends Model
{

    public $table = 'newclass';
    public $primaryKey = 'id';
    public $incrementing = true;
    public $timestamps = false;

}
```

再來產出抓取這個資料庫資料的repository：

```php
<?php
namespace App\Repositories;
use Illuminate\Http\Request;
use App\Models\NewClass;
use Carbon\Carbon;

class NewClassRepository {
    /**
   * 注入的 NewClass model
   * @var type
   */
  protected $model;
  public function __construct(NewClass $model) {
    $this->model = $model;
  }

  public static function withNew() {
    return new NewClassRepository(new NewClass());
  }

    public function getMainData() {
    return $this->model->get();
  }
}
```

再來就是重頭戲controller的部份：

```php
<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use App\Repositories\NewClassRepository;

class TestClassController extends Controller {
    protected $repository;
  public function __construct(NewClassRepository $repository) {
      $this->repository = $repository;
  }

  private static $array; // 從DB查詢出的所有分類資料
  private static $str = ""; // 串接字串用

  public function classData() {
    // 到repository抓資料
    $result = $this->repository->getMainData();

    foreach($result as $data){
      TestClassController::$array[] = array($data->id,$data->f_id,$data->name);
    }

    TestClassController::$str = $this->classMenu();
    $re = TestClassController::$str;
    return View('test',compact('re'));
  }

  /**
   * 編輯字串要回傳到view的
   * @param  integer $f_id [父層]
   */
  public function classMenu($f_id =0){
    for($i=0;$i<count(TestClassController::$array);$i++){

      if(TestClassController::$array[$i][1]==$f_id){
        TestClassController::$str = TestClassController::$str."<ul>";

        TestClassController::$str = TestClassController::$str."<li>";
        // 列出它的名稱
        TestClassController::$str = TestClassController::$str.TestClassController::$array[$i][2];
        TestClassController::$str = TestClassController::$str."</li>";
        // 順便找它的子層直接列出來
        $this->classMenu(TestClassController::$array[$i][0]);

        TestClassController::$str = TestClassController::$str."</ul>";
      }
    }
    return TestClassController::$str;
  }
}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://tobyisme.gitbook.io/php/php+mysql.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
