2017-09-02 53 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(); 

}

相關問題