2012-10-17 85 views
5

我在codeigniter中有這個問題: 我嘗試從數據庫製作導航樹系統。codeigniter group_by只返回第一行

型號:

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

觀點:

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

現在到目前爲止好,結果被返回每個類別的方式是應該的,但該服務返回每個類別只有一個服務,在某些類別中有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 
.... 

回答

4

這是針對聚合函數很好看的使用Group By (MySQL)時。這樣

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

現在使用SUM聚合函數這樣 一個簡單的解釋是。

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

的結果將是:如預期,因爲它只會得到一個範疇,因爲你每個類別分組查詢

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

在您的方案,GROUP BY將無法​​正常工作。

理想的解決方案是擁有2個獨立的功能GetCategoriesGetServices

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

您認爲這些文件,你只需要對環路做的分類,然後做每個類別中的另一查詢。下面是它的將是什麼樣子的輪廓,

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

的人,你是個傳奇人物,我發誓,上帝,我用4小時尋找這一點,並沒有回答,肯定我會寫這個博客上的教程,和當然我會提你。非常感謝你,我真的很感謝你的幫助 – lesandru

+0

其實我想問你一些事情,如果不是太多,在視圖中使用模型是不是很好的做法?不打破MVC? – lesandru

+1

嗨亞歷克斯,它不會打破MVC,因爲你仍然在模型中維護你的數據定義,你只是在你的視圖中暴露模型的方法,理想情況下應該在控制器級別。我通常使用助手,所以從兩個控制器/視圖對模型的所有調用都在助手中。 – ace