คือ ผมเริ่มหัดใช้ codeignite ทำ tutorial มันขึ้น error ครับ
คือ ทำตาม guidebook เลยครับ
error นะครับ (เข้าจาก /index.php/news ถ้าเข้าจากหน้าปกติเป็นหน้าว่างครับ)
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: news
Filename: news/index.php
Line Number: 1
A PHP Error was encountered
Severity: Warning
Message: Invalid argument supplied for foreach()
Filename: news/index.php
Line Number: 1
model มีไฟล์ news_model.php
<?php
class News_model extends CI_Model {
public function __construct()
{
$this->load->database();
}
public function get_news($slug = FALSE)
{
if ($slug === FALSE)
{
$query = $this->db->get('news');
return $query->result_array();
}
$query = $this->db->get_where('news', array('slug' => $slug));
return $query->row_array();
}
}
Controller มี news.php
อ้างอิงจาก:
<?php
class News extends CI_Controller {
public function __construct(){
parent::__construct();
$this->load->model('news_model');
}
public function index(){
$data['news'] = $this->news_model->get_news();
}
public function view($slug){
$data['news'] = $this->news_model->get_news($slug);
}
}
view มี pages.php กับ /news/index.php
<?php
class Pages extends CI_Controller {
$this->load->library('firephp');
public function view($page = 'home')
{
if ( ! file_exists('application/views/pages/'.$page.'.php')){
// Whoops, we don't have a page for that!
show_404();
}
$data['title'] = ucfirst($page); // Capitalize the first letter
$this->load->view('templates/header', $data);
$this->load->view('news/index', $data);
$this->load->view('templates/footer', $data);
}
public function index()
{
$data['news'] = $this->news_model->get_news();
$data['title'] = 'News archive';
$this->load->view('templates/header', $data);
$this->load->view('news/index', $data);
$this->load->view('templates/footer');
}
}
<?php foreach ($news as $news_item): ?>
<h2><?php echo $news_item['title'] ?></h2>
<div id="main">
<?php echo $news_item['text'] ?>
</div>
<p><a href="news/<?php echo $news_item['slug'] ?>">View article</a></p>
<?php endforeach ?>
หาที่ผิดมาครึ่งวันแล้วครับยังไม่เจอครับ ขอบคุณทุกคำตอบครับ
ปัญหา tutorial codeignite
คือ ทำตาม guidebook เลยครับ
error นะครับ (เข้าจาก /index.php/news ถ้าเข้าจากหน้าปกติเป็นหน้าว่างครับ)
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: news
Filename: news/index.php
Line Number: 1
A PHP Error was encountered
Severity: Warning
Message: Invalid argument supplied for foreach()
Filename: news/index.php
Line Number: 1
model มีไฟล์ news_model.php
<?php
class News_model extends CI_Model {
public function __construct()
{
$this->load->database();
}
public function get_news($slug = FALSE)
{
if ($slug === FALSE)
{
$query = $this->db->get('news');
return $query->result_array();
}
$query = $this->db->get_where('news', array('slug' => $slug));
return $query->row_array();
}
}
Controller มี news.php
อ้างอิงจาก:
<?php
class News extends CI_Controller {
public function __construct(){
parent::__construct();
$this->load->model('news_model');
}
public function index(){
$data['news'] = $this->news_model->get_news();
}
public function view($slug){
$data['news'] = $this->news_model->get_news($slug);
}
}
view มี pages.php กับ /news/index.php
<?php
class Pages extends CI_Controller {
$this->load->library('firephp');
public function view($page = 'home')
{
if ( ! file_exists('application/views/pages/'.$page.'.php')){
// Whoops, we don't have a page for that!
show_404();
}
$data['title'] = ucfirst($page); // Capitalize the first letter
$this->load->view('templates/header', $data);
$this->load->view('news/index', $data);
$this->load->view('templates/footer', $data);
}
public function index()
{
$data['news'] = $this->news_model->get_news();
$data['title'] = 'News archive';
$this->load->view('templates/header', $data);
$this->load->view('news/index', $data);
$this->load->view('templates/footer');
}
}
<?php foreach ($news as $news_item): ?>
<h2><?php echo $news_item['title'] ?></h2>
<div id="main">
<?php echo $news_item['text'] ?>
</div>
<p><a href="news/<?php echo $news_item['slug'] ?>">View article</a></p>
<?php endforeach ?>
หาที่ผิดมาครึ่งวันแล้วครับยังไม่เจอครับ ขอบคุณทุกคำตอบครับ