codeigniter group_by只返回第一行 [英] codeigniter group_by returning only first row

查看:155
本文介绍了codeigniter group_by只返回第一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在codeigniter中遇到了这个问题:
我尝试从数据库中创建导航树系统。



model:

  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');
$ b $ if if($ query-> result()== TRUE)
{
foreach($ query-> result_array()as $ row)
{
$ result [] = $ row;
}
返回$ result;






view:

 <?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']); ?>< /锂>
< / ul>
< / li>
< / ul>
<?php endforeach; ?>
<?php endif; ?>

现在到目前为止非常好,结果是按照方式应该返回每个类别,但服务每个类别只返回一项服务,而在某些类别中则有15项服务。
任何人都可以帮我一把,或者解释一下是怎么回事?
非常感谢。



我不是php或codeigniter的专家,我刚开始不久,所以请不要拍



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



例如:

 类别-a 
service1
类别-a
服务2
类别-b
服务10
类别-b
服务11
类别-c
服务30
类别-c
服务31
....


解决方案

这是一个当使用分组依据(MySQL)
简单的解释就是这样

  Date |金额
2012-01-01 | 5
2012-01-01 | 6
2012-01-02 | 1
2012-01-02 | 6

现在使用SUM聚合函数。

  SELECT日期,SUM(金额)作为总计FROM表GROUP BY日期ORDER BY DATE; 

结果将会是:

  Date |金额
2012-01-01 | 11
2012-01-02 | 7

在您的场景中, GROUP BY 没有按预期工作,因为它只会得到一个类别,因为你按每个类别分组查询。



理想的解决方案是有两个独立的函数, 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');
$ b $ if if($ query-> result()== TRUE)
{
foreach($ query-> result_array()as $ row)
{
$ result [] = $ row;
}
返回$ result;



函数getCategories()
{
$ this-> db-> select('category_id,category_title');
$ this-> db-> order_by('category_title','ASC');
$ query = $ this-> db-> get('services_category');
$ b $ if if($ query-> result()== TRUE)
{
foreach($ query-> result_array()as $ row)
{
$ result [] = $ row;
}
返回$ result;






$ b然后在你的视图文件中,你只需要做类别上的for循环然后为每个类别执行另一个查询。以下是它的外观概况,

 <?php if(isset($ categories)&& $类别):foreach($ categories作为$ category):?> 
< ul>
< li>< a href =#><?php echo $ category ['category_title']?>< / a>
< ul>
//如何获得$ services类似于如何获得$ 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']); ?>< /锂>
<?php endforeach; ?>
< / ul>
< / li>
< / ul>
<?php endforeach; ?>
<?php endif; ?>


i have this problem in codeigniter: I try to make a navigation tree system from database.

model:

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

view:

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

now so far so good, the result is returning each category the way is supposed to, but the service is returning only one service per category, and in some categories there is like 15 services. Anybody kind to give me a hand, or an explanation what is going wrong ? Thank you so much.

"i'm not an expert in php or codeigniter, i just started not long time ago, so please don't shoot the beginner."

note: i tried without the group_by and order_by, and is returning all services, but the categories are repeating,

ex:

category-a
   service1
category-a
   service2
category-b
   service10
category-b
   service11
category-c
   service30
category-c
  service31
....

解决方案

This is a good read for aggregate functions when using Group By (MySQL). A simple explanation would be like this

Date        | Amount
2012-01-01  | 5
2012-01-01  | 6
2012-01-02  | 1
2012-01-02  | 6

Now using SUM aggregate function like this.

SELECT Date, SUM(Amount) as Total FROM table GROUP BY Date ORDER BY DATE;

The result will be:

Date        | Amount
2012-01-01  | 11
2012-01-02  | 7

In your scenario, GROUP BY will not work as intended as it will only get one Category since you grouped your query by each category.

The ideal solution to this is to have 2 separate functions, GetCategories and 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;
  }
}

Then in your view file, you just need to do a for loop on the Categories then do another query for each Category. Here's an outline of what it will look like,

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

这篇关于codeigniter group_by只返回第一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