Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This is a good read for aggregate functions when using <a href="http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html" rel="nofollow">Group By (MySQL)</a>. A simple explanation would be like this</p> <pre><code>Date | Amount 2012-01-01 | 5 2012-01-01 | 6 2012-01-02 | 1 2012-01-02 | 6 </code></pre> <p>Now using SUM aggregate function like this.</p> <pre><code>SELECT Date, SUM(Amount) as Total FROM table GROUP BY Date ORDER BY DATE; </code></pre> <p>The result will be:</p> <pre><code>Date | Amount 2012-01-01 | 11 2012-01-02 | 7 </code></pre> <p>In your scenario, <code>GROUP BY</code> will not work as intended as it will only get one Category since you grouped your query by each category.</p> <p>The ideal solution to this is to have 2 separate functions, <code>GetCategories</code> and <code>GetServices</code>.</p> <pre><code>function getServices($category_id = false) { $this-&gt;db-&gt;select('service_url, service_title, category_title'); $this-&gt;db-&gt;join('services_category', 'services_category.id=services.category_id'); if ($category_id !== false) $this-&gt;db-&gt;where('services.category_id', $category_id); $this-&gt;db-&gt;order_by('service_title', 'ASC'); $query = $this-&gt;db-&gt;get('services'); if($query-&gt;result() == TRUE) { foreach($query-&gt;result_array() as $row) { $result[] = $row; } return $result; } } function getCategories() { $this-&gt;db-&gt;select('category_id, category_title'); $this-&gt;db-&gt;order_by('category_title', 'ASC'); $query = $this-&gt;db-&gt;get('services_category'); if($query-&gt;result() == TRUE) { foreach($query-&gt;result_array() as $row) { $result[] = $row; } return $result; } } </code></pre> <p>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,</p> <pre><code>&lt;?php if(isset($categories) &amp;&amp; $categories) : foreach($categories as $category) : ?&gt; &lt;ul&gt; &lt;li&gt;&lt;a href="#"&gt;&lt;?php echo $category['category_title'] ?&gt;&lt;/a&gt; &lt;ul&gt; // how you get $services is similar to how you get $categories &lt;?php $services = $this-&gt;modelname-&gt;getServices($category['category_id']); &lt;?php foreach($services as $s) : ?&gt; &lt;li&gt;&lt;?php echo anchor('categories/' . $s['service_url'], $s['service_title']); ?&gt;&lt;/li&gt; &lt;?php endforeach; ?&gt; &lt;/ul&gt; &lt;/li&gt; &lt;/ul&gt; &lt;?php endforeach; ?&gt; &lt;?php endif; ?&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload