首頁  >  文章  >  後端開發  >  詳解基於Codeigniter框架實作student資訊系統網站動態發布功能程式碼案例

詳解基於Codeigniter框架實作student資訊系統網站動態發布功能程式碼案例

黄舟
黄舟原創
2017-03-24 09:32:061381瀏覽

這篇文章主要介紹了基於Codeigniter框架實現的student資訊系統網站動態發布功能,詳細分析了動態網站相關的資料庫sql語句、MVC各個模組功能與實作技巧,需要的朋友可以參考下

本文實例講述了基於Codeigniter框架實現的student資訊系統站點動態發布功能。表:

CREATE TABLE IF NOT EXISTS `student`(
    //主键id
    `id` int(11) NOT NULL AUTO_INCREMENT,
    //学生姓名
    `s_name` varchar(64) NOT NULL,
    //学生家长的姓名
    `p_name` varchar(64) NOT NULL,
    //学生的家庭住址
    `address` varchar(100) NOT NULL,
    //所在城市
    `city`  varchar(30) NOT NULL,
    //所在国家
    `state` varchar(30) NOT NULL,
    //所在地区的邮政编码
    `zip`  varchar(20) NOT NULL,
    //电话
    `phone` varchar(15) NOT NULL,
    //邮件
    `email` varchar(20) NOT NULL,
    //主键设置
    PRIMARY KEY(`id`)
)ENGINE=INNODB DEFAULT CHARSET=UTF8 AUTO_INCREMENT=1;

*註:在此我有兩個地方需要解釋一下:

1."IF NOT EXISTS":如果資料在建立表格的時候,前面加上了"IF NOT EXISTS",那就表示即使此表已經存在,也會執行成功;

2."ENGINE=INNODB":這個是資料庫的引擎設置,常用

mysql資料庫

#引擎有ISAM,MYISAM,HEAP等;在建立完資料表之後,我們再來看一下資料庫的連線。 .\application\config\config.php檔案內設定基本的URL,對於我的基本url是:localhost/codeigniter/


下面我們來看看mvc思想

架構

的設計首先開啟.application\controllers\檔案目錄,在裡面建立一個student.php

控制器

student.php

#在此我們先來透過student這個控制器來測試一下,列印出helloworld,記住存取路徑是:localhost/codeigniter/index.php/student/index

class student extends CI_Controller{
    //student controller construct
    public function construct(){
     parent::construct();
    }
    //index test function
    public function index(){
     echo "helloworld";
    }
}

it output: helloworld

#下面我們來換一下,看看下面這段code:

class student extends CI_Controller{
    //student controller
    public function construct(){
      parent::construct();
    }
    //define a array,name is arraydata, it have three parameters
    protected $arraydata=array(
      'title'=>'Classroom:Home page',
      'headline'=>'welcome to the classroom Mangement System',
      'include'=>'student_index'
    );
    //index function
    public function index(){
      $this->load->view('template',$this->arraydata);
    }
}

這段程式碼需要一個

檢視

#,template.phptemplate.php:

<!DOCTYPE html PUBLIC &#39;-//W3C//DTD HTML 4.01 Strict//EN&#39; &#39;http://www.w3.org/TR/html4/strict.dtd&#39;>
<html>
<head>
<meta http-equiv=&#39;Content-Type&#39; content=&#39;text/html; charset=utf-8&#39;>
<title><?php echo $title; ?></title>
</head>
<body>
  <h1><?php echo $headline; ?></h1>
  <?php $this->load->view($include)?>
</body>
</html>

其中:

this−>load−>view(include);

包含的是另外一個檢視檔案studen_index.php檔案

student_index.php:

<p>Congratulations. Your initial setup is complete!</p>

共同輸出:

welcome to the classroom Mangement System
Congratulations. Your initial setup is complete!

資料的CURD

C 控制器

#先來看看資料增加的過程,在student控制器中增加一個add()方法

class student extends CI_Controller{
    //student controller
    public function construct(){
      parent::construct();
    }
    //new add function
    public function add(){
      $this->load->helper(&#39;form&#39;);
      //display information for the view
      $data[&#39;title&#39;]=&#39;Classroom:Add Page&#39;;
      $data[&#39;headline&#39;]=&#39;Add data&#39;;
      $data[&#39;include&#39;]=&#39;student_add&#39;;
      //upload view
      $this->load->view(&#39;template&#39;,$data);
    }
    //create function
    public function create(){
      $this->load->helper(&#39;url&#39;);
      $this->load->model(&#39;MStudent&#39;,&#39;&#39;,TRUE);
      $this->MStudent->addData($_POST);
      redirect(&#39;student/add&#39;,&#39;reflesh&#39;);
    }
    //update function
    public function update(){
      //upload codeigniter library
      $this->load->library(&#39;table&#39;);
      $this->load->model(&#39;MStudent&#39;,&#39;&#39;,TRUE);
      $student_query=$this->MStudent->updateData();
      $update_table=$this->table->generate($student_query);
      //display information for the view
      $data[&#39;title&#39;]=&#39;Classroom:Update Page&#39;;
      $data[&#39;headline&#39;]=&#39;Update Page&#39;;
      $data[&#39;include&#39;]=&#39;update_student&#39;;
      $data[&#39;updatetable&#39;]=$update_table;
      $this->load->view(&#39;template&#39;,$data);
    }
    //index function
    public function index(){
      $data[&#39;title&#39;]=&#39;Classroom:Home page&#39;;
      $data[&#39;headline&#39;]=&#39;welcome to classroom Mangement System&#39;;
      $data[&#39;include&#39;]=&#39;student_index&#39;;
      $this->load->view(&#39;template&#39;,$this->arraydata);
    }
}

V 視圖

template .php

<html>
  <head>
    <title><?php echo $title;?></title>
  </head>
  <body>
    <h1><?php echo $headline ?></h1>
    <?php $this->load->view($include)?>
  </body>
</html>

student_add.php

<?php
  echo form_open(&#39;student/create&#39;);
  $field_name=array(&#39;s_name&#39;,&#39;p_name&#39;,&#39;address&#39;,&#39;city&#39;,&#39;state&#39;,&#39;zip&#39;,&#39;phone&#39;,&#39;email&#39;);
  foreach($field_name as $value){
    echo "<p>".$value.":"
    echo form_input(array(&#39;name&#39;=>$value));
    echo "</p>"
  }
  form_submit(&#39;&#39;,&#39;Add&#39;);
  form_close();
?>

update_student.php

<?php
  echo $updatetable;
?>

M 模型

class MStudent extends CI_Model{
  public function addData($data){
    $this->db->insert(&#39;student&#39;,$data);
  }
  public function updateData(){
    $this->db->get(&#39;student&#39;);
  }
}

以上是詳解基於Codeigniter框架實作student資訊系統網站動態發布功能程式碼案例的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn