Welcome Guest, Not a member yet? Register   Sign In
Users and Multiple Categories
#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 />';
}


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



Theme © iAndrew 2016 - Forum software by © MyBB