在codeigniter中基于id显示表数据

dsf9zpds  于 2021-06-20  发布在  Mysql
关注(0)|答案(1)|浏览(322)

我有一个存储id、id login、name等数据的表,但我只想显示基于id login的数据
这里是我的控制器:

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\u登录,我想基于它显示表数据,希望有人能帮助我,因为我只是使用codeigniter,thnaks

5lwkijsr

5lwkijsr1#

我自己解决了这个问题,哈哈。我只是从会话中传递id\u登录值,所以我把这个添加到我的控制器中:

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;
    }
}

相关问题