2017-09-15 29 views
0

如何在沒有$ 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(); 
    } 
} 

我尋找是這個視圖樣本

enter image description here

回答

1

什麼有關呢?

控制器

public function index() 
{ 
    $data['teams'] = $this->{$this->model}->get_teams(); 
    $data['users'] = $this->{$this->model}->get_users(); 

    $arrUsersPerTeam = []; 

    foreach($data['users'] AS $objUser) 
    { 
     $arrUsersPerTeam[$objUser->team_id][] = $objUser; 
    } 

    $data['arrUsersPerTeam'] = $arrUsersPerTeam; 

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

和你的看法

<?php 
foreach ($teams as $item): 
?> 
    <div class="col-xs-12"> 
     <?php echo $item->teamname ?> 
    </div> 
    <?php 
    if (isset($arrUsersPerTeam[$item->team_id])) : 
     foreach ($arrUsersPerTeam[$item->team_id] as $objUser): 
    ?> 
     <div class="col-xs-12"> 
      <?php echo $objUser->username ?> 
     </div> 
    <?php 
     endforeach; 
    endif; 
endforeach; 
0

2件事:你可以從foreach中挑選出關鍵字,以後可以使用,其次「$ item」被用於2個可能意外覆蓋它的地方。

<?php foreach ($teams as $key => $team_item): ?> 
    <div class="col-xs-12"> 
    <?php echo $team_item->teamname ?> 
    </div> 
    <?php foreach ($users as $user_item): ?> 
     <?php if($item->team_id == $key): ?> 
     <div class="col-xs-12"> 
      <?php echo $user_item->username ?> 
     </div> 
     <?php endif ?> 
    <?php endforeach ?> 
<?php endforeach ?> 

我真的不知道,如果這就是你正在努力實現

+0

嘿顯示你的記錄,謝謝你,但它沒有工作爲了我。我試着在團隊名下查看用戶。我擁有team_id(1)> next teamname>具有此team_id的用戶的teamname>用戶可能(3)。 – Gregor

1

你應該使用一個單一的連接查詢,讓您的結果,確保結果是由團隊

/**model*/ 

function some func(){ 

    $this->db->select('u.username, t.team_id, t.teamname'); 
    $this->db->from('team AS t'); 
    $this->db->join('users AS u', 't.team_id = u.team_id'); 
    $this->db->order_by('t.team_id', 'ASC'); 
    $query = $this->db->get(); 
    return $query->result(); 
} 

下令在你控制器收集由您的模型中定義的上述方法返回的結果並傳遞給查看。

鑑於沒有必要對嵌套循環只有單迴路會在這裏做的工作就像$results包含了所有的記錄,然後在視圖中,可以作爲

/* view */ 
$parent = false; 
$index = 1; 
<div class="col-xs-12"> 
    <?php foreach ($results as $result){ ?> 
     <?php if($parent !=$item->teamname){ ?> 
       <?php if($index !=1){ ?> 
        </div> 
       <?php } ?> 
        <div class="col-xs-12"> 
        <h2><?php echo $result->teamname ?></h2> 
     <?php } ?> 

    <p><?php echo $result->username ?></p> 
    <?php 
    $parent =$result->teamname; 
    $index++; 
    } ?> 
</div> // To close the opened div