• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
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!


Messages In This Thread
Users and Multiple Categories - by El Forum - 08-21-2009, 11:30 AM
Users and Multiple Categories - by El Forum - 08-21-2009, 01:47 PM
Users and Multiple Categories - by El Forum - 08-21-2009, 02:36 PM
Users and Multiple Categories - by El Forum - 08-21-2009, 03:46 PM
Users and Multiple Categories - by El Forum - 08-21-2009, 04:02 PM

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.