首页 > 其他 > 详细

CI 模型的使用M与C之间的调用

时间:2015-03-04 22:39:25      阅读:563      评论:0      收藏:0      [点我收藏+]

CI是PHP一个比较轻,并且好用的一个框架

分层也是分的比较清晰的一个

这里先展示MODEL 放在application/models 目录下面user_model.php

<?php
class User_model extends CI_Model {

	public function __construct() {
     //parent::__construct();
     $this->load->database();
	}
	//查询全部的数据
	public function getAll(){
		
		
		 $query = $this->db->get(‘staff‘);
		 echo ‘My-model‘;
		 echo ‘最后一条sql语句是:‘.$this->db->last_query();
		 return $query->result_array();
		 
		
	}
	//条件的查询
	public function GetLastList(){
		  $query = $this->db->select(‘id,admin‘)
          ->from(‘b_staff‘)
          ->where(‘id =‘,8)
          //->where(‘id >=‘,8)
          //->limit(0,2)
          ->order_by(‘id desc ‘)
          ->get();
        $rs = $query->result_array();
        echo ‘最后一条sql语句是:‘.$this->db->last_query();
		return $rs;
		
	}
	//添加数据
	public function insertUser(){
		$data[‘admin‘] = ‘sunzhiiyan0304‘;
        $data[‘paw‘] = ‘123456‘;
        $insertbool = $this->db->insert(‘staff‘,$data);
        return $insertbool;
	}
	//修改数据
	public function UpdateUser(){
		$data[‘admin‘] = ‘sunzhiiyan0304‘;
        $data[‘paw‘] = ‘123456‘;
		$updatebool = $this->db->update(‘staff‘,$data,array(‘id‘=>5));
		return $updatebool;
	}
	//删除
	public function DeleteUser(){
		  $delbool = $this->db->delete(‘staff‘,array(‘id‘=>2));
		  return $delbool;
	}
	//查询---通过sql语句,不运用封装
	public function shower(){
		//装载数据库操作类
		$this->load->database();
		//装载成功后,会放入超级对象的属性中
		//var_dump($this->db);
		$sql = "select * from b_staff ";		
		$rs = $this->db->query($sql);  //返回的不是资源,对象
		
		$list = $rs->result();  //这里返回的是数组,然后
/*		var_dump($list);
		var_dump($this->input);
		var_dump($this->db);
		var_dump($this->uri);
		var_dump($this->lang);
		var_dump($this->db->model);
		var_dump($this->router);*/
		return $list;
	}
	//新增---通过sql语句,不运用封装
	public function insertSql(){
	  $this->load->database();
//	  	$sql = "select * from b_staff ";		
//		$rs = $this->db->query($sql);  //返回的不是资源,对象
//		
//		var_dump($rs);
//		$list = $rs->result();  //这里返回的是数组,然后
//		var_dump($list);
       // $_POST[‘name‘] = ‘sunzhiyan‘;
        //$name = $this->input->post(‘name‘);
        //echo ‘$name=‘.$name;
        echo ‘sinmzhiayn‘;
        $data[‘admin‘] = ‘SSSSS‘;
        $data[‘paw‘] = ‘123456‘;
        $sql = "insert into b_staff (admin,paw)values(?,?)";
        $this->db->query($sql,$data);
	}
}


?>

 命名还是有要求的,这里就不细说了,主要讲的是一个分层和调用的问题

下面是application/controllers 目录下面的welcome.php 页面

通过http://localhost/CI0.1/index.php/welcome/index

能够进行访问

同事,要想访问这个aciton下面的其他方法的时候

只要需要在http://localhost/CI0.1/index.php/welcome/index 后面将index改成要访问的action调用方法名即可

<?php if ( ! defined(‘BASEPATH‘)) exit(‘No direct script access allowed‘);

class Welcome extends MY_Controller {

	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/welcome
	 *	- or -  
	 * 		http://example.com/index.php/welcome/index
	 *	- or -
	 * Since this controller is set as the default controller in 
	 * config/routes.php, it‘s displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see http://codeigniter.com/user_guide/general/urls.html
	 */
	public function index()
	{
		echo ‘-----------------------‘;
		//数据库已经自动加载了
		//其实这里可以不同加载,因为调用model的时候,只要让构造方法去调用就行了
		$this->load->database();

		/*
       //装载分页类
       $this->load->library(‘pagination‘);
       //url路由类
       $this->load->helper(‘url‘);
       //进行配置
       //每页显示数据
       $page_size = 2;
       //配置基础目录
       $config[‘base_url‘]= site_url(‘welcome/index‘);
       //一共多少条数据
       $config[‘total_rows‘] =7;
       //首页
       $config[‘first_link‘] = ‘SHOU‘;
       $config[‘next_link‘] = ‘XIA‘;
       //每页显示的数据
       $config[‘per_page‘] = $page_size;
       $this->pagination->initialize($config);
       //输出按钮
       echo $this->pagination->create_links();
       
       $offset = intval($this->uri->segment(3));
      $rs = $this->db->query($sql);
      $list = $rs->result(); 
      var_dump($list);*/
       
       
        $this->load->model(‘User_model‘);
        //查询
        $list = $this->User_model->getAll();
        //新增
        $bool = $this->User_model->insertUser();
        //修改
       	$updatebool =  $this->User_model->UpdateUser();
       	//删除
       	$delbool =  $this->User_model->DeleteUser();
        //条件查询
        $res =  $this->User_model->GetLastList();
        //通过sql语句进行查询
        $shower =  $this->User_model->shower();
        //通过sql语句进行新增
        $this->User_model->insertSql();
        
/*        var_dump($list);
		var_dump($bool);
		var_dump($updatebool);
		var_dump($delbool);*/
		//var_dump($shower);
		
		echo ‘<br>------------------------------------<br>‘;	
		var_dump($res);		
        //$this->aabbcc();
		$this->load->view(‘welcome_message‘);
	}
	public function test()
	{
	  echo ‘sunzhiyan111‘;
	  $this->load->view(‘nihao‘);
	}


	public function login(){
	 echo ‘login‘;
	 echo md5(uniqid());
	 //加载session
	 $this->load->library(‘session‘);
	 $user = array(‘id‘=>3,‘name‘=>‘sunzhiyan‘);
	 //注册session
	 $this->session->set_userdata(‘user‘,$user);
	 //不要在这里获取刚放入的数据
	 //只有天页面从新加载后才能获取到
	 
	 
	 
	}
	public function checjlogin(){
	  $this->load->library(‘session‘);
	  $this->session->userdata(‘user‘);
	  var_dump($user);
	}
	public function code(){
	 $this->load->helper(‘url‘);
	 $this->load->helper(‘captcha‘);
	 $vals = array(
 //   ‘word‘ => ‘Random word‘,  /使用自己定义的字符串/
    ‘img_path‘ => ‘./captcha/‘,   //此目录手动创建
    ‘img_url‘ => base_url().‘/captcha/‘,
	 
//    ‘font_path‘ => ‘./path/to/fonts/texb.ttf‘,
//    ‘img_width‘ => ‘150‘,
//    ‘img_height‘ => 30,
//    ‘expiration‘ => 60*10  //过期时间,时间一到自动删除图片
	);
	
	$cap = create_captcha($vals);
	echo $cap[‘word‘];   //验证码中的字符串
	echo $cap[‘image‘];  //输出验证码
	//将验证码放入到session中,验证就通过此相等做
	 
	}
	
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */

 

CI 模型的使用M与C之间的调用

原文:http://www.cnblogs.com/sunxun/p/4314440.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!