The CI framework of PHP

Source: Internet
Author: User

CI is a framework of PHP that uses this framework to make our code more concise and highly maintainable. The CI framework is used based on MVC. MVC is the abbreviation, M refers to the model, which is usually used to process data, dealing with the database. V refers to a view, which is used to define how the data and other content are displayed. C refers to the controller, which is a tool for working with models and views. Our CI framework is based on MVC, and we divide the program into three parts: model, view and controller.

Last time, we have completed a small program in PHP, this time to integrate this small program into a CI framework.

index.php is a portal file, the entry file cannot be changed. We just have to write the controller and we can access the page by invoking the model and view.

Here is the code that the program integrates into the CI framework:


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

}

Ways to save your data

function Save () {

Call the Save_project method in Project_model

$this->project_model->save_project ($_post[' project_name ');

Gets the ID of the insert

$id = mysql_insert_id ();

$content = $_post[' context '];

Replace character

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

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

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

Explode content

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

Get Total

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

}

Show data

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

/**

Adding the above class library returned result set is returned as an array, and the result set returned by the original ecological CI class library is the object

*/

Class Project_model extends ci_model{

function __construct () {

Parent::__construct ();

}

M


Insert the Name field method into the project table

function Save_project ($project) {

$sql = "INSERT into project (name) VALUES (' {$project} ')";

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

}

Insert sentence and ID field methods into the Project_sentences table

function Save_projectsentence ($items) {

$query = "INSERT into Project_sentence (sentences,id) values{$items}";

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

}

Display the name and sentence fields

function Getdatas () {

$sql = "Select P.name,ps.sentences from Project P joins project_sentence PS on p.id = ps.id";

return $this->_query ($sql);


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

}

}


?>


View

Because there are two pages here, we should have two views relative to CI. Project_view (Input page) and xianshi.php (Display page) are respectively.

Project_view

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

<title> Input Page </title>


<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= "cols=" ></textarea>

<input type= "Submit" >

</form>


</body>


xianshi.php

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

<title> Show Pages </title>


<body>

<div><a href= "<?php echo site_url (' Project/index ');? > "> Back </a></div>


<table width= "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>


Now that our code has been integrated, we can access it through the URL http://192.168.1.144/index.php/project/index.

This article is from the "Operational Work Notes" blog, make sure to keep this source http://yyyummy.blog.51cto.com/8842100/1439812

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.