PHP的CI架構

來源:互聯網
上載者:User

標籤:ci php

CI是PHP的一個架構,使用該架構可以使得我們的代碼更簡潔,具有較高的可維護性。CI架構是基於MVC進行使用的。MVC是簡稱,M是指模型(Model),通常是用於處理資料,與資料庫的打交道的。V是指視圖(View),是用來定義如何顯示資料以及其他內容的。C是指控制器(Controller),是處理模型和視圖的工具。我們的CI架構是基於MVC的,我們將程式分為模型,視圖和控制器三個部分。

上次,我們已經用php完成了一個小程式,這次就把這個小程式整合為CI架構。

index.php是入口檔案,入口檔案是不能改動的。我們只需要寫好控制器,就可以通過調用模型和視圖來訪問頁面了。

下面就是該程式整合到CI架構的代碼:


Controller

project.php

<?php

   class project extends CI_Controller{

function __construct(){

parent::__construct();

$this->load->model(‘project_model‘);

}

 

 

    public function index()

{

$this->load->view(‘project_view‘);

}

//儲存資料的的方法

function save(){

//調用project_model中的save_project方法

$this->project_model->save_project($_POST[‘project_name‘]);

//擷取插入的id

$id = mysql_insert_id();

$content = $_POST[‘context‘];

//替換字元

$content = str_replace(".",".#",$content);

$content = str_replace("?","?#",$content);

$content = str_replace("!","!#",$content);

//分解內容

$str = explode("#",$content);

//擷取總數

$count = count($str)-1;

$items = null;

for($i = 0 ; $i < $count ; $i++) {

$items .= "(‘{$str[$i]}‘,{$id})".‘,‘;

}

$items = rtrim($items,‘,‘);

$this->project_model->save_projectsentence($items);

$this->getdatas();

}

//顯示資料

function getdatas(){

$list = $this->project_model->getdatas();

foreach($list->result_array() as $v){

$result[] = $v;

}

$data[‘list‘] = $result;

$this->load->view(‘xianshi‘,$data);

}

}

?>




Model

project_model.php

<?php

//require_once ‘common_model.php‘;

/**

增加上面類庫返回結果集是以數組形式返回,原生態ci類庫返回的結果集是object對象

*/

class project_model extends CI_Model{

    function __construct(){

        parent::__construct();

    }

M


   //向project表插入name欄位方法

   function save_project($project){

   

$sql="insert into project(name)VALUES(‘{$project}‘)";

return $this->db->query($sql);

     }

 

//向project_sentences表插入sentence和id欄位方法

function save_projectsentence($items){

$query = "insert into project_sentence (sentences,id)values{$items}";

return $this->db->query($query);

     }

 

//顯示name與sentence欄位

function getdatas(){

 

$sql = "select p.name,ps.sentences from project p join project_sentence ps on p.id = ps.id";

 

// return $this->_query($sql);


return $this->db->query($sql);

}

}


?>


View

因為這裡有兩個網頁,所以相對於CI,我們這裡應有兩個視圖。分別是project_view(輸入頁面)和xianshi.php(顯示頁面)。

project_view

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>輸入頁面</title>

</head>


<body>VI

 <form action="<?php echo site_url(‘project/save‘);?>" method="POST">

 Project: <input type="text" name="project_name" />

 <br>Context: <textarea name="context" rows="20" cols="80"></textarea>

 <input type="submit">

</form>


</body>

</html>


xianshi.php

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>顯示頁面</title>

</head>


<body>

<div><a href="<?php echo site_url(‘project/index‘);?>">返回</a></div>


<table width="500" cellpadding="6" cellspacing="6" border="1">

<tr>

<th>Project_name</th>

    <th>Sentences</th>

</tr>

<?php $name = null;

if(!empty($list)){

foreach($list as $v){

if($name != $v[‘name‘]){

$name = $v[‘name‘];

?>

<tr>

<td align="center"><?php echo $v[‘name‘];?></td>

<td align="center"><?php echo $v[‘sentences‘];?></td>

</tr>

<?php }else{?>

<tr>

<td align="center"></td>

<td align="center"><?php echo $v[‘sentences‘];?></td>

</tr>

<?php }}}?>


</table>

</body>

</html>


至此我們的代碼就整合完了,我們可通過網址http://192.168.1.144/index.php/project/index來訪問了。

本文出自 “營運工作筆記” 部落格,請務必保留此出處http://yyyummy.blog.51cto.com/8842100/1439812

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.