如何在Codeigniter中将对象附加到数组的末尾

时间:2018-10-01 05:21:01

标签: codeigniter-3 array-push

我正在使用codeigniter3 我创建了几个模型,即admin,具有一个视图的用户。我从模型中获取用户列表值并将其打印为列表。但是它只显示数组的最后一个元素。 我的代码在下面

<?php 
class adminmodel extends CI_Model{
    /**
     * 
     * @throws Exception
     */
    public function userslist(){
        try{
            $userlist = $this->getUserListWithDetails();
            if(empty($userlist)) {
                  throw new Exception('no data returne');
            }
        }catch(Exception $e){
            var_dump($e->getMessage());
        }
        return  $userlist;
    }
    /**
     * 
     * @return type
     */
    public function getUserListWithDetails(){

        try{

            $this->db->select('*');
            $this->db->from('users');
            $this->db->where('users.uid!=',1);
            $query = $this->db->get();
            $rows=$query->result_array();

            if($query->num_rows()){
                return $this->getUserObjectListFromDataSet($rows);  
            }
        }catch(Exception $e){
            var_dump($e->getMessage());  
        } 

    }
    /**
     * 
     * @param type $rows
     */
    public function getUserObjectListFromDataSet($rows){

        $userList = array();

        if($rows) {
            foreach ($rows as $row) {
                $uid = $row['uid'];
                $this->load->model('usermodel','user');
                $user=$this->user;
                //Get User Roles
               /* $this->db->select('*');
                $this->db->from('role r');
                $this->db->join('users_roles', 'r.rid = users_roles.rid','left');
                $this->db->where('users_roles.uid',$uid);
                $query=$this->db->get();
                $roles = $query->result_array();
                $this->load->model('rolemodel','role');*/
                $user->setUid($row['uid']);
                $user->setUsername($row['username']);
                //=======================================
                array_push($userList,$this->user);//here is issue

            }

            return $userList;    
        }else{
            return null;
        }
    }

}
?>

MY user model
<?php

class Usermodel extends CI_Model{

    protected $uid;
    protected $username;

    /**
     * @return mixed
     */
    public function getUid()
    {
        return $this->uid;
    }

    /**
     * @param mixed $uid
     */
    public function setUid($uid)
    {
        $this->uid = $uid;
    }

}

我在视图中使用$ userList就像

<div class="box-body">
            <?php if($error=$this->session->flashdata('feedback')):?>
      <div class='row'>
        <?=$error;?>
      </div>
      <?php endif;?>
              <table id="example2" class="table table-bordered table-hover">
                <thead>
                <tr>
                  <th>Sr.No.</th>
                  <th>Username</th>
                  <th>Status</th>
                  <th>Roles</th>
                  <th>Operations</th>
                </tr>
                </thead>
                <tbody>
            <?php 
            //echo "<pre>";print_r($users);echo "</pre>";
            if(count($users)>1): ?>
            <?php $slNo = 1;
            foreach($users as $user):?>
                  <tr>
                  <td><?=$slNo++;?></td>
                  <td><?=$user->getUsername();?></td>
                  <td><?=$user->getUid();?></td>
                  <td>
                    <?php /*
                                    if ($user->getRoles()) {
                                        foreach ($user->getRoles() as $userRole) {
                                          echo $userRole->getRoleName() . '<br/>';
                                        }
                                    }*/
                                    ?>  


                  </td>
                  <td>X</td>
                </tr> 
        <?php endforeach;?>
                <?php else:?>
                <tr>
                    <td colspan="4">No data avilable.</td>
                </tr>
                    <?php endif;?>
                </tbody>
                <tfoot>
                 <tr>
                  <th>Sr.No.</th>
                  <th>Username</th>
                  <th>Status</th>
                  <th>Roles</th>
                  <th>Operations</th>
                </tr></tfoot>
              </table>
            </div>

但是它仅显示最后一个数组值。 请给我建议问题出在哪里以及如何解决这个问题。

0 个答案:

没有答案