码迷,mamicode.com
首页 > 数据库 > 详细

CI 2.2.0可以使用AR模式操作Oracle 10g数据库

时间:2014-08-19 01:04:43      阅读:309      评论:0      收藏:0      [点我收藏+]

标签:style   http   color   使用   os   io   数据   for   

一、控制器

<?php

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

class Topics extends CI_Controller
{

    function __construct()
    {
        parent::__construct();
        $this->load->helper(‘url‘);
        $this->load->helper(‘form‘);
        //注意,database、 session已经自动加载,在以后的过程中,直接使用即可
    }

    function index()
    {
        $this->db->order_by(‘id‘, ‘asc‘);
        $query = $this->db->get(‘topics‘, 20);
        if ($query->num_rows() > 0)
        {
            $data[‘result‘] = $query->result();
        }
        else
        {
            $data[‘result‘] = ‘‘;
        }
        $this->load->view(‘/topics/index‘, $data);
    }

    public function add()
    {
        $this->load->view(‘topics/add‘);
    }

    public function addpost()
    {
        if (trim($this->input->post(‘content‘)) != ‘‘ && trim($this->input->post(‘title‘)) != ‘‘)
        {
            //方法一
//            $sql = ‘insert into TOPICS(id,title,content) values(?,?,?)‘;
//            $bool = $this->db->query($sql, array($this->input->post(‘id‘),
//                 $this->input->post(‘title‘),
//                 $this->input->post(‘content‘))
//            );
//            if ($bool)
//            {
//                redirect(‘topics/index‘, ‘location‘, 301);
//            }

//            方法二:
            $data = array(
                ‘id‘=>  $this->input->post(‘id‘),
                ‘content‘ => $this->input->post(‘content‘),
                ‘title‘ => $this->input->post(‘title‘));
            if ($this->db->insert(‘topics‘, $data))
            {

                redirect(‘topics/index‘, ‘location‘, 301);
            }
        }
    }

    function edit()
    {
        //此处直接从地址栏中取得参数,即ID参数
        $id = $this->uri->segment(3);

        if ($id != ‘‘)
        {
            if (!$this->input->post(‘title‘))
            {
                $query = $this->db->get_where(‘topics‘, array(‘id‘ => $id));
                $data[‘result‘] = $query->result();
                $this->load->view(‘topics/edit‘, $data);
            }
            else
            {
                $this->db->where(‘id‘, $id);
                if ($this->db->update(‘topics‘, $_POST))//注意,此处直接使用了$_POST系统变量
                {
                    redirect(‘topics/index‘, ‘location‘);
                }
            }
        }
    }

    //此语句可以操作oracle
    function del_topics($id)
    {
        if (!$id)
        {
            redirect(‘topics/index‘);
        }
        else
        {
            $this->db->where(‘id‘, $id);
            if ($this->db->delete(‘topics‘))
            {
                redirect(‘topics/index‘);
            }
        }
    }

    //此语句可以操作oracle
    function drop_table()
    {
        $this->load->helper(‘url‘);
        if ($this->db->empty_table(‘topics‘))
        {
            redirect(‘topics/index‘);
        }
    }

}

?>



二、视图add.php  edit.php

(一)add.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>Add Topics</title>
    </head>
    <body>
        <h3>Topics:</h3>
        <?php echo form_open(‘topics/addpost‘); ?>
        <p>ID:<input type=‘text‘  name=‘id‘ value=‘‘  /></p>
        <p>Title:<input type=‘text‘  name=‘title‘ value=‘‘ /></p>
        <p>Content:<input type=‘text‘  name=‘content‘ value=‘‘ /></p>
        <input type=‘hidden‘ name=‘createtime‘ value="" />

        <p><input type=‘submit‘ value=‘Add Topics‘ /></p>
        </form>
        <p>
            <?php echo anchor(‘topics/index‘, "Back to Topics‘s index page"); ?>
        </p>
    </body>
</html>


(二)edit.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>Edit Topics</title>
    </head>
    <body>
        <?php echo form_open(‘topics/edit/‘ . $this->uri->segment(3)); ?>
        <p>ID:<input type=‘text‘ name=‘id‘ value="<?php echo $this->uri->segment(3) ?>" /></p>
        <p>Title:<input type=‘text‘ name=‘title‘ value="<?php echo $result[0]->TITLE ?>" /></p>
        <p>Content:<input type=‘text‘ name=‘content‘ value="<?php echo $result[0]->CONTENT ?>" /></p>
        <p>Createtime:<input type=‘text‘ name=‘createtime‘ value="<?php echo $result[0]->CREATETIME ?>" /></p>
        
        <p><input type=‘submit‘ value=‘Save Topics‘ /></p>
        </form>
        <p><?php echo anchor(‘topics/index‘, "Back to topics‘s Index page"); ?></p>
    </body>
</html>


CI 2.2.0可以使用AR模式操作Oracle 10g数据库,布布扣,bubuko.com

CI 2.2.0可以使用AR模式操作Oracle 10g数据库

标签:style   http   color   使用   os   io   数据   for   

原文地址:http://my.oschina.net/alkz/blog/304125

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!