连贯操作, Post/Get传递参数

D:\LearnWebDevelop\php\thinkphp_3.2.3_full\Application\Home\Model\UserModel.class.php

<?php
namespace Home\Model;
use Think\Model;
class UserModel extends Model {

// 定义自动验证
protected $_validate = array(
    array('username','require','用户名必须'),
    array('score','require','分数必须'),    
);

// 定义自动完成
protected $_auto = array(
    array('create_time','time',1,'function'),
);

}

D:\LearnWebDevelop\php\thinkphp_3.2.3_full\Application\Home\Controller\UserController.class.php

<?php
namespace Home\Controller;
use Think\Controller;
use Think\Model;
class UserController extends Controller{
    
    //http://localhost/thinkphp323/index.php/home/User/index?id=12
    public function index($id=12){
        $User = M("User"); // 实例化User对象
        $u=$User->where('id=' . $id)->select(); // 
        //var_dump($u);exit();
        $this->assign('vo',$u[0]);
        $this->display();
    }
    
    public function update(){
        //var_dump($_POST);exit();
        $User = D('User');
        if($User->create()) {
            //var_dump($User);exit();
            $User->create_time = time();
            $result = $User->save();
            if($result) {
                $this->success('数据保存成功!');
            }else{
                $this->error('数据写入错误!');
            }
        }else{
            $this->error($User->getError());
        }
    }
    
    //http://localhost/thinkphp323/index.php/home/User/where_field_find
    //$User->where('id=1')->field('id,name,email')->find();
    public function where_field_find($where=null,$field=null){    
        if(($where==null) || ($field==null)) {
            $this->display();
        }else{
            $User = M("User"); // 实例化User对象
            $ret=$User->where($where)->field($field)->find();
            var_dump($ret);
        }
    }
    
    //http://localhost/thinkphp323/index.php/home/User/where_delete
    //$User->where('status=1 and id=1')->delete();
    public function where_delete($where=null){   
        if($where==null) {
            $this->display();
        }else{
            $User = M("User"); // 实例化User对象
            $ret=$User->where($where)->delete();
            var_dump($ret);
        }
    }

    
           //下面是一个标准的URL访问格式:
           //http://serverName/index.php/模块/控制器/操作(/参数名/参数值)
//http://localhost/thinkphp323/index.php/home/Form/add
//http://localhost/thinkphp323/index.php/home/index/hello/name/baby



    //http://localhost/thinkphp323/index.php/home/User/inc_score?id=1&value=1
    public function inc_score($id=1,$value=1){
        $User = M("User"); // 实例化User对象
        $User->where('id=' . $id)->setInc('score',(int)$value); // 用户的积分加
    }

    //http://localhost/thinkphp323/index.php/home/User/dec_score?id=1&value=1
    public function dec_score($id=1,$value=1){
        $User = M("User"); // 实例化User对象
        $User->where('id=' . $id)->setDec('score',(int)$value); // 用户的积分加
    }

//$User = M("User"); // 实例化User对象
//$User->where('id=5')->setInc('score',3); // 用户的积分加3
//$User->where('id=5')->setInc('score'); // 用户的积分加1
//$User->where('id=5')->setDec('score',5); // 用户的积分减5
//$User->where('id=5')->setDec('score'); // 用户的积分减1

    //http://localhost/thinkphp323/index.php/home/User/select_score?score=100
    public function select_score($score=1){
        $User = M("User"); // 实例化User对象
        $u=$User->where('score=' . $score)->select();
        var_dump($u);
        
        $map['score'] = array('eq',$score);        
        $arr=$User->where($map)->select(); 
        var_dump($arr);

    }


    //http://localhost/thinkphp323/index.php/home/User/select_user_score?score=100&username=jim
    public function select_user_score($username='jim',$score=100){
        /*
        $User = M("User"); // 实例化User对象
        $condition['username'] = $username;
        $condition['score'] = $score;
        $condition['_logic'] = 'OR';
        */

        $User = M("User"); // 实例化User对象
        // 定义查询条件
        $condition = new \stdClass();
        $condition->username = $username;
        $condition->score = $score;
        $condition->_logic = 'OR';
        $User->where($condition)->select();

        // 把查询条件传入查询方法
        $u=$User->where($condition)->select();
        var_dump($u);
    }

    //http://localhost/thinkphp323/index.php/home/User/user_keyword?keyword=tom
    //http://localhost/thinkphp323/index.php/home/User/user_keyword?keyword=100
    public function user_keyword($keyword='jim'){
        $User = M("User"); // 实例化User对象
        //$map['username|score'] = $keyword;
        $map['username|score'] =array($keyword,100,'_multi'=>true);
        // 把查询条件传入查询方法
        $u=$User->where($map)->select();
        var_dump($u);
    }
    
