PHP代码篇(三)--常用方法
模块下不间断更新,PHP常用方法,欢迎留言!
一、递归删除指定目录下所有文件及文件夹
/** * 递归删除指定目录下所有文件及文件夹 * @param unknown $path,删除路径 */ function deleteFileWay($path) { //如果是目录则继续 if(is_dir($path)){ //扫描一个文件夹内的所有文件夹和文件并返回数组 $info = scandir($path); foreach($info as $k=>$v){ //排除目录中的.和.. if($v != '.' && $v != '..'){ if(is_dir($path.$v)){ //子目录中操作删除文件夹和文件 deleteFileWay($path.$v.'/'); //目录清空后删除空文件夹 @rmdir($path.$v.'/'); }else{ //如果是文件直接删除 @unlink($path.$v); } } } } } /** * [result14 description] * 查找会员下面所有盟主 * 参数$uid会员id * 返回 * 'status'=>0,没有下级盟主 * 'status'=>1,'data'=>下级盟主id,数组类型 */ function result14($uid){ $cancel_user_list=$uid.',';//下级所有盟主列表列表,用于sqlin语句 $crt_level_user=pdo_getall('nets_hjk_members', array('from_uid' => $uid), array('type','memberid','level')); if (empty($crt_level_user)) { return array('status'=>0,'hint'=>"没有需要变更的下级1"); } while (!empty($crt_level_user)) { //从当前用户中提取memberid,用于下一次取下级用户 $crt_level_user_for_sqlin=''; $crt_level_leader_for_sqlin=''; foreach ($crt_level_user as $value) { $crt_level_user_for_sqlin .= $value['memberid'].','; //遍历crt_level_user中所有的行,取出盟主的memberid放在$crt_level_leader_for_sqlin中 if ($value['type'] == 1) { $crt_level_leader_for_sqlin .= $value['memberid'].','; } } $cancel_user_list .= $crt_level_leader_for_sqlin; //获取当前层级下级用户 if(!empty($crt_level_user_for_sqlin)){ $crt_level_user_for_sqlin=substr($crt_level_user_for_sqlin,0,strlen($crt_level_user_for_sqlin)-1); } $crt_level_user=pdo_fetchall("SELECT ex.memberid,ex.type FROM ".tablename('nets_hjk_members')." AS ex where from_uid in(". $crt_level_user_for_sqlin.") "); } $junior_user_list = ''; $cancel_user_list = substr($cancel_user_list,0,strlen($cancel_user_list)-1); $cancel_user_list_arr = explode(",",$cancel_user_list); //当下会员下级所有的盟主,数组 $cancel_user_list_arr = array_splice($cancel_user_list_arr,1); if (empty($cancel_user_list_arr)) { return array('status'=>0,'hint'=>"没有需要变更的下级2"); }else{ return array('status'=>1,'data'=>$cancel_user_list_arr); } }
二、数据库常用操作
//插入一条数据 INSERT INTO ims_stat_rule (rid,hit) VALUES (351,990); //删除一条数据 DELETE FROM ims_nets_hjk_applyfor WHERE member_id = 49483; //修改一条数据 update ims_nets_hjk_members set level = 4 where id = 100248; //查询数据 select * from ims_nets_hjk_members where memberid = 18978; //格式化时间戳 FROM_UNIXTIME(M1.created_at,'%Y-%m-%d %H:%i:%s') AS created_at; //SQL中IF的用法,如果表M1时间戳为0则默认为'/',反之则进行格式化时间戳处理 IF(M1.finish_time = 0,'/',FROM_UNIXTIME(M1.finish_time,'%Y-%m-%d %H:%i:%s')) AS finish_time
三、ThinkPHP5.0常用操作
//查询区间内 $device_string = $db->table('ims_nets_hjk_devices') ->where('device_id',['>=',$k],['<=',$j],'AND') ->where('status',0) ->field('device_id') ->limit(50) ->select(); // 如下为CASE的用法----------------------------- $level = Db::query("SELECT SUM(1) AS '全部用户',SUM(CASE WHEN type=1 AND level= 0 THEN 1 ELSE 0 END) AS '店主',SUM(CASE WHEN type=1 AND level= 1 THEN 1 ELSE 0 END) AS '高级店主',SUM(CASE WHEN type=1 AND level= 2 THEN 1 ELSE 0 END) AS '特约店主',SUM(CASE WHEN type=0 THEN 1 ELSE 0 END) AS '普通用户',SUM(CASE WHEN is_partner=1 THEN 1 ELSE 0 END) AS '合伙人',SUM(CASE WHEN is_insider=1 THEN 1 ELSE 0 END) AS '内部',SUM(CASE WHEN is_city_agent=1 THEN 1 ELSE 0 END) AS '市级代理' FROM ims_nets_hjk_members"); SELECT SUM(CASE WHEN pay_status = 1 AND date_format(from_UNIXTIME(`pay_time`),'%Y-%m-%d') = date_format(now(),'%Y-%m-%d') THEN 1 ELSE 0 END ) AS S1, SUM(CASE WHEN pay_status = 1 AND date_format(from_UNIXTIME(`pay_time`),'%Y-%m-%d') = date_format(now(),'%Y-%m-%d') THEN point ELSE 0 END ) AS S2, SUM(CASE WHEN pay_status = 1 AND order_status = 0 THEN 1 ELSE 0 END ) AS S3 FROM ims_integral_order; // 优化一个四表联查,以会员表作为临时表-------------------------- $list = Db::query(" SELECT M1s.*, M2.avatar AS avatar_is, M2.avatar AS avatar_is,M2.nickname AS nickname_is,M2.pid AS pid_is, M3.integral,M3.asset,M3.wallet, FROM_UNIXTIME(M4.addTime,'%Y-%m-%d %H:%i:%s') AS addTime FROM ( SELECT memberid,pid,avatar,nickname,FROM_UNIXTIME(created_at,'%Y-%m-%d %H:%i:%s') AS created_at,level,type,is_city_agent,is_partner,is_insider,realname,mobile,from_uid FROM ims_nets_hjk_members AS M1 WHERE ".$whes." ORDER BY created_at DESC LIMIT ".(($pindex - 1) * $psize).",". $psize." ) AS M1s LEFT JOIN ims_nets_hjk_members AS M2 ON M1s.from_uid = M2.memberid LEFT JOIN ims_mc_members AS M3 ON M1s.memberid = M3.uid LEFT JOIN t_fans_log AS M4 ON M1s.memberid = M4.fans_memberId "); //TP自加和自减 $res = $this->where('single','gt',0) ->update([ 'red_fee'=>['INC',$fee],//自加 'create_time'=>time(), 'count'=>['DEC',1],//自减 ]); //TP大于或者小于 $res = $this->where([ ['status','=',1], ['page_id','>',0] ]); //DB操作批量插入 $res = $db->table('ims_nets_hjk_sogo_licence')->insertAll($sogo_licence); //查询指定会员已激活的设备,并且不在ims_nets_hjk_sogo_equipment表里面,EXISTS用法 $list = $db->query("SELECT s_id FROM `ims_nets_hjk_devices` WHERE active_member_id = ".$uid." AND `status` = 1 AND NOT EXISTS (SELECT s_id FROM ims_nets_hjk_sogo_equipment WHERE s_id = ims_nets_hjk_devices.s_id ) //打印SQL语句 ->buildSql();
四、ThinkPHP5.0常用操作
//模拟url传递数组 $url = 'http://niushop.jingjinglego.com/index.php?s=/admin/We7Config/addIndexAd&type=2&adList[0][sort]=456&adList[0][img]=upad/test/1e.jpg&adList[0][outerUrl]=www.bai.com&adList[0][source]=1&adList[1][sort]=456&adList[1][img]=upad/test/1e.jpg&adList[1][outerUrl]=www.bai.com&adList[1][source]=1'; //调用请求类request写法,接受数组 $ids = request->post('ids/a'); //获取参数 use think\Request; $request = Request::instance(); $method = $request->method();//获取上传方式 $request->param();//获取所有参数,最全 $get = $request->get();//获取get上传的内容 $post = $request->post();//获取post上传的内容 $request->file('file')//获取文件 //错误和正确提示及跳转error/success $this->error("抱歉你不是最帅的人", __URL__ . "/wap/We7Store/storeManagement"); // 锁定跳转,url重定向 $redirect = __URL(__URL__ . "/wap/login/userlock"); $this->redirect($redirect);
四、检查字符串是否是URL
//判断是否是url function is_url($v){ $pattern="#(http|https)://(.*\.)?.*\..*#i"; if(preg_match($pattern,$v)){ return true; }else{ return false; } }
-----END
影子是一个会撒谎的精灵,它在虚空中流浪和等待被发现之间;在存在与不存在之间....