doutuo4285 2015-03-03 16:26
浏览 45
已采纳

数据库结果为两级菜单

This is result I get from database (mysql). Now I would like to get menu with structure like

department title1
- project1
- project2
department title2
- project3
- project4
....

This are my results

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

I'm using laravel 4.2.(and this will be in blade syntax) if this is any help.

Here's what I tried so far: In my controller

//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);
            }
         }

And then in my view (where I'm build the menu) I have this code

 <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条回答 默认 最新

  • doumou1864 2015-03-03 21:58
    关注

    Maybe it would be better to solve this in MySQL end, but if you want to do it after you retrieve the result, you can try something along these lines:

    The solution works under the premise that you have a result in the following structure:

    $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
        ],
    ];
    

    We initially get a unique array of department titles and we sort it alphabetically. Then, we create a $departments array which has the department titles as keys and includes the structure that we'll use.

    Then, we iterate through the $projects array and we fill the details in the appropriate fields of the $departments array.

    Finally, we sort the projects alphabetically as well.

    $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);
    

    That will produce:

    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"
        }
      }
    }
    

    You can use the $departments array to produce something similar to the following:

    <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>
    

    An alternative is to use the department IDs as keys and then sort the array alphabetically based on the title value.

    $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);
    

    That will produce:

    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"
        }
      }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 mmocr的训练错误,结果全为0
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