• Welcome to Journal web site.

我是 PHP 程序员

- 开发无止境 -

没有了
没有了

第1章 认识Thinkphp、数据库操作~第2章 数据集处理与thinkphp请求响应

Data: 2020-07-21 03:55:54Form: JournalClick: 5

    // http://www.tp.com/index.php/Index/index
    // 找到一个方法, 域名 + 入口文件 + 文件名(类名) + 方法名
    public function index()
    {
        // -------------------2022年5月9日----------------------------
        // 1、访问tp页面
        $arr = [
            'ouyangke' => '欧阳克'
        ];
        print_r($arr);
        echo '欧阳克';

        // 2、原生mysql
        $ret = Db::query("SELECT * FROM `oyk_shop_lists`");
        print_r($ret);
        if(!empty($ret)){
            foreach($ret as $v){
                echo $v['title'];
                echo '<br>';
            }
        }
       
        // 3、原生mysql,添加和修改
        $ret = Db::execute("UPDATE `oyk_shop_lists` SET `status` = 0 WHERE `id` = 15;");
        print_r($ret);

        // 4、select 查询全部,tp查询方式
        $ret = Db::table('oyk_shop_lists')->select();
        if(!empty($ret)){
            foreach($ret as $v){
                echo $v['title'];
                echo '<br>';
            }
        }
        print_r($ret);

        // 5、find 查询单条数据,要增加条件
        $ret = Db::table('oyk_shop_lists')->find(1);
        print_r($ret);

        // 6、value 查询单个数据
        $ret = Db::table('oyk_shop_lists')->value("title");
        print_r($ret);

        // 7、where条件
        $ret = Db::table('oyk_shop_lists')->where('id',10)->value("title");
        print_r($ret);

        // 8、column 查询一列
        $ret = Db::table('oyk_shop_lists')->column("title");
        print_r($ret);
        $ret = Db::table('oyk_shop_lists')->column("title","id");
        print_r($ret);

        // 9、insert 插入
        $arr = [
            'pid' => 1,
            'cid' => 2,
            'title' => '欧阳克直播课',
            'img' => '',
            'price' => 588,
            'num' => 0,
            'info' => '',
            'status' => 1,
            'add_time' => 0,
        ];
        $ret = Db::table('oyk_shop_lists')->insert($arr);
        print_r($ret);

        // 10、insertGetId
        $arr = [
            'pid' => 1,
            'cid' => 2,
            'title' => '欧阳克直播课',
            'img' => '',
            'price' => 588,
            'num' => 0,
            'info' => '',
            'status' => 1,
            'add_time' => 0,
        ];
        $ret = Db::table('oyk_shop_lists')->insertGetId($arr);
        print_r($ret);

        // 11、insertAll
        $arr = [
            [
                'pid' => 1,
                'cid' => 2,
                'title' => '欧阳克直播课',
                'img' => '',
                'price' => 588,
                'num' => 0,
                'info' => '',
                'status' => 1,
                'add_time' => 0,
            ],
            [
                'pid' => 1,
                'cid' => 2,
                'title' => '欧阳克直播课',
                'img' => '',
                'price' => 588,
                'num' => 0,
                'info' => '',
                'status' => 1,
                'add_time' => 0,
            ],
            [
                'pid' => 1,
                'cid' => 2,
                'title' => '欧阳克直播课',
                'img' => '',
                'price' => 588,
                'num' => 0,
                'info' => '',
                'status' => 1,
                'add_time' => 0,
            ]
        ];
        $ret = Db::table('oyk_shop_lists')->insertAll($arr);
        print_r($ret);

        // 12、update 修改
        $ret = Db::table('oyk_shop_lists')->where('id',20)->update([
            'title' => '朱老师直播课'
        ]);
        print_r($ret);

        // 13、inc 自增
        $ret = Db::table('oyk_shop_lists')->where('id',20)->inc('num')->update();
        $ret = Db::table('oyk_shop_lists')->where('id',20)->inc('num',10)->update();
        print_r($ret);

        // 14、dec 自减
        $ret = Db::table('oyk_shop_lists')->where('id',20)->dec('num')->update();
        print_r($ret);

        // 15、detele 删除
        $ret = Db::table('oyk_shop_lists')->delete(19);
        print_r($ret);

       
        // 16、useSoftDelete 软删除
        $ret = Db::table('oyk_shop_lists')->useSoftDelete('status',2)->delete(20);
        print_r($ret);

        // 17、WHERE 查询方法
        $ret = Db::table('oyk_shop_lists')->where('id',20)->find();
        $ret = Db::table('oyk_shop_lists')->where('id','=',20)->find();
        $ret = Db::table('oyk_shop_lists')->where('id','<',5)->select();
        $ret = Db::table('oyk_shop_lists')->where('id','>',15)->select();
        $ret = Db::table('oyk_shop_lists')->where('id','>=',15)->select();
        $ret = Db::table('oyk_shop_lists')->where('id','<=',5)->select();
        $ret = Db::table('oyk_shop_lists')->where('title','like','%直播课%')->select();
        $ret = Db::table('oyk_shop_lists')->whereLike('title','%直播课%')->select();
        $ret = Db::table('oyk_shop_lists')->where('title','not like','%直播课%')->select();
        $ret = Db::table('oyk_shop_lists')->where('title','not like','%直播课%')->select();
       
        // 18、name 表名,去掉表前缀
        $ret = Db::name('shop_lists')->select();

        // 19、field 字段返回值
        $ret = Db::name('shop_lists')->field('title')->select();
        $ret = Db::name('shop_lists')->field(['title','img'])->select();
        print_r($ret);

        // 20、order 排序
        $ret = Db::name('shop_lists')->order('id DESC')->select();
        print_r($ret);

        // 21、limit 查询条数
        $ret = Db::name('shop_lists')->order('id DESC')->limit(2,2)->select();
        print_r($ret);

        // 22、page 翻页,更方便
        $ret = Db::name('shop_lists')->order('id DESC')->page(3,3)->select();
        print_r($ret);

        // 24、getLastSql 先执行mysql语句,在返回原生的mysql语句
        $ret = Db::name('shop_lists')->field(['title','img'])->where('id','>',5)->order('id DESC')->page(1,3)->select();
        print_r($ret);
        echo Db::getLastSql();

        // 25、fetchSql 返回原生的mysql语句
        $ret = Db::name('shop_lists')->field(['title','img'])->where('id','>',5)->order('id DESC')->page(1,3)->fetchSql()->select();
        echo $ret;
       
        // 文档:www.ouyangke.com
        // 19期项目展示:http://19.ouyangke.com/#/

        // 后台管理系统2:
        // 演示站:http://admin-thinkphp-antd-vue-learning.ouyangke.com
        // 码云:https://gitee.com/ouyangke_com/admin-thinkphp-antd-vue-learning

        // thinkphp官网:https://www.thinkphp.cn/

        // ----------------------2022年5月10日
        // 1、count 统计数据表中的数量
        $count = Db::table('oyk_shop_lists')->count();
        // 向上取整
        echo ceil($count / 10);
        echo $count;
        // 2、sum 统计数量
        $sum = Db::table('oyk_shop_lists')->sum('num');
        echo $sum;

        // 3、where 条件 AND OR
        // 还可以使用数组的方式,进行查询

        // 条件比较多,用需要用到判断,判断这个条件是否存在
       
        $where[] = ['status','=',1];

        $price  = 1000;
        if(!empty($price)){
            $where[] = ['price','>=',$price];
        }

        $num = 10;
        if(!empty($num)){
            $where[] = ['num','>=',$num];
        }

        $lists = Db::table('oyk_shop_lists')->where($where)
                                            ->order('price DESC')
                                            ->select();
        echo Db::getLastSql();
        SELECT * FROM `oyk_shop_lists` WHERE `status` = 1 AND `price` >= 1000 AND `num` >= 10 ORDER BY `price` DESC
        print_r($lists);

        $lists = Db::table('oyk_shop_lists')
        if(!empty($price)){
            ->where('price','>=',1000);
        }
                                            ->whereOr('num','>=',1)
                                            ->order('price DESC')
                                            ->select();
        print_r($lists);


        // 4、事务:数据库 进行回滚
        $arr = [
            'pid' => 1,
            'cid' => 2,
            'title' => '欧阳克直播课',
            'img' => '',
            'price' => 588,
            'num' => 0,
            'info' => '',
            'status' => 1,
            'add_time' => 0,
        ];
        // 数据库事务开始;
        Db::startTrans();
        $ret = Db::table('oyk_shop_lists')->insert($arr);

        if(empty($ret)){
            // 事务回滚
            Db::rollback();
        }

        $cat = Db::table('oyk_shop_cat')->insert([
            'pid' => 21,
            'name' => 'ouyang',
            'pic' => '',
            'sort' => 0,
            'status' => 1
        ]);

        if(empty($cat)){
            // 事务回滚
            Db::rollback();
        }
        // 事务提交
        Db::commit();
       
        // 5、对数据集进行处理
        $ret = Db::table('oyk_shop_cat')->where('status',1)->select();
        // 是数据库里的功能
        if($ret->isEmpty()){
            echo '未找到数据';
        }
        print_r($ret->toArray());

        // 6、请求数据,get
        print_r($_GET);
        print_r(Request::get());
        print_r(Request::get('id'));
        // param 获取get和post的参数
        print_r( Request::param() );
        print_r( Request::post() );

       
        // 模拟商城查询的方式
        if(Request::method() == 'POST'){
            $where[] = ['status','=',1];

            $price_min  = Request::post('price_min',0);
            $price_min  = Request::post('price_min/d',0);
            echo $price_min;
            if(!empty($price_min)){
                $where[] = ['price','>=',$price_min];
            }
            $price_max  = Request::post('price_max',0);
            if(!empty($price_max)){
                $where[] = ['price','<=',$price_max];
            }

            $num = Request::post('num',0);
            if(!empty($num)){
                $where[] = ['num','>=',$num];
            }

            $lists = Db::table('oyk_shop_lists')->where($where)
                                                ->order('price DESC')
                                                ->select();

                                                print_r($lists);
        }

        // 请求软件: Postman、ApiPost

        echo Request::host();
        echo Request::scheme();
        echo Request::port();
        echo Request::time();
        echo date('Y-m-d H:i:s',Request::time());
        echo Request::controller();
        echo '<br>';
        echo Request::action();


        // 7、调用model
        $db = new ShopLists();

        $lists = $db->get_data();
        $lists = $db->set_data();
        $lists = $db->search_data();

        if(!empty($lists)){
            foreach($lists as &$v){
                if($v['status'] == 1){
                    $v['status_name'] = '开启';
                }else{
                    $v['status_name'] = '关闭';
                }
            }
        }
        print_r($lists);
    }


Name:
<提交>