用户
 找回密码
 入住 CI 中国社区
搜索
查看: 13248|回复: 16
收起左侧

CodeIgniter编写的简单留言板的DEMO

[复制链接]
发表于 2013-2-8 11:19:20 | 显示全部楼层 |阅读模式
本帖最后由 Mr.Xu 于 2013-2-8 11:23 编辑

    小弟我第一次学习php,第一次用codeigniter编写demo,自己动手学习写东西总能够发现很多新知识。一下是小弟写的一个留言板的demo,请大家多多指教。

1、留言板的model类,与数据交互。
PHP复制代码
 
<?php
/**
 * 描述:留言板model类
 * author:xuzw13@gmail.com
 * */

class Guestbook_model extends CI_Model{
        const TBL_GB = 'guestbook';
 
        function __construct()
        {
                parent::__construct();
                $this->load->database();
        }
 
        function getGuestbooks()
        {
                $sql = "SELECT * FROM ".self::TBL_GB;
                $query = $this->db->query($sql);
                //返回条数
                $numRow = $query->num_rows();
        $resultRow = $query->result_array();
                return array('numRow'=>$numRow,'resultRow'=>$resultRow);
        }
 
        function get_guestbook($_id)
        {
                $sql = "SELECT * FROM ".self::TBL_GB." where id=?";
                $query = $this->db->query($sql,array($_id));
                return $query->result_array();
        }
 
    /*query方式实现插入*/
    function add_guestbook($arrs)
    {
            $sql = "insert into ".self::TBL_GB."(title,content,addtime) values (?,?,now())";
                $query = $this->db->query($sql,$arrs);
                 return  $this->db->affected_rows();
    }
 
    /*query方式实现插入*/
    function add_guestbook_active($arrs)
    {
        $this->db->insert(self::TBL_GB,$arrs);
        return  $this->db->affected_rows();
    }
 
    function edit_guestbook($arrs)
    {
            $sql = "update ".self::TBL_GB." set title=?,content=? where id=?";
                $query = $this->db->query($sql,$arrs);
                return  $this->db->affected_rows();
    }
 
    function delete_guestbook($_id)
    {
            $this->db->where('id',$_id);
            $this->db->delete(self::TBL_GB);
    }
 
}
 
/*End of guestbook_model.php */
/*Location: /application/models/guestbook_model.php */
 
复制代码



2、留言板的controller类,处理页面请求等。
PHP复制代码
 
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * 描述:留言板controller类
 * author:xuzw13@gmail.com
 * */

class Guestbook extends CI_Controller{
 
    public function __construct()
    {
        parent::__construct();
        $this->load->helper(array('form','url'));        //加载辅助函数
        $this->load->model('Guestbook_model');
        $this->load->library('form_validation'); //表单验证类
    }
 
        function index()
        {
                header("Content-type:text/html;charset=utf-8");
 
                $data['title'] = 'CI-留言板首页';
 
        $result = $this->Guestbook_model->getGuestbooks();
        $data['guestbooks'] = $result;
 
                $this->load->view('guestbook/header',$data);
                $this->load->view('guestbook/index',$data);
                $this->load->view('guestbook/footer.html');
        }
 
    /*添加留言界面*/
        function add()
        {
        header("Content-type:text/html;charset=utf-8");
 
                $data['title'] = 'CI-留言板首页';
 
                $this->load->view('guestbook/header',$data);
                $this->load->view('guestbook/add');
                $this->load->view('guestbook/footer.html');
        }
 
      /*添加留言数据处理*/
        function add_guestbook()
        {
        header("Content-type:text/html;charset=utf-8");
 
                $data['title'] = 'CI-留言板首页';
 
        $this->form_validation->set_rules('title','Title','required');
        $this->form_validation->set_rules('content','Content','required');
 
        if($this->form_validation->run() == FALSE)
        {
                $data['title'] = 'CI-留言板首页';
                $data['errors'] = validation_errors();
 
                    $this->load->view('guestbook/header',$data);
                    $this->load->view('guestbook/add');
                    $this->load->view('guestbook/footer.html');
        }
        else
        {
                /*$data = array($this->input->post('title'),$this->input->post('content'));
                 $result = $this->Guestbook_model->add_guestbook($data);*/

                 $data = array('title' => $this->input->post('title'),
                         'content' => $this->input->post('content'));  
                 $result = $this->Guestbook_model->add_guestbook_active($data);            
            redirect(site_url('guestbook'));
        }
                 
        }
 
