【问题标题】:Cakephp combine columns with the same name or groupCakephp 组合具有相同名称或组的列
【发布时间】:2015-01-08 16:59:50
【问题描述】:

我正在使用 CakePHP 作为我项目的框架。在这里,我有一个 Group 和 Name 列。我一直在尽力将数据与同一组结合起来。

我在控制器中的代码是这样的:

> $this->Custom->Rental->find('all', array('Pool' => 'name'));
> $this->set('rents', $this->Paginator->paginate('Rental'));

这是我当前的表..

Group                          Name
1                              A
2                              D
1                              W
3                              C

这就是我想要做的:

Group                          Name
1                              A
                               W
2                              D
3                              C

如果他们有相同的号码,它将只显示一个组号。我很抱歉这个菜鸟问题。我已经研究了 2 天,但我找不到解决方案。

【问题讨论】:

  • 如果我没听错,只需使用 SQL group by

标签: sql cakephp merge cakephp-2.0


【解决方案1】:

基本思路:

控制器

$this->Paginator->order = 'Rental.group_id ASC';
$this->set('rents', $this->Paginator->paginate('Rental'));

查看

<?php
 $group = '';
 foreach ($rents AS $rent) { 
   if($group != $rent['Rental']['group_id']) {
      echo $rent['Rental']['group_id'];
   }  
   echo $rent['Rental']['name'];
   $group = $rent['Rental']['group_id']; 
 } ?>

【讨论】:

    猜你喜欢
    • 2022-07-25
    • 2013-06-06
    • 2023-02-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-22
    • 2020-01-22
    • 1970-01-01
    相关资源
    最近更新 更多