Codeigniter 用户登录注册模块
Codeigniter 用戶登錄注冊模塊
以下皆是基于Codeigniter + MySQL
一、要實現(xiàn)用戶登錄注冊功能,首先就要和MySQL數(shù)據(jù)庫連接,操作流程如下:
CI中貫徹MVC模型,即Model + View + Controller。數(shù)據(jù)模型Model處理數(shù)據(jù)庫的運(yùn)算,視圖View顧名思義即使將數(shù)據(jù)顯示出來的頁面,而控制器Controller是用來代理完成某項任務(wù)的PHP類,Controller充當(dāng)MVC架構(gòu)應(yīng)用程序的“粘合劑”。再回到數(shù)據(jù)模型,通過創(chuàng)建控制器類,可以對數(shù)據(jù)庫或者其他數(shù)據(jù)存儲方式進(jìn)行取回、插入和更新。CI中要使用數(shù)據(jù)庫,首先要在配置文件“application/config/database.php”中對數(shù)據(jù)庫進(jìn)行配置,包括數(shù)據(jù)庫名、用戶名、密碼等。以下代碼段繼承控制器CI_Model類,獲得數(shù)據(jù)表‘news’,并對數(shù)據(jù)表‘news’進(jìn)行了讀和寫的簡單操作:
<?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'); //獲得‘news’數(shù)據(jù)表,并將結(jié)果返回到‘query’變量中return $query->result_array();}$query = $this->db->get_where('news',array('slug' => $slug)); //獲得‘news’數(shù)據(jù)表中主鍵為‘slug’的數(shù)據(jù)記錄return $query->row_array();}public function set_news(){$this->load->helper('url');$slug = url_title($this->input->post('title'), 'dash', TRUE);$data = array('title' => $this->input->post('title'),'slug' => $slug,'text' => $this->input->post('text')); return $this->db->insert('news', $data); //將‘data’數(shù)據(jù)記錄插入‘news’數(shù)據(jù)表中 } }?二、創(chuàng)建完模型CI_Model類,從數(shù)據(jù)庫查詢到數(shù)據(jù)之后,需要創(chuàng)建控制器CI_Controller類將數(shù)據(jù)模型和用來顯示數(shù)據(jù)內(nèi)容的視圖“粘合”起來。以下代碼段繼承控制器CI_Controller類,調(diào)用了數(shù)據(jù)模型,控制器類獲得數(shù)據(jù)模型中的數(shù)據(jù)并傳遞給視圖顯示出來:
<?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();$data['title'] = 'News archive';$this->load->view('templates/header',$data);$this->load->view('news/index',$data);$this->load->view('templates/footer',$data);}public function view($slug){$data['news_item'] = $this->news_model->get_news($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');}public function create(){$this->load->helper('form');$this->load->library('form_validation');$data['title'] = 'Create a news item';$this->form_validation->set_rules('title','Title','required');$this->form_validation->set_rules('text','text','required');if ($this->form_validation->run() == FALSE) {$this->load->view('templates/header',$data);$this->load->view('news/create');$this->load->view('templates/footer'); } else {$this->news_model->set_news();$this->load->view('news/success');}} }三、創(chuàng)建完數(shù)據(jù)模型和控制器類之后,最后一件事就是創(chuàng)建視圖View將數(shù)據(jù)顯示出來。以下代碼段循環(huán)顯示數(shù)據(jù)庫中的數(shù)據(jù):
<?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 = "http://localhost/citest/index.php/news/<?php echo $news_item['slug']?>">View article</a></p><?php endforeach?>轉(zhuǎn)載于:https://www.cnblogs.com/cloume/archive/2012/11/20/2777132.html
總結(jié)
以上是生活随笔為你收集整理的Codeigniter 用户登录注册模块的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【原创】jQuery1.8.2源码解析之
- 下一篇: 提高网站首页载入速度的常用方法