Skip to main content

cara menampilankan satu record dari table

May 23, 2011 by juliyan

juliyan's picture

para master, pertanyaan nubie banget nih

bagaimana cara menampilkan satu record dari table.
dari manggil tabel dan record sampai muncul di view.
mohon bantuan model, controller dan view nya ya.

thanks master

Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Re: cara menampilankan satu record dari table

May 23, 2011 by firgilius, 2 days 4 hours ago
Comment: 8210

firgilius's picture

Contoh table 'news'

Model dengan nama model 'News':

<?php
class News extends CI_Model{
        public function __construct()
	{
		parent::__construct();
	}
 
        function detail($id)
	{
		$condition = array("id"=>$this->db->escape_str($id));
		$query = $this->db->get_where('news',$condition);
		if($query->num_rows() > 0){
			$result = $query->result_array();
			return $result[0];
		} else {
			return false;
		}
	}
}

Controller namanya jg news (bebas aj) :

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
 
class News extends CI_Controller {
   public function  __construct() {
       parent::__construct();
       $this->load->model('News');
   }
 
   public function detail($id)
    {
        if($id)
        {
            $detail=News::detail($id);
            if($detail)
            {
                $data['detail']=$detail;
                $this->load->view('news', $data);
            } else {
                redirect('news');
            }
        } else {
            redirect('news');
        }
    }
}

View terserah tp dsini samplenya 'news':

<div class="left">
 
                <h3>
                    <?php echo $detail['title'];?>
                </h3>                
                <?php echo $detail['description'];?>
            </div>

Hatur nuhun

Cannot redeclare class

May 23, 2011 by juliyan, 1 day 17 hours ago
Comment: 8214

juliyan's picture

Master firgilius kok error waktu di test!

Fatal error: Cannot redeclare class News in C:\AppServ\www\belajarCI\application\models\news.php on line 19

file model, controller, dan view semua sama pakai news.php

Redeclare

May 23, 2011 by firgilius, 1 day 15 hours ago
Comment: 8215

firgilius's picture

Coba ganti nama class News model jadi 'MNews':

<?php
class MNews extends CI_Model{
        public function __construct()
	{
		parent::__construct();
	}
 
        function detail($id)
	{
		$condition = array("id"=>$this->db->escape_str($id));
		$query = $this->db->get_where('news',$condition);
		if($query->num_rows() > 0){
			$result = $query->result_array();
			return $result[0];
		} else {
			return false;
		}
	}
}

And di controller pemanggilan model 'News' ubah jadi 'MNews'

problem solved

May 24, 2011 by juliyan, 1 day 14 hours ago
Comment: 8216

juliyan's picture

master firgilius, iya benar karena nama controller dan model nya sama.
thanks master firgilius.

Premium Drupal Themes by Adaptivethemes