初入thinkphp

    花3天时间入门了php和thinkphp框架,紧接着就做了一个小后台,简单使用了thinkphp框架封装的一些类和函数。

现在来总结一下:

           

 1    //登陆函数
 2 public function Login()
 3     {
 4          //判断函数是否首次被调用
 5         if(!IS_POST)
 6         $this->display();
 7         else
 8         {
 9            //使用I函数来活得模板传来的数据
10            $map['user_name']=I('post.username');
11            $map['user_psd']=I('post.userpsd');
12            //empty函数判断变量是否为空
13            if(empty($map['user_name'])||empty($map['user_psd']))
14                $this->error('用户名或密码没写');
15            //与数据库中的数据对比
16           $UserInfo = M('zx_admin')->where($map)->select();
17           if(empty($UserInfo[0]['user_name']))
18                $this->error('用户名或密码错误');
19           else
20           {
21              //设置session值以便之后调用
22             session('user',$UserInfo[0]);
23               $this->success('登陆成功',U('Index/index'));
24           }
25                
26         }
27     }
 1 //添加数据函数
 2 public function add(){
 3         if(!IS_POST)
 4             $this->display();
 5         else
 6         {
 7           //数组形式获取提交的数据,用I函数也可实现比如I('post.').
 8             $article = $_POST;
 9          //获取当前时间 date()函数是php原生函数
10             $article['time'] = date('Y-m-d',time());
11             if(empty($article['title'])||empty($article['article']))
12             {
13                 $this->error('数据不完整');
14             }
15             $model = M('zx_article');
16          //对上传类进行配置
17             $config = array(
18                 'autoSub' => false,
19                 'hash' => false,
20                 'maxSize' => 3145728,
21                 'rootPath' =>'./Public/',
22                 'savePath' => './Uploads/',
23                 'saveName'   =>    array('uniqid',''),
24                 'exts'       =>    array('jpg', 'gif', 'png', 'jpeg'),
25                 );
26            //实例化一个上传函数27             $upload = new Upload($config);
28             $info = $upload->upload();
29             if(!$info)
30             {
31                 $this->error($upload->getError());
32             }
33             else {
34                 $article['photo'] = $info['photo']['savename'];//将上传后文件的名字传给数组
35             }
36             $result = $model->data($article)->add();
37             $this->success('成功提交数据','Index/show');
38         }
39     }
 1             //登陆界面表单
 2             //{:U()}无参调用U函数获取当前位置的URL
 3                     <form class="form-horizontal" action={:U()} method="post">
 4         <div class="form-group">
 5             <label class="col-md-2  control-label text-primary">账户</label>
 6             <div class="col-md-10">
 7                 <input type="text" id="username" name="username" class="form-control" placeholder="输入账户"/>
 8             </div>
 9         </div>
10         <div class="form-group">
11              <label class="col-md-2 control-label text-primary">密码</label>
12              <div class="col-md-10">
13                  <input type="password" id="userpsd" name="userpsd" class="form-control" placeholder="输入密码"/>
14              </div>
15         </div>
16         <div class="form-group">
17             <button class="btn btn-primary col-md-offset-4 w-50">登陆</button>
18         </div>
19     </form>
 1 <table class="table table-bordered table-striped table-hover">
 2             <thead>
 3                 <tr>
 4                     <th> 
 5                         序号
 6                     </th>
 7                     <th> 
 8                         标题
 9                     </th>
10                     <th> 
11                         添加时间
12                     </th>
13                     <th> 
14                         浏览量
15                     </th>
16                     <th> 
17                         显示状态
18                     </th>
19                     <th> 
20                         操作
21                     </th>
22                 </tr>
23             </thead>
24             <tbody>
25             //使用内置标签来循环输出数据
26                 <volist name="data" id="vo">
27                     <tr>
28                         <td>
29                             {$vo['article_id']}
30                         </td>
31                         <td>
32                             {$vo['title']}
33                         </td>
34                         <td>
35                             {$vo['time']}
36                         </td>
37                         <td>
38                             {$vo['view']}
39                         </td>
40                         <td>
41                             
42                         </td>
43                         <td>
44                             <div class="btn-group">
45                                 <a class="btn btn-primary" href="{:U('Index/update?article_id='.$vo['article_id'])}">//传id以编辑>
46                                     编辑
47                                 </a>
48                                 <a class="btn btn-danger" href="{:U('Index/article_delete?article_id='.$vo['article_id'])}">
49                                     删除
50                                 </a>
51                             </div>
52                         </td>
53                     </tr>
54                 </volist>
55             </tbody>
56         </table>
1 //显示当前管理员
2 <div class="inline-block user">
3                     当前用户:
4                     {$_SESSION['user']['user_name']}<br/>//使用之前设置的session,不需要函数传参进来
5                     <a href="#">[设置]</a>
6                     &nbsp
7                     <a href="__MODULE__/Login/Logout">[退出]</a>
8                 </div>

 thinkphp使用的MVC设计模式,不过我这里只是用了控制器和模板而没用模型,因为是刚刚开始学习感觉没用模型也可实现小程序。

原文地址:https://www.cnblogs.com/disneyland/p/4261032.html