1.根据前面学习tp框架的经验,上手ci框架就非常快,都是采用mvc这种架构,以及单文件入口。
2.不同之处,
数据库调用,需要加载,才能使用
$this->load->database();
$query = $this->db->query('SELECT name, title, email FROM my_table') foreach ($query->result() as $row) { echo $row->title; echo $row->name; echo $row->email; } echo 'Total Results: ' . $query->num_rows();
配置在这个配置文件位于 application/config/database.php目录,
参数模式:模块/控制器/方法/参数/参数
接收:方法(参数,参数)
3.头部
//./applications/controllers/hello.php //放止用户直接通过路径来访问控制器,如果这样的话会显示找不到(封装)
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
4.数据库
对数据库的操作——
1.连接数据库($this->load->database());
2.插入数据($this->db->insert($t_name,$arr);)
$t_name——你要操作的表]
$arr——你要插入的数据("key"=>value)
3.更新数据
$this->db->where(字段名,字段值)
$this->db->update(表名,修改值的数组)
4.查询数据
$this->db->where(字段名,字段值)
$this->db->select(字段)
$query = $this->db->get(表名)
return $query->result();
5.删除数据
$this->db->where(字段名,字段值)
$this->db->delete(表名)
5.上传
a.定义一个数组,设置一些与上传相关的参数
//设置上传目录,这里写./,目录要建在网站根目录,就是和application同级 //如果你要放在application目录下,可以用系统定义的路径常量APPPATH //例如:APPPATH.'uploads/' $config['upload_path'] = './uploads/'; //设置允许上传的类型 $config['allowed_types'] = 'gif|jpg|png'; $config['max_size'] = '100'; //如果是图片还可以设置最大高度和宽度 $config['max_height'] = 768; $config['max_width'] = 1024;
load->view('up');
}
//显示上传信息
public function up(){
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = "2000";
$this->load->library('upload',$config);
//打印成功或错误的信息
if($this->upload->do_upload('upfile'))
{
$data = array("upload_data" => $this->upload->data());
var_dump($data);
}
else
{
$error = array("error" => $this->upload->display_errors());
var_dump($error);
}
}
}
6.登录验证
./application/controllers/login.php
load->view('login.html');
}
public function checklogin(){
$this->load->model("test_m");
$user = $this->test_m->user_select($_POST['uname']);
if($user){
if($user[0]->upass == $_POST['upass']){
echo '密码正确!';
$this->load->library('session');
$arr = array("uid" => $user[0]->usid);
$this->session->set_userdata($arr);
echo "
";
echo $this->session->userdata('uid');
}
else{
echo '密码不正确!';
}
}
else{
echo '用户名不存在';
}
}
public function checksession(){
$this->load->library('session');
if($this->session->userdata('uid')){
echo '已经登录。';
}
else{
echo '没有登录。';
}
}
public function loginout(){
$this->load->library('session');
$this->session->unset_userdata('uid');
}
}
7.分页功能
1.必须知道的一些参数
a.总共有多少条记录
b.一页要有多少条记录
c.总共多少页
d.当前页前后要显示多少个分页链接
2.设置一些CI分页类基本参数
//总条数
$config['total_rows']
//一页显示几条
$config['per_page']
//定义当前页的前后各有几个数字链接
$config['num_links']
//定义没有分页参数,主URL
$config['base_url']
3.调用CI的分页类
$this->load->library('pagination');
4.执行分页方法
$this->pagination->initialize($config);
5.输出分页链接
echo $this->pagination->create_links();
6.查询部分数据(limit)
echo $this->db->limit($num,$start); //从$start查$num条
./applications/controllers/page.php
load->model('test_m');
for ($i = 1;$i <= 100;$i++){
$name = 'u'.$i;
$arr = array("usid"=>$i,"uname"=>$name,"upass"=>123456);
$this->test_m->user_insert($arr);
}
}
public function pagelist(){
$this->load->model('test_m');
$user = $this->test_m->user_select_all();
$allnum = count($user);
$pagenum = 20;
$config['total_rows'] = $allnum;
$config['per_page'] = $pagenum;
$config['num_links'] = 3;
$config['base_url'] = "/CI/index.php/page/pagelist";
$config['use_page_numbers'] = true;
$this->load->library('pagination');
$this->pagination->initialize($config);
var_dump($this->pagination->create_links());
echo $this->pagination->create_links();
echo "
";
$id = $this->uri->segment(3); //获得url第三段字符
$id =$id ? $id:1;
$start = ($id - 1) * $pagenum;
$list = $this->test_m->user_select_limit($start,$pagenum);
var_dump($list);
}
}