如何在沒有$ id的情況下對一些信息進行排序?我的問題是,我可以閱讀所有的團隊和用戶。但我怎麼能解決這個問題? 爲了讓我在一個盒子裏得到一支球隊和來自這支球隊的球員。用我的代碼,我有所有用戶在所有的箱子。
我可以foreach在foreach嗎?
觀點是什麼我嘗試:
<?php foreach ($teams as $item): ?>
<div class="col-xs-12">
<?php echo $item->teamname ?>
</div>
<?php foreach ($users as $item): ?>
<div class="col-xs-12">
<?php echo $item->username ?>
</div>
<?php endforeach ?>
<?php endforeach ?>
這是我的數據庫
表用戶
user_id username team_id
1 paul 1
2 tom 2
3 brad 1
4 pim 2
表隊
team_id teamname
1 team1
2 team1
現在我有一個像不退domaincom /團隊 ,我想在這一側
隊1:保羅,布拉德 隊2:湯姆,PIM
控制器:
<?php
class Teams extends CI_Controller {
public $layout = 'full';
public $module = 'teams';
public $model = 'Teams_model';
public function __construct() {
parent::__construct();
$this->load->model($this->model);
$this->_primary_key = $this->{$this->model}->_primary_keys[0];
}
public function index()
{
$data['teams'] = $this->{$this->model}->get_teams();
$data['users'] = $this->{$this->model}->get_users();
$data['teamsWithUsers'] = $this->{$this->model}->get_users_by_team();
$data['items'] = $this->{$this->model}->get();
$this->load->view($this->module, $data);
}
}
這是我的模型
<?php
class Teams_model extends CI_model
{
public $_table = 'teams';
public $_primary_keys = array('teams_id');
function get_teams()
{
return $this->db->get('teams')->result();
}
function get_users()
{
return $this->db->get('users')->result();
}
function get_users_by_team($teamID = null)
{
$this->db->select('u.user_id, u.username, u.team_id, t.teamname');
$this->db->from('users AS u');
$this->db->join('teams AS t', 't.team_id = u.team_id');
if ($teamID != '') {
$this->db->where('t.team_id', $teamID);
}
$this->db->order_by('t.teamname', 'ASC');
$this->db->order_by('u.username', 'ASC');
$query = $this->db->get();
return $query->result();
}
}
我尋找是這個視圖樣本
嘿顯示你的記錄,謝謝你,但它沒有工作爲了我。我試着在團隊名下查看用戶。我擁有team_id(1)> next teamname>具有此team_id的用戶的teamname>用戶可能(3)。 – Gregor