Welcome Guest, Not a member yet? Register   Sign In
Users and Multiple Categories
#1

[eluser]derekmichaeljohnson[/eluser]
I have a users, cats (id, name) and users_cats table (user_id, cat_id) which I join together like so:

Code:
$this->db->select('users.*, users_cats.cat_id, cats.name AS cat');
$this->db->from('users');
$this->db->join('users_cats', 'users.id = users_cats.user_id','left');
$this->db->join('cats', 'users_cats.cat_id = cats.id','left');
$users = $this->db->get();

Code:
<?php foreach($users->result() as $user) : ?>
    <h3>&lt;?=$user->name?&gt;</h3>
    <p>Category(s): &lt;?=$user->cat?&gt;</p>
&lt;?php endforeach; ?&gt;

Looks like this:

Quote:John Smith
Category(s): Plumbing

Tom Brown
Category(s): Roofing

But if a user has multiple categories assigned to him in the "users_cats" table, like:

Code:
user_id   cat_id
1         4
1         7

How can I get it to show both of them, or 3 or 4 or however many there are?

Thanks!
#2

[eluser]Jondolar[/eluser]
Your result set would look something like this:
1 john smith plumbing
1 john smith roofing
2 tom brown roofing

Therefore, you need to modify your view to have an inner loop when the user_id is the same as the previous one (hint, save the user_id to a temp variable and then compare it on the next loop). Your inner loop will display the categories and the outer loop will display the name. The inner loop will only break when the user_id does not match the previous user id.
#3

[eluser]derekmichaeljohnson[/eluser]
Ah, you're right. But how will the inner loop get all the category names for that user if the next cat name won't come until the outer loop loops?

Can you show me an example?
#4

[eluser]jedd[/eluser]
Is [url="http://ellislab.com/forums/viewreply/622401/"]this message[/url] kind of what you are looking for?
#5

[eluser]Dark Preacher[/eluser]
Maybe this kind of code will help..
Create function in one of your models, "get_ucats" for example

Code:
function get_ucats()
{
  $data = array();
  $q = $this->db->get('users');
  if($q->num_rows() > 0)
  {
    foreach ($q->result_array() as $user)
    {
      $data[$user['id']]['user'] = $user['name'];
      $qc = $this->db->where('user_id', $user['id'])->get('categorys');
      if($qc->num_rows() > 0)
      {
        foreach ($qc->result_array() as $cat)
        {
          $data[$user['id']]['cats'][$cat['id']] = $cat['name'];
        }
      }
    }
  }
  return $data;
}
and then in your view use it like this

Code:
$ucats = $this->m_common->get_ucats();
foreach($ucats as $user_id => $user)
{
  if(isset($user['user']))
  {
    echo '<h1>'.anchor('user/'.$user_id, $user['user']).'</h1>';
    if(isset($user['cats']) && is_array($user['cats']))
    {
      echo 'Category(s): ';
      $count = count($user['cats']);
      $i = 2;
      foreach($user['cats'] as $cat_id => $cat)
      {
        echo anchor('category/'.$cat_id, $cat);
        echo ($i++ <= $count)?', ':'';
      }
    }
  }
  echo '<br />';
}




Theme © iAndrew 2016 - Forum software by © MyBB