Welcome Guest, Not a member yet? Register   Sign In
Confusion re loops for database results
#1

[eluser]jacobkball[/eluser]
Hi there,

A CI newbie question follows Smile

I have a page where I want to list a series of db query results, but I'm having trouble getting my head around how to actually achieve it using models, controllers and views.

What I'm trying to get is:

Category One
- Cat One SubCat One (count of items in subcat)
- Cat One SubCat Two (count of items in subcat)
- Cat One SubCat Three (count of items in subcat)

Category Two
- Cat Two SubCat One (count of items in subcat)
- Cat Two SubCat Two (count of items in subcat)
- Cat Two SubCat Three (count of items in subcat)

and I have these three functions in my model:

Code:
function get_maincat_info () {
    $query = $this->db->get('maincategories');
    return $query->result();
  }
  
  function get_subcat_info ($maincatid) {
    $query = $this->db->where('MainCatID',$maincatid);
    $query = $this->db->get('subcategories');
    return $query->result();
  }
  
  function get_item_count ($subcatid) {
    $query = $this->db->where('SubCatID',$subcatid);
    $query = $this->db->where('ShowItem','y');
    $count = $this->db->count_all_results('iteminfo');
    return $count;
  }

Obviously, a loop is needed in my view:

Code:
<?php foreach ($results as $row) { ?>
       &lt;?php echo $row->MainCatName;?&gt;<br>
    &lt;?php foreach ($subcatresults as $subcatrow) { ?&gt;
        <a href="/search/&lt;?php echo $row->MainCatLink . '/' . $subcatrow->SubCatLink; ?&gt;/" class="catlink">&lt;?php echo $subcatrow->SubCatName; ?&gt;</a>&nbsp;&nbsp;[ &lt;?php echo $countresults[0]; ?&gt; ]<br>
        &lt;?php
        }
    }
?&gt;

but I don't know how to do it so that it loops through each main category, retrieves the sub category info AND count, and returns it to the view.

I don't think I've explained it very well, so let me know what other info you need to see what I'm actually trying to do!!

Hope someone can help Smile

Cheers
Jacob


Messages In This Thread
Confusion re loops for database results - by El Forum - 06-02-2008, 10:06 PM
Confusion re loops for database results - by El Forum - 06-03-2008, 04:12 AM
Confusion re loops for database results - by El Forum - 06-03-2008, 04:59 AM



Theme © iAndrew 2016 - Forum software by © MyBB