2017-04-03 70 views
0

我想要做的是将所有用户和与它们有关的数据的列表添加到使用PHP foreach的表行中,获得此信息来自数据库。我与这个越来越的问题是以下错误:

[Severity: Error Message: Call to a member function result_array() on array]

我读了similar question关于这一点,但它似乎并没有解决我的问题,因为我然后得到错误的众多试图让一个做当从模型或控制器中删除result_array()部件时,从非对象获取属性。

下面是user.php的型号:

Class User extends CI_Model 
{ 
    function populateUsers() 
    { 
     $this->db->select('username, accessLevel, fullName'); 
     $this->db->from('users'); 

     $userquery = $this->db->get(); 

     if($userquery -> num_rows()>=1) 
     { 
      return $userquery->result_array(); 
     } 
     else 
     { 
      return false; 
     } 

    } 

} 

而且这里距离AdminCP.php控制器的一个片段:

public function index() 
    { 
    $this->load->helper(array('form')); // for user creation 
    $session_data = $this->session->userdata('logged_in'); 
    $aLevel = $session_data['accessLevel']; 
    if($this->session->userdata('logged_in') && $aLevel == 'Admin') 
    {     
     $userResult = $this->user->populateUsers(); 
     if($userResult) 
     { 
      $user_array = array(); 
      foreach($userResult as $row) 
      { 
       $user_array = array(
       'username' => $row->username, 
       'accessLevel' => $row->accessLevel, 
       'fullName' => $row->fullName 
       ); 

       $data['accessLevel'] = $session_data['accessLevel'];     
       $data['username'] = $session_data['username'];   
       $data['fullName'] = $session_data['fullName']; 
       $data['userList'] = $user_array;     
       $this->load->view('admin_cp_view', $data); 
      } 
      return TRUE; 
     } 
     else 
     { 
      return FALSE; 
     } 
    } 
    else 
    { 
    redirect('home', 'refresh');    
    session_write_close(); 
    } 
} 

最后,这是Admin_CP_View.php页面的部分我想要显示此信息:

<div class="well"> 
<h4>Manage users</h4> 
<p>Select a user from the list to manage</p> 
<table class="table">       
        <thead>       
          <tr> 
           <th>Username</th> 
           <th>Access Level</th> 
           <th>Full Name</th> 
          </tr> 
         </thead> 
         <tbody> 
         <?php foreach ($userList->result_array() as $row) 
         { ?> 
          <tr> 
           <td><?php echo $row['username'];?></td> 
           <td><?php echo $row['accessLevel'];?></td> 
           <td><?php echo $row['fullName'];?></td>         
          </tr> 
         <?php } ?> 

         </tbody> 
        </table> 
       </div> 

请让我知道我要去哪里错她e,谢谢!

回答

-1

更改用户模式是:

Class User extends CI_Model 
{ 
    function populateUsers() 
    { 
     $this->db->select('username, accessLevel, fullName'); 
     $this->db->from('users'); 

     $userquery = $this->db->get(); 

     if($userquery->num_rows() >= 1) 
     { 
      return $userquery->result(); 
     } 
     else 
     { 
      return false; 
     } 

    } 

} 
0

你有几个问题。首先,不是一个问题,因为它是毫无价值的代码。一个控制器不需要返回一个值,因为Codeigniter没有期望一个值,也不会关注它。

其次,我从index中删除了这些行,因为它们在视图中似乎未被使用。

$data['accessLevel'] = $session_data['accessLevel']; 
$data['username'] = $session_data['username']; 
$data['fullName'] = $session_data['fullName']; 

这里是控制器的修改后的索引函数,其逻辑返回TRUE或FALSE删除。

public function index() 
{ 
    $this->load->helper(array('form')); // for user creation 
    $session_data = $this->session->userdata('logged_in'); 
    $aLevel = $session_data['accessLevel']; 
    if($this->session->userdata('logged_in') && $aLevel == 'Admin') 
    { 
    $data['userList'] = $this->user->populateUsers(); 
    $this->load->view('admin_cp_view', $data); 
    } 
    else 
    { 
    session_write_close(); 
    redirect('home', 'refresh'); 
    } 
} 

