buat database terlebih dahulu
create database latihan;
use latihan;
lalu buat table galerinya,
CREATE TABLE IF NOT EXISTS `galeri` (
`id` int(3) NOT NULL AUTO_INCREMENT,
`foto` varchar(100) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
sekarang waktunya konfigurasi database dan autoload.
buka file database.php pada folder application/config/database.php
$db['default']['hostname'] = 'localhost';
$db['default']['username'] = 'root';
$db['default']['password'] = '';
$db['default']['database'] = 'latihan';
$db['default']['dbdriver'] = 'mysql';
lalu buka file autoload.php pada folder application/config/autoload.php, cari baris kode berikut
$autoload['libraries'] = array('');
ubah menjadi
$autoload['libraries'] = array('database');
lalu cari kode
$autoload['helper'] = array('');
ubah menjadi
$autoload['helper'] = array('url','file','html','form');
setelah itu siapkan modelnya, berikut modelnya
galeri_model.php
- <?php
- class galeri_model extends CI_Model{
- function view($table,$limit="", $start=""){
- if(!empty($limit) || !empty($start)){
- $q = "LIMIT $start,$limit";
- }
- $query=$this->db->query("select * from $table $q")->result();
- return $query;
- }
- function hitung($table) {
- return $this->db->count_all("$table");
- }
- }
simpan pada folder application/models, lalu buat controllernya.
galeri.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Galeri extends CI_Controller {
public function index()
{
$this->load->model('galeri_model');
$this->load->library('pagination');
$config = array();
$config["base_url"] = base_url() . "galeri/index";
$config["total_rows"] = $this->galeri_model->hitung("galeri");
$config["per_page"] = 1;
$config["uri_segment"] = 3;
$this->pagination->initialize($config);
$page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
$data["links"] = $this->pagination->create_links();
$data['view']=$this->galeri_model->view("galeri",$config["per_page"], $page);
$this->load->view('view', $data);
}
}
view.php
- <div >
- <table >
- <tr>
- <td>Foto</td>
- </tr>
- <?php
- foreach($view as $data){
- ?>
- <tr>
- <td><?php echo $data->foto?></td>
- </tr>
- <?php
- }
- ?>
- </table>
- </div>
- </div>
- </div>
- </div>
- <?php if( ! empty($links)){ echo $links; } ?>
simpan pada folder application/views. yap sudah segitu bahasan mengenai paggination, semoga bermanfaat, jika masih kurang mengerti sila