数据库结果为两级菜单

时间:2015-03-03 16:26:38

标签: php mysql laravel blade

这是我从数据库(mysql)获得的结果。现在我想得到像

这样结构的菜单
department title1
- project1
- project2
department title2
- project3
- project4
....

这是我的结果

project title   |   department title    | department_id 
 project1       |   department1         |   9
 project2       |   department2         |   2
 project3       |   department3         |   1
 project11      |   department1         |   9
 project23      |   department2         |   2
 project24      |   department3         |   1
 project15      |   department1         |   9
 project26      |   department2         |   2
 project21      |   department4         |   4

我使用laravel 4.2。(这将是刀片语法)如果这是任何帮助。

这是我到目前为止所尝试的内容: 在我的控制器中

//projects is above results from db query builder
 foreach($projects as $project)
         {
            if($project->department_id != "")
            {
              $department = array(
               "id" => $project->department_id,
               "title" => $project->department_title, 
              );
              $departments_projects[] = array_push($departments_projects,$department);
            }
         }

然后在我看来(我在这里构建菜单)我有这个代码

 <ul class="sub-menu">
            @foreach($departments as $department)
            <li class="">
              <a href="javascript:;">
                {{ $department['title'] }} <i class="icon-arrow"></i>
              </a>
              <ul class="sub-menu">
                @foreach($projects as $project)
                  @if($project->department_id == $department['id'] && $project->title != "")
                    <li>
                      <a href="{{ route('department.project.show',array($project->department_id,$project->id)) }}">
                        {{ $project->title }}
                      </a>
                    </li>
                  @endif
                @endforeach

              </ul>
            </li>
            @endforeach
          </ul>

1 个答案:

答案 0 :(得分:0)

也许在MySQL端解决这个问题会更好,但是如果你想在检索结果后这样做,你可以尝试这些方法:

解决方案的前提是您有以下结构的结果:

$projects = [
    [
        'project_title' => 'project2',
        'department_title' => 'department2',
        'department_id' => 2
    ],
    [
        'project_title' => 'project1',
        'department_title' => 'department1',
        'department_id' => 9
    ],
    [
        'project_title' => 'project3',
        'department_title' => 'department3',
        'department_id' => 1
    ],
    [
        'project_title' => 'project11',
        'department_title' => 'department1',
        'department_id' => 9
    ],
    [
        'project_title' => 'project13',
        'department_title' => 'department2',
        'department_id' => 2
    ],
];

我们最初获得一系列独特的部门标题,并按字母顺序排序。然后,我们创建一个$departments数组,其中部门标题为关键字,并包含我们将使用的结构。

然后,我们遍历$projects数组,并在$departments数组的相应字段中填写详细信息。

最后,我们也按字母顺序对项目进行排序。

$department_names = array_unique(array_column($projects, 'department_title'));
sort($department_names);

$departments = array_fill_keys($department_names, [
    'id' => null,
    'projects' => []
]);

foreach($projects as $project) {
    $departments[$project['department_title']]['id'] = $project['department_id'];
    $departments[$project['department_title']]['projects'][] = $project['project_title'];
}

foreach($departments as $key => $department) {
    sort($departments[$key]['projects']);
}

var_dump($departments);

这会产生:

array(3) {
  ["department1"]=>
  array(2) {
    ["id"]=>
    int(9)
    ["projects"]=>
    array(2) {
      [0]=>
      string(8) "project1"
      [1]=>
      string(9) "project11"
    }
  }
  ["department2"]=>
  array(2) {
    ["id"]=>
    int(2)
    ["projects"]=>
    array(2) {
      [0]=>
      string(9) "project13"
      [1]=>
      string(8) "project2"
    }
  }
  ["department3"]=>
  array(2) {
    ["id"]=>
    int(1)
    ["projects"]=>
    array(1) {
      [0]=>
      string(8) "project3"
    }
  }
}

您可以使用$departments数组生成类似于以下内容的内容:

<ul>
<?php foreach($departments as $key => $department) { ?>
    <li>
        <?php echo $key; ?>
        <ul>
            <?php foreach($department['projects'] as $project) { ?>
            <li><?php echo $project; ?></li>
            <?php } ?>
        </ul>
    </li>
<?php } ?>
</ul>

另一种方法是使用部门ID作为键,然后根据标题值按字母顺序对数组进行排序。

$department_ids = array_unique(array_column($projects, 'department_id'));

$departments = array_fill_keys($department_ids, [
    'title' => null,
    'projects' => []
]);

foreach($projects as $project) {
    $departments[$project['department_id']]['title'] = $project['department_title'];
    $departments[$project['department_id']]['projects'][] = $project['project_title'];
}

foreach($departments as $key => $department) {
    sort($departments[$key]['projects']);
}

uasort($departments, function ($x, $y) {
    return strcasecmp($x['title'], $y['title']);
});

var_dump($departments);

这会产生:

array(3) {
  [9]=>
  array(2) {
    ["title"]=>
    string(11) "department1"
    ["projects"]=>
    array(2) {
      [0]=>
      string(8) "project1"
      [1]=>
      string(9) "project11"
    }
  }
  [2]=>
  array(2) {
    ["title"]=>
    string(11) "department2"
    ["projects"]=>
    array(2) {
      [0]=>
      string(9) "project13"
      [1]=>
      string(8) "project2"
    }
  }
  [1]=>
  array(2) {
    ["title"]=>
    string(11) "department3"
    ["projects"]=>
    array(1) {
      [0]=>
      string(8) "project3"
    }
  }
}