2017-09-02 49 views
2

我尝试显示特定团队中的用户。我的问题是,我可以显示所有用户,但不仅仅是具有特定的team_id。如果我认为我的网站domain.com/teams/1我只想看到用户TEAM_ID 1codeigniter foreach具有相同ID的多个条目

也许你能帮助我,我需要改变这一行

$data['users'] = $this->db->get('users')->result(); 

,我只得到来自用户team_id的用户。

我有两个SQL表

表用户

user_id username team_id 

    1  paul  1 
    2  tom  2 
    3  brad  1 
    4  pim  2 

表队

team_id teamname 

    1   team1 
    2   team1 

现在我尝试查看保罗和布拉德为球队之一,汤姆和PIM作为团队2。

控制器

public function index($id) { 
     $data = array(); 
     if ($id) { 
      $this->{$this->model}->{$this->_primary_key} = $id; 
      $this->{$this->model}->order_by['teams_id'] = 'DESC'; 


      $data['users'] = $this->db->get('users')->result(); 


      $data['item'] = $this->{$this->model}->get(); 
      $this->load->view($this->module, $data); 
      if (!$data['item']) 
       show_404(); 
     } 

    } 

视图domain.com/teams/1

<?php foreach ($users as $item): ?> 
    <?php echo $item->username ?> 
<?php endforeach ?> 

回答

0

您可以拨打其中前得到:

$this->db->where('team_id', $id); 
$data['users'] = $this->db->get('users')->result(); 
0

公共功能指数($ TEAM_ID = 0) {

$data=array(); 
if($team_id > 0) 
{ 
    $data['team_users'] = $this->Team_modal->getTeamUsers($team_id); 
} 

    $this->load->view('showTeam',$data); 

    /* 

    if you want to print data then : 
    if(count($data['team_users']) > 0){ 
      foreach($data['team_users'] as $row){ 
      echo"user_id =".$row['user_id']." username =".$row['username']." team_id =".$row['team_id']."<br>"; 
      } 
     } 
     else{ 
     echo"Oops no data for rquested team id.."; 
     } 
    */ 

}

在型号:

公共职能getTeamUsers($ team_id) {

$this->db->select('user_id,username,team_id'); 
$this->db->where('team_id',$team_id); 
return $this->db->get('table_name')->result_array(); 

}

相关问题