codeigniter group_by仅返回第一行

les*_*dru 6 group-by codeigniter

我在codeigniter中遇到这个问题:我尝试从数据库制作导航树系统.

模型:

function getServices()
{
 $this->db->select('service_url, service_title, category_title');    
 $this->db->join('services_category', 'services_category.id=services.category_id');    
 $this->db->group_by('category_title');
 $this->db->order_by('service_title', 'ASC');    
 $query = $this->db->get('services');

 if($query->result() == TRUE)    
 {    
    foreach($query->result_array() as $row)
    {
       $result[] = $row;
    }
    return $result;
  }
}
Run Code Online (Sandbox Code Playgroud)

视图:

<?php if(isset($services) && $services) : foreach($services as $s) : ?>    
   <ul>
     <li><a href="#"><?php echo $s['category_title'] ?></a>    
       <ul>
        <li><?php echo anchor('services/' . $s['service_url'], $s['service_title']); ?></li>
       </ul>    
     </li>    
   </ul>    
<?php endforeach; ?>    
<?php endif; ?>
Run Code Online (Sandbox Code Playgroud)

现在到目前为止一直很好,结果是按照预期的方式返回每个类别,但是服务每个类别只返回一个服务,而在某些类别中有15个服务.有人帮我一把,或解释出了什么问题?非常感谢.

"我不是php或codeigniter的专家,我刚开始不久,所以请不要拍初学者."

注意:我试过没有group_by和order_by,并且正在返回所有服务,但类别正在重复,

例如:

category-a
   service1
category-a
   service2
category-b
   service10
category-b
   service11
category-c
   service30
category-c
  service31
....
Run Code Online (Sandbox Code Playgroud)

ace*_*ace 5

对于使用Group By(MySQL)的聚合函数,这是一本不错的书。一个简单的解释就是这样

Date        | Amount
2012-01-01  | 5
2012-01-01  | 6
2012-01-02  | 1
2012-01-02  | 6
Run Code Online (Sandbox Code Playgroud)

现在使用像这样的SUM聚合函数。

SELECT Date, SUM(Amount) as Total FROM table GROUP BY Date ORDER BY DATE;
Run Code Online (Sandbox Code Playgroud)

结果将是:

Date        | Amount
2012-01-01  | 11
2012-01-02  | 7
Run Code Online (Sandbox Code Playgroud)

在您的方案中,GROUP BY由于按每个类别对查询进行了分组,因此只能获得一个类别,因此将无法按预期工作。

理想的解决方案是具有2个单独的功能,GetCategories并且GetServices

function getServices($category_id = false)
{
  $this->db->select('service_url, service_title, category_title');
  $this->db->join('services_category', 'services_category.id=services.category_id');
  if ($category_id !== false)
    $this->db->where('services.category_id', $category_id);
  $this->db->order_by('service_title', 'ASC');
  $query = $this->db->get('services');

  if($query->result() == TRUE)
  {
    foreach($query->result_array() as $row)
    {
      $result[] = $row;
    }
    return $result;
  }
}

function getCategories()
{
  $this->db->select('category_id, category_title');
  $this->db->order_by('category_title', 'ASC');
  $query = $this->db->get('services_category');

  if($query->result() == TRUE)
  {
    foreach($query->result_array() as $row)
    {
      $result[] = $row;
    }
    return $result;
  }
}
Run Code Online (Sandbox Code Playgroud)

然后,在视图文件中,您只需要对类别执行一个for循环,然后对每个类别进行另一个查询。这是它的外观概述

<?php if(isset($categories) && $categories) : foreach($categories as $category) : ?>
  <ul>
    <li><a href="#"><?php echo $category['category_title'] ?></a>
      <ul>
        // how you get $services is similar to how you get $categories
        <?php $services = $this->modelname->getServices($category['category_id']);
        <?php foreach($services as $s) : ?>
          <li><?php echo anchor('categories/' . $s['service_url'], $s['service_title']); ?></li>
        <?php endforeach; ?>
      </ul>
    </li>
  </ul>
<?php endforeach; ?>
<?php endif; ?>
Run Code Online (Sandbox Code Playgroud)