2015-01-20 114 views
-1

我收到错误消息:在我的视图中foreach()的无效参数。我想显示我的MySQL表中的所有条目,但我一直在收到错误消息。我是Codeigniter的新手,无法真正弄清楚如何解决这个问题。该代码如下......请帮助我为foreach()提供的参数无效codeigniter

user_model.php

<?php     
          foreach ($daftar as $data) : 
         ?> 
          <tr> 
           <td><?php echo $data->id_petugas; ?></td> 
           <td></td> 
           <td></td> 
           <td></td> 
           <td> 
            <button class="btn edit"><i class="icon-edit"></i></button> 
            <button class="btn btn-danger remove" data-toggle="confirmation"> 
             <i class="icon-remove"></i></button> 
           </td> 
          </tr> 
         <?php 
          endforeach; 
         ?>   

user_controller

<?php 
class User_controller extends CI_Controller{ 
    function __Construct() 
    { 
     parent ::__construct(); 
    } 

    function user(){ 
     $this->load->model('user_model'); 
     $data['daftar'] = $this->user_model->get_user_all(); 

     $this->load->view('daftar_user',$data); 
    } 

daftar_user.php

<?php     
          foreach ($daftar as $data) : 
         ?> 
          <tr> 
           <td><?php echo $data->id_petugas; ?></td> 
           <td></td> 
           <td></td> 
           <td></td> 
           <td> 
            <button class="btn edit"><i class="icon-edit"></i></button> 
            <button class="btn btn-danger remove" data-toggle="confirmation"> 
             <i class="icon-remove"></i></button> 
           </td> 
          </tr> 
         <?php 
          endforeach; 
         ?>  

我已经从这个错误文件

+0

get_user_all看起来像什么? (你没有发布你的模型,你发布了两次你的观点。) – 2015-01-20 22:10:56

+0

对不起,先生,这种模式 – 2015-01-21 00:34:03

+0

功能get_user_all() \t \t { \t \t \t $查询= $这个 - > DB->查询( “SELECT * FROM petugas为了通过ASC”); \t \t \t return $ query-> result(); \t \t \t \t \t $ this-> db-> select('*'); \t \t \t $ this-> db-> from('petugas'); \t \t \t $ this-> db-> order_by('id_petugas','ASC'); \t \t \t \t \t \t $ query = $ this-> db-> get(); \t \t \t \t如果($查询 - > NUM_ROWS()> 0){ \t \t \t \t \t的foreach($查询 - >结果()作为$数据){ \t \t \t \t \t \t $ daftar [] = $数据; \t \t \t \t \t} \t \t \t \t回$ daftar; \t \t \t \t} \t \t \t \t \t} – 2015-01-21 00:34:47

回答

0

T他不是你正在使用的正确的编码技术。因此,请首先纠正您的型号查询代码,例如:

$query = $this->db->from('petugas')->order_by('id_petugas','ASC')->get()->result(); 
return $query; 

无需更改其他任何东西。您将看到您的所有数据。

我希望这会帮助你。

0
function get_user_all() 
{ 
    $query=$this->db->query("select * from petugas order by asc"); 
    return $query->result(); 
    $this->db->select('*'); 
    $this->db->from('petugas'); 
    $this->db->order_by('id_petugas','ASC'); 
    $query=$this->db->get(); 
    if($query->num_rows() > 0){ 
    foreach($query->result() as $data){ 
     $daftar[]= $data; 
    } 
    return $daftar; 
    } 
} 
0

OK,它的工作..感谢了很多

我的登录系统就可以了。它的工作..我有三个用户类型1.admin 2.customer服务3.服务员。 我需要,如果登录到管理员和CS或服务员。其重定向三种不同的看法...

我试过转移两个不同的意见,应该如何分配两个以上的意见?

<?php if($level == "1"){ ?> 
    <?php echo $this->view('admin/template'); ?> 
<?php } else { ?> 
    <?php echo $this->view('cs/template'); ?> 
<?php } ?>