ariyanti dwiastuti ariyanti dwiastuti - 5 months ago 11
PHP Question

can't show my database in codeignaiter

Can anybody help?
I think there is no problem with my code. But I still can't show my database on codeigniter.

This is my code :

view :

<html>

<head>
<title>View Data</title>
</head>

<body>
<h1>View Data</h1>
<table border="0">
<tr>
<th>Id Buku</th>
<th>Judul Buku</th>
<th>Tahun Terbit</th>
</tr>

<?php if (is_array($coba)) foreach ($coba as $item) { ?>
<tr>
<td>
<?php echo $item->id_buku;?></td>
<td>
<?php echo $item->judul_buku;?></td>
<td>
<?php echo $item->tahun_terbit;?></td>
</tr>
<?php } ?>

</table>
</body>

</html>


Models:

<?php

class Test_model extends CI_Model{

//konstruktor; dipanggil saat pertama kali object dibuat
function __construct(){

parent::__construct();

/*
load database;
database yang di load adalah databse yang kita definisikan pada file database.php
*/
$this->load->library('table');
$this->load->database();
}

//fungsi yang akan mengambil data pada database
function get_all_book_data(){

//query data
$query = $this->db->get('tabel_coba');


}
}
?>


Controlller :

<?php
class Test_controller extends CI_Controller{

/*
konstruktor;
dipanggil saat pertama kali object controller dibuat
load model
*/
public function __construct(){
parent::__construct();
$this->load->model('test_model');
}

/*
public method;
load view, tampilkan data pada class view
*/
public function get_book_data(){

$data['coba'] = $this->test_model->get_all_book_data();

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


FYI :
Its my database : (database name : guestbook)

Answer

First off make sure you have auto loaded config/autoload.php your database and set your correct details in config/database.php

You can find all information on user guide http://www.codeigniter.com/userguide2/ and http://www.codeigniter.com/userguide2/database/index.html

Second Change Get Book Data To Index

class Test_controller extends CI_Controller{

public function __construct(){
    parent::__construct();
    $this->load->model('test_model');
}

public function index(){
    // Checks if any in array
    $data['coba'] = array();

    // Gets data

    $results = $this->test_model->get_all_book_data(); 

    // Run get results in loop
    foreach ($results as $result) {
       $data['coba'][] = array(
        'id_buku' => $result['id_buku'],
        'judul_buku' => $result['judul_buku'],
        'tahun_terbit' => $result['tahun_terbit']
       );
    }

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

}

Third in model use if statement to check if true or false;

And remove $this->load->library('table'); and $this->load->database(); You should autoload your database library when you have set your connection in config/database.php

class Test_model extends CI_Model {

    public function get_all_book_data() {
        // Change the tablename to what your table is.
        $query = $this->db->get('tablename');


        // Checks if there are any items greater than 0 meaning no data
        if ($query->num_rows() > 0) {
            return $query->result_array();
        } else {
            return false;
        }
    }
}

In view try this

<table>
<thead>
<tr>
<th></th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
<?php if ($coba) { ?>
<?php foreach ($coba as $item) {?> 
    <tr>
    <td><?php echo $item['id_buku'];?></td>
    <td><?php echo $item['judul_buku'];?></td>
    <td><?php echo $item['tahun_terbit'];?></td>
    </tr>
<?php } ?>
<?php } else { ?>
<tr>
<td class="text-center" colspan="3">No Results</td>
</tr>
<?php } ?>
<tbody>
</table>
Comments