    //http://localhost/thinkphp323/index.php/home/User/max_score    
    public function max_score(){
        $User = M("User"); // 实例化User对象
        // 获取用户数:
        $userCount = $User->count();
        echo "userCount : $userCount <br>";
        // 或者根据字段统计:
        $userCount = $User->count("id");
        echo "userCount id : $userCount <br>";
        // 获取用户的最大积分:
        $maxScore = $User->max('score');
        echo "maxScore : $maxScore <br>";
        // 获取积分大于0的用户的最小积分:
        $minScore = $User->where('score>0')->min('score');
        echo "minScore : $minScore <br>";
        // 获取用户的平均积分:
        $avgScore = $User->avg('score');
        echo "avgScore : $avgScore <br>";
        // 统计用户的总成绩:
        $sumScore = $User->sum('score');
        echo "sumScore : $sumScore <br>";
    }

    //http://localhost/thinkphp323/index.php/home/User/query_score/score/20
    public function query_score($score=60){    
        $Model = new \Think\Model(); // 实例化一个model对象 没有对应任何数据表
        $ret = $Model->query("select * from think_user where score >= " . $score);
        var_dump($ret);
    }
    
    //http://localhost/thinkphp323/index.php/home/User/insert_user?username=lily&score=60
    public function insert_user($username,$score=60){
        $Model = new Model(); // 实例化一个model对象 没有对应任何数据表
        $ret=$Model->execute("insert into think_user(username,score) values('{$username}',{$score})");
        var_dump($ret);
    }
    
    //http://localhost/thinkphp323/index.php/home/User/delete_user?username=lily
    public function delete_user($username){
        $Model = new Model(); // 实例化一个model对象 没有对应任何数据表
        $ret=$Model->execute("delete from think_user where username='{$username}'");
        var_dump($ret);
    }

    //http://localhost/thinkphp323/index.php/home/User/update_user?username=lily&score=60
    public function update_user($username,$score=60){
        $Model = new Model(); // 实例化一个model对象 没有对应任何数据表
        $ret=$Model->execute("update think_user set score={$score} where username='{$username}'");
        var_dump($ret);
    }
    
    
    //http://localhost/thinkphp323/index.php/home/User/where_order_limit?where=&order=&limit=
    //http://localhost/thinkphp323/index.php/home/User/where_order_limit?where=username like '%m'&order=username&limit=3
    //http://localhost/thinkphp323/index.php/home/User/where_order_limit?where=username like '%l%'&order=username&limit=3
    public function where_order_limit($where,$order,$limit){
        $User = M("User"); // 实例化User对象
        echo "User->where($where)->order($order)->limit($limit)->select()",'<hr>';
        $ret=$User->where($where)->order($order)->limit((int)$limit)->select();
        var_dump($ret);
           echo "User->order($order)->limit($limit)->where($where)->select()",'<hr>';
        $ret=$User->order($order)->limit((int)$limit)->where($where)->select();
        var_dump($ret); 
    }
    
    
    
    
}

D:\LearnWebDevelop\php\thinkphp_3.2.3_full\Application\Home\View\User\where_field_find.html

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <title> New Document </title>
  <meta charset="UTF-8">
  <meta name="Author" content="">
  <meta name="Keywords" content="">
  <meta name="Description" content="">
 </head>

 <body>

<FORM method="get" action="">
where:<INPUT type="text" name="where" ><br>
field:<INPUT type="text" name="field" ><br>
<INPUT type="submit" value="get提交">
</FORM>

<FORM method="post" action="">
where:<INPUT type="text" name="where" ><br>
field:<INPUT type="text" name="field" ><br>
<INPUT type="submit" value="post提交">
</FORM>

 </body>
</html>

D:\LearnWebDevelop\php\thinkphp_3.2.3_full\Application\Home\View\User\where_delete.html

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <title> New Document </title>
  <meta charset="UTF-8">
  <meta name="Author" content="">
  <meta name="Keywords" content="">
  <meta name="Description" content="">
 </head>

 <body>

<FORM method="get" action="">
where:<INPUT type="text" name="where" ><br>
<INPUT type="submit" value="get提交">
</FORM>

<FORM method="post" action="">
where:<INPUT type="text" name="where" ><br>
<INPUT type="submit" value="post提交">
</FORM>

 </body>
</html>

D:\LearnWebDevelop\php\thinkphp_3.2.3_full\Application\Home\View\User\index.html

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <title> New Document </title>
  <meta charset="UTF-8">
  <meta name="Author" content="">
  <meta name="Keywords" content="">
  <meta name="Description" content="">
 </head>

 <body>

<FORM method="post" action="__URL__/update">
username:<INPUT type="text" name="username" value="{$vo.username}">
<br/>
score:<TEXTAREA name="score" rows="5" cols="45">{$vo.score}</TEXTAREA>
<br/>
<INPUT type="hidden" name="id" value="{$vo.id}">
<!-- <INPUT type="hidden" name="create_time" value=""> -->
<INPUT type="submit" value="提交">

</FORM>


<FORM method="get" action="__URL__/index">
id:
<INPUT type="text" name="id" >
<INPUT type="submit" value="get提交">
</FORM>

<FORM method="post" action="__URL__/index">
id:
<INPUT type="text" name="id" >
<INPUT type="submit" value="post提交">
</FORM>

 </body>
</html>

 

posted @ 2017-12-01 10:23  sky20080101  阅读(72)  评论(0)    收藏  举报