日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

CodeIgniter开发实际案例-新闻网站【转】

發布時間:2025/7/14 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 CodeIgniter开发实际案例-新闻网站【转】 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

CodeIgniter開發實際案例-新聞網站

轉:http://blog.csdn.net/ict2014/article/details/22104711?utm_source=tuicool&utm_medium=referral

標簽:?codeigniter新聞框架示例 ?分類: ? Code Igniter(3)?

1、建立數據庫

? ? ? 運用Navicat For?MySQL工具,創建一個數據庫,名稱為"news",

? ? ? 并建立如下表(鼠標右鍵,命令行運行如下sql語句):

?

[sql]?view plaincopy print?
  • CREATE?TABLE?news?(??
  • ????id?int(11)?NOT?NULL?AUTO_INCREMENT,??
  • ????title?varchar(128)?NOT?NULL,??
  • ????slug?varchar(128)?NOT?NULL,??
  • ????text?text?NOT?NULL,??
  • ????PRIMARY?KEY?(id),??
  • ????KEY?slug?(slug)??
  • );??

  • ? ? 建立完數據庫以及表之后,刷新數據庫,然后雙擊打開news表,填充兩條內容。

    ?

    ? ? 第一條:(title slug text) 分別為(1,first,Nice Weather!)

    ? ? 第二條:(title slug text) 分別為(2,second,?Pray for MH370!)

    2、建立Model模型

    ? ? ? 在本系列第二講中,已經將codeigniter安裝包拷貝到了wampserver的www目錄下。

    ? ? ? 在codeigniter文件夾中,我們在application/models下新建一個文件,名稱為“news_model.PHP”

    ?

    [php]?view plaincopy print?
  • <?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();??
  • ????}??
  • ??
  • ????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);??
  • ????}??
  • ??????
  • }??
  • ?>??

  • ? ? ?model必須繼承CI_Model,構造函數用于加載數據庫,get_news用于讀取數據庫中的新聞,set_news用于插入一條新聞記錄。

    ?

    3、建立View

    ? ? 在application下新建兩個文件夾,templates和news。

    ? ? 在templates文件夾下,新建兩個文件,header.php和footer.php。

    ? ? header.php的內容如下:

    ?

    [html]?view plaincopy print?
  • <html>??
  • <head>??
  • ????<title><?php?echo?$title??>?-?News</title>??
  • </head>??
  • <body>??
  • ????<h1>News</h1>??

  • ? ?footer.php的內容如下:

    ?

    ?

    [html]?view plaincopy print?
  • <strong>??2011</strong>??
  • </body>??
  • </html>??

  • ? ? 在news文件夾下,新建四個文件,index.php, success.php, view.php和create.php。

    ?

    ? ? index.php內容如下:

    ?

    [php]?view plaincopy print?
  • <?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??>??

  • ? ?success.php內容如下:

    ?

    ?

    [html]?view plaincopy print?
  • Success??

  • ? ? view.php內容如下:

    ?

    ?

    [php]?view plaincopy print?
  • <?php??
  • ????echo?'<h2>'.$news_item['title'].'</h2>';??
  • ????echo?$news_item['text'];??
  • ?>??

  • ? ? create.php內容如下:

    ?

    ?

    [php]?view plaincopy print?
  • <h2>Create?a?news?item</h2>??
  • ??
  • <?php?echo?validation_errors();??>??
  • ??
  • <?php?echo?form_open('news/create')??>??
  • ??
  • ????<label?for="title">Title</label>??
  • ????<input?type="input"?name="title"?/><br?/>??
  • ??
  • ????<label?for="text">Text</label>??
  • ????<textarea?name="text"></textarea><br?/>??
  • ??
  • ????<input?type="submit"?name="submit"?value="Create?news?item"?/>??
  • ??
  • </form>??

  • 4、建立Controller

    ?

    ? ?在application/controllers下新建文件news.php。

    ? ?news.php文件內容如下:

    ?

    [php]?view plaincopy print?
  • <?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');??
  • ????}??
  • ??
  • ????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');??
  • ????????}??
  • ????}??
  • ??
  • }??
  • ?>??

  • ? ?Controller用于加載news_model以及生成view視圖。其中,除了構造函數之外,其他的每一個函數對應一個界面。

    ?

    5、修改配置文件

    ? ?修改數據庫文件,在application/config下,打開database.php,修改如下內容,添加數據庫、用戶名、密碼等信息。

    ? ?

    ? ?修改application/config下的routes.php,輸出已有的兩行代碼,添加如下內容,

    ?

    [html]?view plaincopy print?
  • $route['news/create']?=?'news/create';??
  • $route['news/(:any)']?=?'news/view/$1';??
  • $route['news']?=?'news';??
  • $route['(:any)']?=?'pages/view/$1';??
  • $route['default_controller']?=?'welcome';??

  • 6、測試

    ?

    在瀏覽器中輸入如下網址,

    http://127.0.0.1/codeigniter/index.php/news

    可以看到如下頁面:

    ?

    輸入如下網址:

    http://127.0.0.1/codeigniter/index.php/news/create

    可以看到如下添加新聞的界面:

    同時兩個頁面中都有一些鏈接,可以點擊,對應著views/news下的幾個文件。

    ?

    總結:CodeIgniter是基于MVC架構的。只要相應的開發model、view以及controller即可。model用于管理數據,view用于顯示,controller充當中介者,用于管理model以及view以及其他資源。學習框架最好的方式,就是搭建一個簡單的項目,并且閱讀其中的代碼。要學習model、view以及controller的代碼。

    轉載于:https://www.cnblogs.com/fangyuan303687320/p/5538127.html

    總結

    以上是生活随笔為你收集整理的CodeIgniter开发实际案例-新闻网站【转】的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。