我将采取不同的代码模型的populateUsers方法。首先不要盲目使用Query Builder。在某些情况下它是一个很好的工具。但是你的sql非常简单,为这样一个超简单的db请求运行大量的Query Build代码没有意义。

请参阅“为什么?”的代码注释。在其他变化。

Class User extends CI_Model 
{ 
    function populateUsers() 
    { 
    $userquery = $this->db->query("Select usernam, accessLevel, fullName from users"); 

    if($userquery->num_rows() > 0) 
    { 
     return $userquery->result_array(); 
    } 
    //{ 
    //else 
    //you do not need the else because if there were rows the method has returned 
    //and the next line will never be executed. 

    //On the other hand, if there are no rows then this line WILL execute 
    return array(); //Return empty array so that the view won't try to run foreach on a boolean 
// } 
    } 

然后在视图中为您<table>的代码是这样

<table class="table">       
    <thead>       
    <tr> 
     <th>Username</th> 
     <th>Access Level</th> 
     <th>Full Name</th> 
    </tr> 
    </thead> 
    <tbody> 
    <?php 
    foreach($userList as $row) { ?> 
    <tr> 
     <td><?php echo $row['username']; ?></td> 
     <td><?php echo $row['accessLevel']; ?></td> 
     <td><?php echo $row['fullName']; ?></td>         
    </tr> 
    <?php } ?> 
    </tbody> 
</table> 

这将在$userList写一排表中每个项目。

我想你可能没有正确地做出我第一次尝试回答的建议。您可能有多个页面加载,因为您在foreach循环内调用了load->view。现在只有表格行应该重复。

+0

感谢您的帮助,但它仍然无法正常工作,并且实际上导致页面不断重复,并且从数据库中检索到不同的行:(http://prntscr.com/esbril)。它基本上不会将数据附加到一行,而是实际上看起来像是在生成一个全新的页面。 – Vndrevv

+0

我很难想像您描述的内容,并且您链接到的屏幕截图不会显示任何异常内容。 – DFriend

+0

啊,是的,我应该更清楚。基本上,发生的事情是每个foreach检索的记录,它将作为整个其他页面追加,但在“其他”页面的该表上显示不同的值行。这里是另一个截图,应该澄清我的意思:http://prntscr.com/esehi1(请注意,我手动缩小它,以便您可以看到) – Vndrevv

0

在您的AdminCP中编辑。PHP

public function index() 
    { 
    $this->load->helper(array('form')); // for user creation 
    $session_data = $this->session->userdata('logged_in'); 
    $aLevel = $session_data['accessLevel']; 
    if($this->session->userdata('logged_in') && $aLevel == 'Admin') 
    {     
     $userResult = $this->user->populateUsers(); 
     if($userResult) 
     { 

       $this->load->view('admin_cp_view', $userResult); 
      } 
      return TRUE; 
     } 
     else 
     { 
      return FALSE; 
     } 
    } 
    else 
    { 
    redirect('home', 'refresh');    
    session_write_close(); 
    } 
} 

在Admin_CP_View.php编辑

<div class="well"> 
<h4>Manage users</h4> 
<p>Select a user from the list to manage</p> 
<table class="table">       
        <thead>       
          <tr> 
           <th>Username</th> 
           <th>Access Level</th> 
           <th>Full Name</th> 
          </tr> 
         </thead> 
         <tbody> 
         <?php foreach ($userResult as $row) 
         { ?> 
          <tr> 
           <td><?php echo $row['username'];?></td> 
           <td><?php echo $row['accessLevel'];?></td> 
           <td><?php echo $row['fullName'];?></td>         
          </tr> 
         <?php } ?> 

         </tbody> 
        </table> 
       </div> 

这可能帮助你..谢谢!

相关问题