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

小程序测试教程。。。。。

[复制链接]
发表于 2010-8-5 21:45:00 | 显示全部楼层 |阅读模式
看着手册写程序。。。。。。。。。。

《程序代码1》
视图层
test_view.php

HTML复制代码
<html>
<head>
<title>测试1</title>
</head>
<form action='Test/deal' method=post>
用户名:<input name=t1><br>
内容:<input name=t2><br>
<input type=submit value="提交">
</form>
</html>
复制代码

感想:手册中的表单辅助函数简直一文不值,这就好比以前学过的struts框架的标签库一样,强烈建议无视它,用常规的html标记写就行了。

控制层
test.php

PHP复制代码
<?php
        class Test extends Controller
        {
                function __construct()
                {
                        parent::Controller();
                        $this->load->database();
                }
                function index()
                {
                        $this->load->view('test_view');
                }
                function deal()
                {
                        $this->load->model('test_model');
                        $this->test_model->ins();
                }
        }
?>
复制代码


感想:看一下和程序代码2的不同点,这里没有获取post传的值。

模型层
test_model

PHP复制代码
<?php
        class test_model extends Model
        {
                function __construct()
                {
                        parent::Model();
                }
                function ins()
                {
                        $dat=array(
                                'name'=>$this->input->post('t1',true),                 
                                'content'=>$this->input->post('t2',true)
                                                );
                $this->db->insert('ci1',$dat);
                }
        }
?>
复制代码

程序完成,enjoy!

程序代码2
视图层不变

控制层
test1.php

PHP复制代码
<?php
        class Test1 extends Controller
        {
                function __construct()
                {
                        parent::Controller();
                        $this->load->database();
                }
                function index()
                {
                        $this->load->view('test1_view');
                }
                function deal()
                {
                        $data['a']=$this->input->post('t1',true);
                        $data['b']=$this->input->post('t2',true);
                        $this->load->library('session');
                        $this->session->set_userdata($data);
                        $this->load->model('test1_model');
                        $this->test1_model->ins();
                        $this->test1_model->sel();
                }
        }
?>
复制代码

感想:获取post值,用session存储,增加了一个查询功能。。
模型层
test1_model

PHP复制代码
<?php
        class test1_model extends Model
        {
                function __construct()
                {
                        parent::Model();
                }
                function ins()
                {
                        $dat=array(
                                'name'=>$this->session->userdata('a'),                 
                                'content'=>$this->session->userdata('b')
                                                );
                $this->db->insert('ci1',$dat);
                }
                function sel()
                {
                        $query=$this->db->get('ci1');
                        foreach($query->result() as $row)
                        {
                                echo $row->name;
                                echo $row->content."<br>";
                        }
                }
        }
?>
复制代码


感想:最后想说的是,框架是死的,人是活的,我们要利用框架的优点,而不要被框架束缚。。。

111111.png

评分

参与人数 2威望 +8 收起 理由
lamtin + 3 精品文章
Hex + 5 我很赞同

查看全部评分

发表于 2010-8-10 09:55:46 | 显示全部楼层
學習了!
期待更多這樣的教程來!!!!
发表于 2010-8-10 11:22:24 | 显示全部楼层
哦...yeah....
发表于 2010-8-10 12:49:26 | 显示全部楼层
我感觉吧,模型层不应该直接和session打交道,应该在控制层传参,这样较独立一些。松耦合。我是这样想的。我是新手,不知道对不对?
发表于 2010-8-12 11:13:30 | 显示全部楼层
学习学习 我都是新手,不知道对不对
发表于 2010-8-14 17:09:36 | 显示全部楼层
新手 来学习下
发表于 2010-8-26 19:29:12 | 显示全部楼层
学习一下

本版积分规则