首页 > 代码库 > CI 2.2.0可以使用AR模式操作Oracle 10g数据库

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

一、控制器

<?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 = http://www.mamicode.com/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=http://www.mamicode.com/‘‘ />


        <p>Title:<input type=‘text‘  name=‘title‘ value=http://www.mamicode.com/‘‘ />


        <p>Content:<input type=‘text‘  name=‘content‘ value=http://www.mamicode.com/‘‘ />


        <input type=‘hidden‘ name=‘createtime‘ value=http://www.mamicode.com/"" />

        <p><input type=‘submit‘ value=http://www.mamicode.com/‘Add Topics‘ />


        </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=http://www.mamicode.com/"<?php echo $this->uri->segment(3) ?>" />


        <p>Title:<input type=‘text‘ name=‘title‘ value=http://www.mamicode.com/"<?php echo $result[0]->TITLE ?>" />


        <p>Content:<input type=‘text‘ name=‘content‘ value=http://www.mamicode.com/"<?php echo $result[0]->CONTENT ?>" />


        <p>Createtime:<input type=‘text‘ name=‘createtime‘ value=http://www.mamicode.com/"<?php echo $result[0]->CREATETIME ?>" />


        
        <p><input type=‘submit‘ value=http://www.mamicode.com/‘Save Topics‘ />


        </form>
        <p><?php echo anchor(‘topics/index‘, "Back to topics‘s Index page"); ?></p>
    </body>
</html>