CI 2.2.0可以使用AR模式操作Oracle 10g資料庫

來源:互聯網
上載者:User

標籤: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>


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.