2012-10-17 6 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; ?> 

これまでのところは良い、結果は道がになっている各カテゴリを返しているが、サービスは、カテゴリごとに1つのだけのサービスを返していますいくつかのカテゴリには15のサービスがあります。 誰かが私に手を差し伸べるか、説明が間違っていますか? ありがとうございます。

"私はphpやcodeigniterの専門家ではありません。私はずっと前から始まっていますので、初心者を撃ってはいけません。"

注:私はGROUP_BYとORDER_BYなしで試みたが、すべてのサービスを返しているが、カテゴリが繰り返され、

例:Group By (MySQL)を使用した場合

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

答えて

4

これは、集計関数のために良い読み物です。 簡単な説明は、次のようにSUM集計関数を使用して、この

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

ようなものです。

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

次に、ビューファイルで、カテゴリの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; ?> 
+0

男だ、あなたは、私は間違いなく、私がブログでこのことについてのチュートリアルを書きますが、私は4時間にわたり、このために探していた神と無応答に誓う、そして、伝説ですもちろん、私はあなたに言及します。本当にありがとうございます。私は本当にあなたの助けに感謝します。 – lesandru

+0

実際に私はあなたに何かを尋ねたいと思っています。 MVCを破ることはありませんか? – lesandru

+1

こんにちはアレックス、あなたはまだモデルのデータ定義を維持して以来、MVCを壊すことはありません、あなたはちょうどコントローラレベルにあるべきビューのモデルのメソッドを公開しました。私は通常ヘルパーを使用しているので、両方のコントローラ/ビューからのモデルへのすべての呼び出しはヘルパー内にあります。 – ace

関連する問題