ThinkPHPKJ_S02

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
<script> hidden <form  action = "\_\_URL\_\_create"  method ='post'  ><!-- create 不再生成个脚本文件了,直接添加方法创建C 中 直接用 -->      利用  “\_\_URL\_\_  ”动态的寻找 域名(项目)到模块的部分! $_POST\[\]   UserAction.class.php

<?php
// 本类由系统自动生成,仅供测试用途
class UserAction extends Action {
    public function index(){
            $m = M('User');
            $arr = $m -> select();
            //var_dump($arr);
            $this -> assign( 'data',$arr );
            
            $this -> display();
            
    
    }
    
    public function del(){
             $m = M('User');
             $id = $_GET\['id'\];
             $count =  $m ->delete($id);
             if($count > 0){
                     $this -> success(' 数据删除成功! ');
                 }    
             else
             {
                     $this ->error(' 数据删除失败! ');
                 }
        
    }
    
    
    
    
    /**负责显示修改页面!
    
    */
    public function modify()
    {
            $id = $_GET\['id'\];
             $m = M('User');
             $arr = $m -> find($id);
             $this -> assign('data',$arr);
                $this -> display();
        
        }
    
    public function update()
    {
            $m = M('User');
            $data\['id'\] = $_POST\['id'\];
            $data\['username'\] = $_POST\['username'\];
            $data\['sex'\] = $_POST\['sex'\];
            $count = $m->save($data);     //好像是  返回修改的多少!
        
            if($count > 0){
                $this -> success('数据修改成功!','index');
                }
                else{
                    
                $this -> error('数据修改失败!');
                    }
        
        
        }
    
    
    
    /**
            负责  产生新增用户的界面!!
    */
    public function add()
    {
        $this -> display();
        
        
        }
    public function create()
    {
        $m =M('User');
        $m -> username = $_POST\['username'\];
        $m -> sex = $_POST\['sex'\];
        $idNum = $m -> add();  // 会返回一个 id  number
        
        if( $idNum > 0)
        {
            $this -> success( '数据添加成功!' ,'index' );
            
            }else {
                 $this -> error('数据添加失败!');
            }
        
        
        }
    
    
    
    public function show()
    {
            //echo "欢迎你:"-$\_GET\['name'\]-'你的年龄是:'-$\_GET\['age'\];
            //$this-> display();
        
    }
    
    
}

index.html

<html>
        <head>
                <meta http-equiv-"content-type" content="text/html;charset-utf-8">
                <title>Index</title>
                <script>
                    function jump(){
                            window.location= "/thinkphp/index.php/User/add";
                        }
                    
                </script>
            </head>
        <body>
                <table border = '1' width = "500" align = 'center' >
                            <tr>
                                    <th >id</th>
                                    <th >username</th>
                                    <th >sex</th>
                                    <th >操作 </th>
                                                        
                            </tr>
                            <volist name = 'data' id = 'vo'>
                            <tr>
                                    <td >{$vo.id }</td>
                                    <td >{$vo.username}</td>
                                    <td >{$vo.sex}</td>
                                    <td ><a href= "/thinkphp/index.php/User/del/id/{$vo.id}" >删除</a> |
                                        <a href="/thinkphp/index.php/User/modify/id/{$vo.id}" >修改</a></td>
                                                        
                            </tr>        
                            </volist>            
                            
                </table>
                <center>
                <button onclick = "jump()" >添加用户</button><!-- Java script -->
                </center>
                <!--  <h1>Hello World!</h1> {$data} -->
            <!\-\- 我是在html注释内 -->
        </body>
            
    
</html>

modify.html

<html>
        <head>
                <meta http-equiv-"content-type" content="text/html;charset-utf-8">
                <title>Modify</title>
                <script>
                    window.onload = function(){
                            if({$data.sex }== 0 ){
                                    <!\-\- alert(0); -->
                                    document.getElementsByName('sex')\[1\].checked = 'checked';
                        }else{
                            
                                    document.getElementsByName('sex')\[0\].checked = 'checked';
                        }    
                            
                }
                </script>
            </head>
        <body>
                <form  action = "/thinkphp/index.php/User/update"  method ='post'  >
                            <input type = 'hidden' name = 'id'  value = "{$data.id}" />     <!-- 隐藏域 -->
                            姓名:<input type = "text"  name = 'username' value = "{$data.username}"/></br>
                            性别:男<input type = "radio"  name = 'sex'  value = '1' > 女<input type = "radio"
                                            name = 'sex'  value = '0'/></br>
                            <input type = 'submit' value = "提交修改"/>
                            
                </form>
        </body>
            
    
</html>

add.html

<html>
        <head>
                <meta http-equiv-"content-type" content="text/html;charset-utf-8">
                <title>Add</title>
        
            </head>
        <body>
                <form  action = "/thinkphp/index.php/User/create"  method ='post'  ><!-- create 不再生成个脚本文件了,直接添加方法创建C 中 直接用 -->
                            <!--   <input type = 'hidden' name = 'id'  value = "{$data.id}" />  隐藏域 -->
                            姓名:<input type = "text"  name = 'username' /></br>
                            性别:男<input type = "radio"  name = 'sex'  value = '1' > 女<input type = "radio"
                                            name = 'sex'  value = '0'/></br>
                            <input type = 'submit' value = "添加新用户"/>
                            <!\-\- 添加新用户后  form  调用 action  利用  method  进行方法操作!  -->
                </form>
        </body>
            
    
</html>

CURD   php+html   了解了   Control  层的 1.对  M层的  数据调用 2.对 V层的前端  html  的书写 3.  modify()// 负责页面的书写   调用回 C 使用update方法----<不再生成个脚本文件了 ! --   update()// form  标签是个关键!     负责更新   M 4. add()  //页面 --create()// 添加入  M层 数据!  同  3.!!! 5.注意返回的数值!!来检测  成功与否。 6.最后的跳转一定注意!
(っ•̀ω•́)っ✎⁾⁾ 坚持技术学习、内容输出与分享,您的支持将鼓励我继续创作!(*/ω\*)
( • ̀ω•́ )✧如有疑问或需要技术讨论,请留言或发邮件到 aclearzhang@qq.com.(*・ω< ) 
  • 本文作者:: AClearZhang
  • 本文链接:: 79.html
  • 版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 3.0 许可协议。转载请注明出处!