在Codeigniter中显示基于ID的表数据

时间:2018-08-05 06:00:57

标签: php mysql database codeigniter-3

我有一个表,用于存储ID,ID登录名,名称等数据。但是我只想显示基于ID登录的数据

这里是我的控制器:

function index(){
    $data['hasil'] = $this->M_user_lapor->index_model();
    $this->load->view('v_user_lapor/index', $data);
}

我的模特:

function index_model(){
    $baca = $this->db->query('select * from user_lapor');
    if($baca->num_rows() > 0){
        foreach ($baca->result() as $data){
            $hasil[] = array(
                'id_login'=>$data->id_login,
                'id_lapor'=>$data->id_lapor,
                'nm_unit'=>$data->nm_unit,
                'pic_1'=>$data->pic_1,
                'pic_2'=>$data->pic_2,
                'ip_wan'=>$data->ip_wan,
                'ip_lan'=>$data->ip_lan,
                'prov'=>$data->prov,
                'icn_sid'=>$data->icn_sid,
                'tlkm_sid'=>$data->tlkm_sid,
                'status'=>$data->status,

            );
        }
        return json_encode ($hasil);
    }else{
        return false;
    }
}   

查看:

 <tbody>
    <?php
       if ($hasil){
         $no = 1;
         $array = json_decode($hasil, true);
         foreach($array as $data) {
         ?>
       <tr>
          <td class="text-center"><?php echo $no++;?></td>
          <td><?php echo $data['nm_unit'];?></td>
          <td><?php echo $data['pic_1'];?></td>
          <td><?php echo $data['pic_2'];?></td>
          <td><?php echo $data['ip_wan'];?></td>
          <td><?php echo $data['ip_lan'];?></td>
          <td><?php echo $data['prov'];?></td>
          <td><?php echo $data['icn_sid'];?></td>
          <td><?php echo $data['tlkm_sid'];?></td>
       </tr>
       <?php
          }
           }
       ?>
     </tbody>

如您所见,我的模型中有id_login,我想显示基于它的表数据,希望有人可以帮助我,因为我只是在使用codeigniter,thaks

1 个答案:

答案 0 :(得分:0)

我自己解决了这个问题,哈哈。我只是从会话中传递了id_login值,所以我将其添加到控制器中:

function index(){
    $id_login = $this->session->id_login;
    $data['hasil'] = $this->M_user_lapor->index_model($id_login);
    $this->load->view('v_user_lapor/index', $data);
}

并将其称为我的模型:

function index_model($id_login){
    $baca = $this->db->query('select * from user_lapor where id_login='.$id_login);
    if($baca->num_rows() > 0){
        foreach ($baca->result() as $data){
            $hasil[] = array(
                'id_login'=>$data->id_login,
                'id_lapor'=>$data->id_lapor,
                'nm_unit'=>$data->nm_unit,
                'pic_1'=>$data->pic_1,
                'pic_2'=>$data->pic_2,
                'ip_wan'=>$data->ip_wan,
                'ip_lan'=>$data->ip_lan,
                'prov'=>$data->prov,
                'icn_sid'=>$data->icn_sid,
                'tlkm_sid'=>$data->tlkm_sid,
                'status'=>$data->status,
            );
        }
        return json_encode ($hasil);
    }else{
        return false;
    }
}