PHP头条
热点:

[CI]CodeIgniter快速开发指南,cicodeigniter


---------------------------------------------------------------------------------------------------------

使用CI以来最强烈的感受是其彻底的MVC设计, 举个例子 : 在application/modesl目录里, 写我们的模型操作, 统一继承CI_Model.

而在控制器里只写逻辑, 无法直接操作数据库, 需要数据直接调用模型, 最后是调用模板.

 

下面分别展示模型, 控制器, 和视图间的协作.

/**
 * 用户模型, 完整CURD示例
 * @Chenwei
 */
class User_model extends CI_model
{
public function __construct() { parent::__constrcut(); } /** * 查询用户信息, 这里不建议使用单一id参数作为条件, 为了便于控制器自己组装条件复用此模型方法 * @param array 格式如: $where = array('id'=>1); * @return array */ public function userInfo($where = array()) { if($where && is_array($where)) { $res = $this->db->select('id, username, age')->where($where)->get('users'); return $res->result_array(); //以二维数组形式返回结果 } else {
       $res = $this->db->select('id, username, age')->get('users');
return $res->result_array(); } } /** * 添加用户 * @param array 格式如: $data = array('username'=>'Chenwei', 'age'=>'18'); * @reteurn bool */ public function userAdd($data) { if($data && is_array($data)) { $bool = $this->db->insert('users', $data); return $bool; } else { return false; } } /** * 删除用户 * @param int $id * @reteurn bool */ public function userDel($id) { if($id) { $where = array('id'=>$id); $bool = $this->db->where($where)->delete('users'); return $bool; } else { return false; } } /** * 修改用户 * @param array $where 条件 * @param array $data 新数据 * @reteurn bool */ public function userEdit($where, $data) { if($where && is_array($where)) { $bool = $this->db->where($where)->update('users', $data); return $bool; } else { return false; } } } /** * 几点注意: * 1. 模型类名字User_model首字母大写, 其余字母小写, 继承基础模型类CI_Model * 2. 类文件名 application/models/user_model.php * 3. 控制器中如何载入此模型 :
    $this->load->model('User_model', 'user'); 这是以user为对象名引入;
    $this->load->model('User_model'); 这是默认以User_model为对象名引入. 模型文件支持子目录;
    如果类文件在application/models/blog/user_model.php中, 可以这样引入: $this->load->model('blog/User_model'); * 4. 如果有需要, 你可以设置自动加载, 在 application/config/autoload.php文件中.
 * 5. 如果没有设置自动连接数据库, 加在模型的时候可以设置连接, 像这样 $this->load->model('User_model', '', TRUE);
*/

Ps:
这里是一个联合查询的例子, 有需要可以尝试:
$res = $this->db->select('p.id, p.uid, p.order_no, p.amount, p.pay_way, p.pay_type, p.pay_bank, p.pay_time, p.goods_type, p.contact_tel, p.detail_desc, p.add_time, u.username')->from('payment as p')->join('users as u', 'p.uid = u.id')->order_by('p.id', 'desc')->get();

 

/**
 * 用户控制器, CURD示例
 * @Chenwei
 */
class Users extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();

        $this->load->model('User_model', 'user');
    }
    
    /**
     * 用户列表
     */
    public function index()
    {
        $data['user_list'] = $this->user->userInfo();

        $this->load->view('user_list', $data);  //调用模板, 并将数据输出到前台
    }

    /**
     * 添加用户
     */
    public function user_add()
    {
        $data = array(
            'username'=>$this->input->post('name');
            'age'=>intval($this->input->post('age'));
        );

        $bool = $this->user->userAdd($data);

        if($bool)
        {
           $this->show_tips('操作成功 !');
        }
        else
        {
            $this->show_tips('操作失败 !');
        }
    }

    /**
     * 修改用户
     */
    public function user_edit()
    {
        $id = $this->input->post('id');

        $data = array(
            'username'=>$this->input->post('name');
            'age'=>intval($this->input->post('age'));
        );

        if($id)
        {
       $where = array('id'=>$id);
$bool = $this->user->userEdit($where, $data); if($bool) { $this->show_tips('操作成功 !'); } else { $this->show_tips('操作失败 !'); } } else { $this->show_tips('非法操作 !'); } } /** * 删除用户 */ public function user_del() { $id = $this->input->post('id'); $bool = $this->user->userDel($id); if($bool) { $this->show_tips('操作成功 !'); } else { $this->show_tips('操作失败 !'); } } } /**
 * 几点注意: * 1. 控制器文件在 application/controller/users.php , 支持子目录 * 2. 控制器名首字母必须大写, 且必须继承CI_Controller * 3. 前后台权限控制都在application/core/MY_Controller.php文件中,
    定义两个控制器, 分别用于前台和后台, 继承CI_Controller , 其余都只需继承这两个自定义的控制器即可. * 4. 定义默认控制器, 在 application/config/route.php
*/

 

/**
 * 视图层 示例
 * @Chenwei
 */
<?php
    $this->load->view('header');
?>

<!-- 简单的输出 -->
<div>
    <table>
        <?php if($user_list):?>
            <?php foreach($user_list as $v):?>
            <tr><td><?=$v['username'];?></td></tr>
            <?php endforeach;?>
        <?php endif;?>
    </table>
</div>

<?php
    $this->load->view('header');
?>

/**
 * 几点注意:
 * 1. 模板中可以直接使用控制器中分配的变量, 使用CI系统的所有函数和方法.
 * 2. 开启CI短标签支持后, 即使php未开启支持, CI也会帮我们自动解析, 可以放心使用.
 */

可能存在手误, 以上Code不要直接复制使用; 更多CI的实用用法, 可以随时去查阅CI手册.

 

Link: http://www.cnblogs.com/farwish/p/3991419.html

@黑眼诗人 <www.chenwei.ws>

www.phpzy.comtrue/php/31961.htmlTechArticle[CI]CodeIgniter快速开发指南,cicodeigniter --------------------------------------------------------------------------------------------------------- 使用CI以来最强烈的感受是其彻底的MVC设计, 举个例子 : 在applicat...

相关文章

PHP之友评论

今天推荐