简体   繁体   中英

what's wrong with my code to appear database?

help me please, my db is dbindosat and table is formpengajuan

my controller

    <?php 


class Crud extends CI_Controller{

    function __construct(){
        parent::__construct();      
        $this->load->model('m_data');
        $this->load->helper('url');

    }

    function index(){
        $data['filedata'] = $this->m_data->tampil_data()->result();
        $this->load->view('viewdata',$data);
    }
...

my model

    <?php 

class M_data extends CI_Model{
    function tampil_data(){
        return $this->db->get('formpengajuan');

    }

    function input_data($data,$table){
        $this->db->insert($table,$data);
    }
    }

my view (viewdata)

<?php 
foreach($filedata as $data){ 
?>
<tr>
<td><?php echo $data-> kode; ?></td>
<td><?php echo $data-> instansi; ?></td>
<td><?php echo $data-> alamatinstansi; ?></td>
<td><?php echo $data-> judulkegiatan; ?></td>
<td><?php echo $data-> jeniskegiatan; ?></td>
<td><?php echo $data-> dana; ?></td>
<td><?php echo $data-> tanggalacara; ?></td>
<td><?php echo $data-> kontak; ?></td>
<td><?php echo $data-> email; ?></td>
</tr>
<?php } ?>

but messege A PHP Error was encountered Severity: Notice

Message: Undefined variable: filedata

Filename: views/viewdata.php

and Severity: Warning

Message: Invalid argument supplied for foreach()

Filename: views/viewdata.php

the database does not appear, please help.. thanks

You have to change this line of code in your controller:

 function index(){
    $data['filedata'] = $this->m_data->tampil_data();
    $this->load->view('viewdata',$data);
}

and this in your model code:

   function tampil_data(){
       return $this->db->get('formpengajuan')->result();
   }

and if you are not auto loading your database then load it in your controllers by adding this lines

 function __construct(){  
    parent::__construct(); 
    $this->load->database();
 }

Let me know if these changes did help you to solve your problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM