按照CI的用户手册教程连接的数据库;读取新闻条目
http://codeigniter.org.cn/user_guide/tutorial/news_section.html
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->getnews(); $data['title'] = 'News archive'; $this->load->view('templates/header', $data); $this->load->view('news/index', $data); $this->load->view('templates/footer'); } public function view($slug){ $data['news_item']=$this->news_model->getnews($slug); if (empty($data['news_item'])) { show_404(); } $data['title'] = $data['news_item']['title']; $this->load->view('templates/header', $data); $this->load->view('news/view', $data); $this->load->view('templates/footer'); } } 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->result_array(); }
} header.php文件 <html> <head> <title><?php echo $title ?> - CodeIgniter 2 Tutorial</title> </head> <body> <h1>CodeIgniter 2 Tutorial</h1>
news/index.php文件
<?php foreach ($news as $news_item): ?>
<h2><?php echo $news_item['title'] ?></h2> <div class="main"> <?php echo $news_item['text'] ?> </div> <p><a href="././index.php/news/<?php echo $news_item['slug'] ?>">View article</a></p>
<?php endforeach ?> footer.php文件 <strong>© 2011</strong> </body>
</html> 数据库配置文件
$db['default']['hostname'] = 'localhost'; $db['default']['username'] = 'root'; $db['default']['password'] = ''; $db['default']['database'] = 'ci_model'; $db['default']['dbdriver'] = 'mysql'; $db['default']['dbprefix'] = ''; $db['default']['pconnect'] = TRUE; $db['default']['db_debug'] = TRUE; $db['default']['cache_on'] = FALSE; $db['default']['cachedir'] = ''; $db['default']['char_set'] = 'utf8'; $db['default']['dbcollat'] = 'utf8_general_ci'; $db['default']['swap_pre'] = ''; $db['default']['autoinit'] = TRUE;
$db['default']['stricton'] = FALSE;
用户名,密码,数据库名均正确;
刷新页面没有任何反应,无报错
|