php - Show database data with CodeIgniter -
i want show data in database codeigniter.
my code shows error 404 page requested not found
.
model
<?php class daftar_model extends ci_model { public function __construct() { //connect ke database $this->load->database(); } public function get_pertanyaan() { $query = $this->db->get('pertanyaan_ts'); return $query->result_array(); } } ?>
controller
<?php if ( ! defined('basepath')) exit('no direct script access allowed'); class daftar_controller extends ci_controller { public function index() { //echo "hallo"; $this->load->model('administrator/pertanyaan/daftar_model'); $this->load->view('administrator/pertanyaan/daftar_view',$data); $data["pertanyaan_ts"] = $this->daftarpertanyaan_model->get_pertanyaan; }
view
<html> <head> <title><?php echo $judul; ?></title> </head> <body> <h1>daftar user</h1> <table border="1"> <thead> <tr> <th>pertanyaan</th> </tr> </thead> <tbody> <?php foreach($pertanyaan $p){ ?> <tr> <td><?php echo $p->pertanyaan; ?></td> </tr> <?php } ?> </tbody> </table> </body> </html>
a couple changes controller , should good.
change name of class , browse example.com/daftar
class daftar extends ci_controller { public function index() { //assign page title $data['judaul'] = "daftar user"; $this->load->model('administrator/pertanyaan/daftar_model'); //you need assign value $data before sending view $data["pertanyaan_ts"] = $this->daftar_model->get_pertanyaan; $this->load->view('administrator/pertanyaan/daftar_view', $data); }
your model returns array need access data sent view array syntax $p['pertanyaan_ts']
not object syntax $p->pertanyaan_ts
. modified view file
<html> <head> <title><?php echo $judul; ?></title> </head> <body> <h1>daftar user</h1> <table border="1"> <thead> <tr> <th>pertanyaan</th> </tr> </thead> <tbody> <?php foreach($pertanyaan_ts $p) { ?> <tr> <td><?php echo $p['pertanyaan']; ?></td> </tr> <?php } ?> </tbody> </table> </body> </html>
Comments
Post a Comment