Codeigniter foreach没有$ id

时间:2017-09-15 07:57:41

标签: php codeigniter

如何在没有$ id的情况下对某些信息进行排序?我的问题是,我可以阅读所有团队和用户。但我怎么能这个呢? 这样我就能让一支球队和这支球队的球员放在一个盒子里。在我的代码中,我拥有所有框中的所有用户。

我可以在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 / teams这样的消退 而我想在这方面

队伍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

3 个答案:

答案 0 :(得分: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;

答案 1 :(得分: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

答案 2 :(得分: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 ?>

我不确定那是不是你想要实现的目标