Postingan lainnya
bagaimana cara mengambil satu data dari data base di codeigniter??
jika ngambil semua data kan kaya gini.. di bagian modelnya
<?php
/**
*
*/
class Kata_motifasi extends CI_Model
{
function __construct()
{
# code...
$this->load->database();
}
function get_Kata_mutiara()
{
# code...
$query = $this->db->get('kata_motivasi');
return $query->result_array();
}
}
di bagian controller
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Home extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->model('kata_motifasi');
$this->load->helper('url_helper');
}
/**
* Index Page for this controller.
*
* Maps to the following URL
* http://example.com/index.php/welcome
* - or -
* http://example.com/index.php/welcome/index
* - or -
* Since this controller is set as the default controller in
* config/routes.php, it's displayed at http://example.com/
*
* So any other public methods not prefixed with an underscore will
* map to /index.php/welcome/<method_name>
* @see https://codeigniter.com/user_guide/general/urls.html
*/
public function index()
{
//$this->kata_motifasi->get_Kata_mutiara();
$this->load->helper('url');
$data['kata_motivasi'] = $this->kata_motifasi->get_Kata_mutiara();
$data['title'] = 'selamat datang';
$this->load->view('home/header.php');
$this->load->view('home/index.php', $data);
$this->load->view('home/footer.php');
}
}
nah jadi bagai mana kalau semisal saya cuma mau ngambil satu data daridatabase database?? sekian terimakasih
0
2 Jawaban:
model
function get_Kata_mutiara()
{
# code...
$query = $this->db->get('kata_motivasi');
return $query->row();
}
controller
$katamutiara = $this->kata_motifasi->get_Kata_mutiara();
//get datanya
if (isset($katamutiara))
{
echo $katamutiara->title;
echo $katamutiara->name;
echo $katamutiara->body;
}
1