溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

PHP的CI框架怎么用

發(fā)布時(shí)間:2021-10-19 10:20:09 來源:億速云 閱讀:107 作者:小新 欄目:web開發(fā)

小編給大家分享一下PHP的CI框架怎么用,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!

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

上次,我們已經(jīng)用php完成了一個(gè)小程序,這次就把這個(gè)小程序整合為CI框架。

index.php是入口文件,入口文件是不能改動(dòng)的。我們只需要寫好控制器,就可以通過調(diào)用模型和視圖來訪問頁面了。

下面就是該程序整合到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');

}

//保存數(shù)據(jù)的的方法

function save(){

//調(diào)用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);

//分解內(nèi)容

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

//獲取總數(shù)

$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();

}

//顯示數(shù)據(jù)

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';

/**

增加上面類庫返回結(jié)果集是以數(shù)組形式返回,原生態(tài)ci類庫返回的結(jié)果集是object對(duì)象

*/

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

因?yàn)檫@里有兩個(gè)網(wǎng)頁,所以相對(duì)于CI,我們這里應(yīng)有兩個(gè)視圖。分別是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>

以上是“PHP的CI框架怎么用”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI