基于Codeigniter框架实现的student信息系统站点动态发布功能详解
本文实例讲述了基于Codeigniter框架实现的student信息系统站点动态发布功能。分享给大家供大家参考,具体如下:
既然是动态站点,肯定有数据库表的存在,在此不废话,下面我们来看一下数据库表:
CREATETABLEIFNOTEXISTS`student`( //主键id `id`int(11)NOTNULLAUTO_INCREMENT, //学生姓名 `s_name`varchar(64)NOTNULL, //学生家长的姓名 `p_name`varchar(64)NOTNULL, //学生的家庭住址 `address`varchar(100)NOTNULL, //所在城市 `city`varchar(30)NOTNULL, //所在国家 `state`varchar(30)NOTNULL, //所在地区的邮政编码 `zip`varchar(20)NOTNULL, //电话 `phone`varchar(15)NOTNULL, //邮件 `email`varchar(20)NOTNULL, //主键设置 PRIMARYKEY(`id`) )ENGINE=INNODBDEFAULTCHARSET=UTF8AUTO_INCREMENT=1;
*注:在此我有两个地方需要解释一下:
1."IFNOTEXISTS":如果数据在创建表的时候,在前面加上了"IFNOTEXISTS",那就表明即使此表已经存在,也会执行成功;
2."ENGINE=INNODB":这个是数据库的引擎设置,常用mysql数据库引擎有ISAM,MYISAM,HEAP等;
具体参考资料:http://baike.baidu.com/view/68455.htm
在创建完数据表之后,我们再来看一下数据库的连接。打开.\application\config\database.php文件,在内设置数据库变量参数,在.\application\config\config.php文件内设置基本的URL,对于我的基本url是:http://localhost/codeigniter/
下面我们来看看mvc思想架构的设计
首先打开.application\controllers\文件目录,在里面创建一个student.php控制器:
student.php
在此我们先来通过student这个控制器来测试一下,打印出helloworld,记住访问路径是:http://localhost/codeigniter/index.php/student/index
classstudentextendsCI_Controller{
//studentcontrollerconstruct
publicfunction__construct(){
parent::__construct();
}
//indextestfunction
publicfunctionindex(){
echo"helloworld";
}
}
itoutput:helloworld
下面我们来换一下,看看下面这段code:
classstudentextendsCI_Controller{
//studentcontroller
publicfunction__construct(){
parent::__construct();
}
//defineaarray,nameisarraydata,ithavethreeparameters
protected$arraydata=array(
'title'=>'Classroom:Homepage',
'headline'=>'welcometotheclassroomMangementSystem',
'include'=>'student_index'
);
//indexfunction
publicfunctionindex(){
$this->load->view('template',$this->arraydata);
}
}
这段代码需要一个视图,template.php
template.php:
load->view($include)?>
其中:
this−>load−>view(include);
包含的是另外一个视图文件studen_index.php文件
student_index.php:
Congratulations.Yourinitialsetupiscomplete!
联合输出:
welcometotheclassroomMangementSystem Congratulations.Yourinitialsetupiscomplete!
数据的CURD
C控制器
先来看看数据的增加过程,在student控制器中增加一个add()方法
classstudentextendsCI_Controller{
//studentcontroller
publicfunction__construct(){
parent::__construct();
}
//newaddfunction
publicfunctionadd(){
$this->load->helper('form');
//displayinformationfortheview
$data['title']='Classroom:AddPage';
$data['headline']='Adddata';
$data['include']='student_add';
//uploadview
$this->load->view('template',$data);
}
//createfunction
publicfunctioncreate(){
$this->load->helper('url');
$this->load->model('MStudent','',TRUE);
$this->MStudent->addData($_POST);
redirect('student/add','reflesh');
}
//updatefunction
publicfunctionupdate(){
//uploadcodeigniterlibrary
$this->load->library('table');
$this->load->model('MStudent','',TRUE);
$student_query=$this->MStudent->updateData();
$update_table=$this->table->generate($student_query);
//displayinformationfortheview
$data['title']='Classroom:UpdatePage';
$data['headline']='UpdatePage';
$data['include']='update_student';
$data['updatetable']=$update_table;
$this->load->view('template',$data);
}
//indexfunction
publicfunctionindex(){
$data['title']='Classroom:Homepage';
$data['headline']='welcometoclassroomMangementSystem';
$data['include']='student_index';
$this->load->view('template',$this->arraydata);
}
}
V视图
template.php
load->view($include)?>
student_add.php
".$value.":"
echoform_input(array('name'=>$value));
echo""
}
form_submit('','Add');
form_close();
?>
update_student.php
M模型
classMStudentextendsCI_Model{
publicfunctionaddData($data){
$this->db->insert('student',$data);
}
publicfunctionupdateData(){
$this->db->get('student');
}
}
更多关于CodeIgniter相关内容感兴趣的读者可查看本站专题:《codeigniter入门教程》、《CI(CodeIgniter)框架进阶教程》、《php优秀开发框架总结》、《ThinkPHP入门教程》、《ThinkPHP常用方法总结》、《ZendFrameWork框架入门教程》、《php面向对象程序设计入门教程》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》
希望本文所述对大家基于CodeIgniter框架的PHP程序设计有所帮助。