    /*修改页面*/
        function edit($_id)
        {
                header("Content-type:text/html;charset=utf-8");
 
                $data['title'] = 'CI-留言板修改信息页面';
 
        $result = $this->Guestbook_model->get_guestbook($_id);
 
        $data['result'] = $result[0];
                $this->load->view('guestbook/header',$data);
                $this->load->view('guestbook/edit',$data);
                $this->load->view('guestbook/footer.html');
 
        }
 
    /**
     * 修改留言信息
     * */

        function edit_guestbook()
        {
        header("Content-type:text/html;charset=utf-8");
 
                $data['title'] = 'CI-留言板首页';
 
        $this->form_validation->set_rules('title','标题','required');
        $this->form_validation->set_rules('content','内容','required');
 
        if($this->form_validation->run() == FALSE)
        {
                $data['title'] = 'CI-留言板首页';
            $data['errors'] = validation_errors();
                    $this->load->view('guestbook/header',$data);
                    $this->load->view('guestbook/edit');
                    $this->load->view('guestbook/footer.html');
        }
        else
        {
                $data = array($this->input->post('title'),
                                  $this->input->post('content'),
                                  $this->input->post('hid'));
 
                 $result = $this->Guestbook_model->edit_guestbook($data);
                 
              redirect(site_url('guestbook'));
        }
 
        }
 
    /*删除留言信息*/
        function delete($_id)
        {
                 $result = $this->Guestbook_model->delete_guestbook($_id);
                 
         redirect(site_url('guestbook'));
        }
}
 
/*End of guestbook.php */
/*Location: /application/controllers/guestbook.php */
 
复制代码


以上是简单留言板核心的代码,很多地方写的不好,请大家多多指教。
以下是源码,请大家多多指教!{:soso_e129:}{:soso_e129:}{:soso_e129:}

ci_guestbook.rar (416.01 KB, 下载次数: 283)

demo图片

demo图片





本帖被以下淘专辑推荐:

发表于 2014-7-12 09:39:20 | 显示全部楼层
学习了,谢谢楼主分享
发表于 2014-7-13 13:56:17 | 显示全部楼层
不错,值得新手学习
发表于 2013-2-8 13:07:46 | 显示全部楼层
基本的CRUD
发表于 2013-2-8 13:26:37 | 显示全部楼层
不错,学习了
 楼主| 发表于 2013-2-8 20:36:12 | 显示全部楼层
我是新手 发表于 2013-2-8 13:07
基本的CRUD

是啊 ..基本的crud
发表于 2013-2-8 20:38:20 | 显示全部楼层
新浪微博的雏形。。。哈哈哈哈哈
 楼主| 发表于 2013-2-8 20:38:54 | 显示全部楼层
topwaybobo 发表于 2013-2-8 13:26
不错,学习了

请多多指教
 楼主| 发表于 2013-2-8 21:08:25 | 显示全部楼层
跟屁虫 发表于 2013-2-8 20:38
新浪微博的雏形。。。哈哈哈哈哈

很简单curd操作而已哈。。。
发表于 2013-2-8 23:44:50 | 显示全部楼层
从代码来看,似乎任何人都可以删除任何人的评论吧?
 楼主| 发表于 2013-2-12 18:25:04 | 显示全部楼层
Altair 发表于 2013-2-8 23:44
从代码来看,似乎任何人都可以删除任何人的评论吧?

恩。。没有做用户管理的。。。所以谁都可以
 楼主| 发表于 2013-2-12 18:25:21 | 显示全部楼层
hcmtl 发表于 2013-2-9 10:28
恩,刚刚会用..努力吧.。这东西很简单....

请多多指教

本版积分规则