我嘗試顯示特定團隊中的用戶。我的問題是,我可以顯示所有用戶,但不僅僅是具有特定的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 ?>