publicfunctioncountMonthUsers(){$month=date('Y-m',time());$prefix=config('database.prefix');$semRes=Db::query("selectFROM_UNIXTIME(create_time,'%Y-%m-%d')days,count(id)countfrom".$prefix."user_submitWHEREFROM_UNIXTIME(create_time,'%Y-%m')='".$month."'ANDdelete_time=0groupbydays");$seoRes=Db::query("selectFROM_UNIXTIME(create_time,'%Y-%m-%d')days,count(id)countfrom".$prefix."user_submitWHEREFROM_UNIXTIME(create_time,'%Y-%m')='".$month."'ANDdelete_time=0groupbydays");//x轴数据,作为x轴标注$j=date("t");//获取当前月份天数$start_time=strtotime(date('Y-m-01'));//获取本月第一天时间戳$xData=array();for($i=0;$i<$j;$i++){$xData[]=date('Y-m-d',$start_time+$i*86400);//每隔一天赋值给数组}//处理获取到的数据$ySemData=array();if(!empty($semRes)){foreach($xDataas$k=>$v){foreach($semResas$kk=>$vv){if($v==$vv['days']){$ySemData[$k]=$vv['count'];break;}else{$ySemData[$k]=0;continue;}}}}else{foreach($xDataas$k=>$v){$ySemData[$k]=0;}}$ySeoData=array();if(!empty($seoRes)){foreach($xDataas$k=>$v){foreach($seoResas$kk=>$vv){if($v==$vv['days']){$ySeoData[$k]=$vv['count'];break;}else{$ySeoData[$k]=0;continue;}}}}else{foreach($xDataas$k=>$v){$ySeoData[$k]=0;}}return[$xData,$ySemData,$ySeoData];}返回array(3){[0]=>array(28){[0]=>string(10)"2018-02-01"[1]=>string(10)"2018-02-02"[2]=>string(10)"2018-02-03"[3]=>string(10)"2018-02-04"[4]=>string(10)"2018-02-05"[5]=>string(10)"2018-02-06"[6]=>string(10)"2018-02-07"[7]=>string(10)"2018-02-08"[8]=>string(10)"2018-02-09"[9]=>string(10)"2018-02-10"[10]=>string(10)"2018-02-11"[11]=>string(10)"2018-02-12"[12]=>string(10)"2018-02-13"[13]=>string(10)"2018-02-14"[14]=>string(10)"2018-02-15"[15]=>string(10)"2018-02-16"[16]=>string(10)"2018-02-17"[17]=>string(10)"2018-02-18"[18]=>string(10)"2018-02-19"[19]=>string(10)"2018-02-20"[20]=>string(10)"2018-02-21"[21]=>string(10)"2018-02-22"[22]=>string(10)"2018-02-23"[23]=>string(10)"2018-02-24"[24]=>string(10)"2018-02-25"[25]=>string(10)"2018-02-26"[26]=>string(10)"2018-02-27"[27]=>string(10)"2018-02-28"}[1]=>array(28){[0]=>int(0)[1]=>int(0)[2]=>int(0)[3]=>int(0)[4]=>int(0)[5]=>int(0)[6]=>int(0)[7]=>int(0)[8]=>int(0)[9]=>int(0)[10]=>int(0)[11]=>int(0)[12]=>int(0)[13]=>int(0)[14]=>int(0)[15]=>int(0)[16]=>int(0)[17]=>int(0)[18]=>int(0)[19]=>int(0)[20]=>int(0)[21]=>int(0)[22]=>int(0)[23]=>int(30)[24]=>int(0)[25]=>int(6)[26]=>int(17)[27]=>int(0)}[2]=>array(28){[0]=>int(0)[1]=>int(0)[2]=>int(0)[3]=>int(0)[4]=>int(0)[5]=>int(0)[6]=>int(0)[7]=>int(0)[8]=>int(0)[9]=>int(0)[10]=>int(0)[11]=>int(0)[12]=>int(0)[13]=>int(0)[14]=>int(0)[15]=>int(0)[16]=>int(0)[17]=>int(0)[18]=>int(0)[19]=>int(0)[20]=>int(0)[21]=>int(0)[22]=>int(0)[23]=>int(0)[24]=>int(0)[25]=>int(0)[26]=>int(6)[27]=>int(0)}}
$data=Db::name('z_weight_log')->group("DATE_FORMAT(FROM_UNIXTIME(createTime),'%Y-%m-%d')")->select();
1获取时间戳当天时间戳://这里是当天时间戳$start_today=strtotime(date("Y-m-d",time()));$end_today=strtotime(date('Y-m-d',time()))+86400-1;//本月时间戳$start_month=strtotime(date('Y-m-01'));$end_month=strtotime(date('Y-m-d',strtotime('+1day')));//本季度时间戳$quarter=empty($param)?ceil((date('n'))/3):$param;//获取当前季度$start_quarter=mktime(0,0,0,$quarter*3-2,1,date('Y'));$end_quarter=mktime(0,0,0,$quarter*3+1,1,date('Y'));2查询条件$wheretoday=[['time','>=',$start_today],['time','<=',$end_today],];$wheremonth=[['time','>=',$start_month],['time','<=',$end_month],];$wherequarter=[['time','>=',$start_quarter],['time','<=',$end_quarter],];3查询数据$today=Db::name('account')->where($wheretoday)->select();//当天的数据$month=Db::name('account')->where($wheremonth)->select();//本月的数据$quarter=Db::name('account')->where($wherequarter)->select();//本季度的数据4返回结果//这里是json数据返回格式returnjson(['code'=>200,'msg'=>'数据查询成功','today'=>$today,"month"=>$month,'quarter'=>$quarter,]);最后总结可用于一般的时间查询。初入php,有很多不懂的地方见谅,希望多提意见一起探讨。
在thinkphp模板引擎里可以这样写:{$vo.title|msubstr=0,5,’utf-8′,false}扩展函数库的方法不能直接使用,需要加载或者拷贝到项目函数库中才能使用。加载扩展函数库,使用:Load('extend');加载在相应的Action中msubstr($str,$start=0,$length,$charset="utf-8″,$suffix=true)$str:要截取的字符串$start=0:开始位置,默认从0开始$length:截取长度$charset="utf-8″:字符编码,默认UTF-8$suffix=true:是否在截取后的字符后面显示省略号,默认true显示,false为不显示functionmsubstr($str,$start=0,$length,$charset="utf-8",$suffix=true){if(function_exists("mb_substr"))returnmb_substr($str,$start,$length,$charset);elseif(function_exists('iconv_substr')){returniconv_substr($str,$start,$length,$charset);}$re['utf-8']="/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xff][\x80-\xbf]{3}/";$re['gb2312']="/[\x01-\x7f]|[\xb0-\xf7][\xa0-\xfe]/";$re['gbk']="/[\x01-\x7f]|[\x81-\xfe][\x40-\xfe]/";$re['big5']="/[\x01-\x7f]|[\x81-\xfe]([\x40-\x7e]|\xa1-\xfe])/";preg_match_all($re[$charset],$str,$match);$slice=join("",array_slice($match[0],$start,$length));if($suffix)return$slice."…";return$slice;}
使用Db的写法 $a = Db::name('physical_test_log a') ->join('user b','a.tester_id=b.id') ->join('user c','a.testee_id=c.id') ->join('agency d','a.root_agency_id=d.id') ->join('agency_class e','a.agency_id=e.id')->field('a.id,a.test_date,a.age_year,b.username as tester_name,c.username as testee_name,d.name as agency_name,e.name as class_name')->select()->toArray();使用模型的写法 在model文件里先关联模型publicfunctiontester(){return$this->hasOne('User','id','tester_id')->bind(['tester_name'=>'username']);}publicfunctiontestee(){return$this->hasOne('User','id','testee_id')->bind(['testee_name'=>'username']);}publicfunctionagency(){return$this->hasOne('Agency','id','root_agency_id')->bind(['agency_name'=>'name']);}publicfunctionagencyclass(){return$this->hasOne('AgencyClass','id','agency_id')->bind(['class_name'=>'name']);}在控制器里$list=PhysicalTestLog::with(['tester','testee','agency','agencyclass'])->where($where)->order($sort,$order)->limit($offset,$limit)->select();$list->visible(['id','test_date','age_year','tester_name','testee_name','class_name','agency_name','agency_id']);$list=collection($list)->toArray();
usethink\Db;$tableName='your_table_name';//表名$databaseName='your_database_name';//数据库名$sql="SELECTcolumn_name,column_commentFROMinformation_schema.columnsWHEREtable_name='{$tableName}'ANDtable_schema='{$databaseName}'";$result=Db::query($sql);
thinkphp5获取mysql数据库表字段注释1、支持获取单个字段和所有字段。2、使用场景;例如状态:有两种状态:1|正常,2|禁用。通常用于获取器3、#*#为分隔符,json格式保存。//获取数据库字段注释//@paramstring$table_name数据表名称(必须,不含前缀)//@paramstring$field字段名称(默认获取全部字段,单个字段请输入字段名称)//@paramstring$table_schema数据库名称(可选)//@returnstringfunctionget_db_column_comment($table_name='',$field=true,$table_schema=''){ //接收参数 $database=config('database'); $table_schema=empty($table_schema)?$database['database']:$table_schema; $table_name=$database['prefix'].$table_name; //缓存名称 $fieldName=$field===true?'allField':$field; $cacheKeyName='db_'.$table_schema.'_'.$table_name.'_'.$fieldName; //处理参数 $param=[ $table_name, $table_schema ]; //字段 $columeName=''; if($field!==true){ $param[]=$field; $columeName="ANDCOLUMN_NAME=?"; } //查询结果 $result=Db::query("SELECTCOLUMN_NAMEasfield,column_commentascommentFROMINFORMATION_SCHEMA.COLUMNSWHEREtable_name=?ANDtable_schema=?$columeName",$param); //pp(Db::getlastsql()); if(empty($result)&&$field!==true){ return$table_name.'表'.$field.'字段不存在'; } //处理结果 foreach($resultas$k=>$v){ if(strpos($v['comment'],':')!==false){ $tmpArr=explode(':',$v['comment']); $data[$v['field']]=json_decode(end($tmpArr),true); } } //字段注释格式不正确 if(empty($data)){ return$table_name.'表'.$field.'字段注释格式不正确'; } returncount($data)==1?reset($data):$data;}
输出:学生模块——学生管理——添加学生栏目模板——管理栏目用户模块——用户管理——————用户管理——————添加用户——分组管理图标模块——图标模块系统设置——学校模块——————学校管理——————增加学校——后台首页——合作单位——————合作单位管理——————增加合作单位——上报教学站点——————站点管理——————添加站点模板代码:{volistname="name"id="vo"}<div><span>{$vo.title}</span></div>{volistname="vo.child"id="vo2"}<div><span>——{$vo2.title}</span>{volistname="vo2.child"id="vo3"}<div><span>——————{$vo3.title}</span></div>{/volist}</div>{/volist}{/volist}tp5控制器PHP代码:1、以静态数组的办法$data=[["id"=>22,"user"=>"houn","pid"=>0,"child"=>[["id"=>22,"user"=>"baichi","pid"=>0,"child"=>[["id"=>22,"user"=>"baichi03","pid"=>0,],["id"=>22,"user"=>"baichi03","pid"=>0,],]],["id"=>22,"user"=>"baichi","pid"=>0,],]],["id"=>22,"user"=>"houn","pid"=>0,"child"=>""],["id"=>22,"user"=>"houn","pid"=>0,"child"=>[["id"=>22,"user"=>"12baichi","pid"=>0,"child"=>[["id"=>22,"user"=>"21baichi03","pid"=>0,],["id"=>22,"user"=>"21baichi03","pid"=>0,],]],["id"=>22,"user"=>"baichi","pid"=>0,],]],];$this->assign('name',$data);2、读取数据库形式 TP5php代码//循环列出栏目数据1级$res=Db::name('menu')->where('flid',1)->order('uiddesc')->select();$data="";foreach($resas$k=>$v){//2级$res2=Db::name('menu')->where("pid",$v['id'])->select();$data2="";foreach($res2as$k2=>$v2){//3级$res3=Db::name('menu')->where("pid",$v2['id'])->select();$data3="";foreach($res3as$k3=>$v3){$data3[$k3]['id']=$v3['id'];$data3[$k3]['title']=$v3['title'];$data3[$k3]['child']=$v3['title'];}$data2[$k2]['id']=$v2['id'];$data2[$k2]['title']=$v2['title'];$data2[$k2]['child']=$data3;}$data[$k]['id']=$v['id'];$data[$k]['title']=$v['title'];$data[$k]['child']=$data2;}$this->assign('name',$data);进阶例子:$where=newWhere;if($state){$where['state']=$state-1;}if($search){$where['number']=['like',"%".$search."%"];}$where['deleted']=0;$where['user_id']=$Session['user_id'];$list=Db::name('order')->field('id,number,price,type,state,time,pay_time')->where($where)->order(['id'=>'desc'])->paginate(10,false,['query'=>request()->param(),'type'=>'page\Page','var_page'=>'page']);if(!empty($list)){foreach($listas$k=>$v){$oprs=Db::name('order_product')->field('id,title,num,price,pid,controller,img')->where('number',$v['number'])->select();if(!empty($oprs)){foreach($oprsas$k2=>$v2){if($v2['controller']=='Combination'){//组合$p_list=Db::name('order_product_clist')->field('id,title,controller,price,img,type_mold,num,pid')->where('opid',$v2['id'])->select();if(!empty($p_list)){$cbrs=[];foreach($p_listas$k3=>$v3){////$price31+=$v3['price'];$cbrs[$k3]['id']=$v3['pid'];$cbrs[$k3]['title']=$v3['title'];////$cbrs['price']=$v3['price'];$cbrs[$k3]['img']=$v3['img']?\app\admin\model\Common::file_url($v3['img']):$this->system_config['system_index_no_img'];if($v3['controller']=='Live'){$ttt="直播";}elseif($v3['controller']=='Video'){$ttt="点播";}elseif($v3['controller']=='Testpaper'){$ttt="试卷";}elseif($v3['controller']=='Shop'){$ttt="商城";}else{$ttt="其它";}$cbrs[$k3]['controller']=$v3['controller'];$cbrs[$k3]['type_title']=$ttt;}}else{$cbrs=[];}}if($v2['controller']=='Testpaper'){//试卷试卷ID要加密//$v2['id']=21;$v2['pid']=model('index/CommonModel')->lock_url($v2['id'],'id');}else{$v2['pid']=$v2['pid'];}if($v2['controller']=='Live'){$type_title1='直播';}elseif($v2['controller']=='Video'){$type_title1='点播';}elseif($v2['controller']=='Testpaper'){$type_title1='试卷';}elseif($v2['controller']=='Shop'){$type_title1='商品';}elseif($v2['controller']=='Combination'){$type_title1='组合';}else{$type_title1='商品';}$v2['type_title']=$type_title1;$v2['img']=$v2['img']?\app\admin\model\Common::file_url($v2['img']):$this->system_config['system_index_no_img'];$v2['child']=$cbrs;$oprs[$k2]=$v2;}}else{$oprs=[];}$v['child']=$oprs;$list[$k]=$v;}}
publicfunctionbuildList($parent_id=0,$level=1){$list=[];foreach($this->dataas$key=>$val){if($val['parent_id']==$parent_id){$v['id']=$val['id'];$v['parent_id']=$val['parent_id'];$v['name']=$val['name'];$v['module']=$val['module'];$v['filename']=$val['filename'];$v['icon']=$val['icon'];$v['is_show']=$val['is_show'];$v['sort']=$val['sort'];$v['level']=$level;$children=$this->buildList($v['id'],$level+1);if(empty($children)){$v['has_children']=false;}else{$v['has_children']=true;}$list[]=$v;$list=array_merge($list,$children);}}return$list;}css: .taxons-list{border-right:1pxsolid#f3f3f3;border-bottom:1pxsolid#f3f3f3;}.taxons-listli{height:45px;line-height:45px;border-left:1pxsolid#f3f3f3;border-top:1pxsolid#f3f3f3;}.taxons-listlia{font-size:14px;}.taxons-list.taxon-no{width:5%;}.taxons-list.taxon-name{width:50%;}.taxons-list.taxon-nameimg{vertical-align:middle;cursor:pointer;}.taxons-list.level_2.line-verticle{display:block;width:15px;height:40px;position:relative;float:left;}.taxons-list.level_2.line-verticle:before{content:'';position:absolute;top:0;left:0;width:1px;height:25px;background:#dddddd;}.taxons-list.level_2.line-verticle:after{content:'';position:absolute;top:25px;left:0;width:15px;height:1px;background:#dddddd;}.taxons-list.level_3.line-verticle{display:block;width:30px;height:40px;position:relative;float:left;}.taxons-list.level_3.line-verticle:before{content:'';position:absolute;top:0;left:0;width:1px;height:25px;background:#dddddd;}.taxons-list.level_3.line-verticle:after{content:'';position:absolute;top:25px;left:0;width:30px;height:1px;background:#dddddd;}.taxons-list.taxon-module{width:10%;}.taxons-list.taxon-show{width:10%;}.taxons-list.taxon-sort{width:10%;}.taxons-list.taxon-operate{width:10%;}.taxons-list.pull-left{border-right:1pxsolid#d9d9d9;padding-left:5px;}.taxons-list.taxon-header{background:#f3f3f3;}.taxons-list.taxon-hide{display:none;}html:<ulclass="taxons-list"><liclass="clearfixtaxon-header"><divclass="pull-lefttaxon-no">编号</div><divclass="pull-lefttaxon-name">名称</div><divclass="pull-lefttaxon-module">模块</div><divclass="pull-lefttaxon-show">是否显示</div><divclass="pull-lefttaxon-sort">排序</div><divclass="pull-righttaxon-operate">操作</div></li>{volistname="list"id="vo"}{ifcondition="$vo['level']eq1"}<liclass="taxon-bodyparent_{$vo.parent_id}level_{$vo.level}"data-id="{$vo.id}">{else/}<liclass="taxon-bodyparent_{$vo.parent_id}level_{$vo.level}taxon-hide"data-id="{$vo.id}">{/if}<divclass="pull-lefttaxon-no">{$vo.id}</div><divclass="pull-leftclearfixtaxon-name">{ifcondition="$vo['has_children']"}<imgsrc="/backend/has_children.gif"/>{else/}<imgsrc="/backend/no_children.gif"/>{/if}<spanclass="line-verticle"></span>{$vo.name}</div><divclass="pull-lefttaxon-module">{$vo.module}</div><divclass="pull-lefttaxon-show">{$vo.is_show}</div><divclass="pull-lefttaxon-sort">{$vo.sort}</div><divclass="pull-righttaxon-operate">{ifcondition="$vo['level']lt3"}<aclass="operate"href="/backend/taxons/create?parent_id={$vo.id}">添加子栏目</a>{/if}<aclass="operate"href="/backend/taxons/{$vo.id}/edit">编辑</a><aclass="operatedelete-operate"href="javascript:void(0)"data-url="/backend/taxons/{$vo.id}">删除</a></div></li>{/volist}</ul>js:$(".taxons-listli").each(function(){varid=$(this).attr('data-id');varnext_node=$('.parent_'+id);$(this).find('.taxon-name>img').click(function(){if(next_node.hasClass('taxon-hide')){$(this).attr('src','no_children.gif');next_node.removeClass('taxon-hide');}else{if(next_node.length!=0){$(this).attr('src','has_children.gif');}//如果还有第三级if(next_node.length>0){for(i=0;i<next_node.length;i++){vartext=next_node[i];//这里获取到的是html字符串,需要把它转为dom对象,否则获取不到data-idvarnext_id=$(text).attr('data-id');//转为dom对象$(text)varnext_next_node=$('.parent_'+next_id);if(next_next_node.length>0){next_node.find('.taxon-nameimg').attr('src','has_children.gif');next_next_node.addClass('taxon-hide');}}}next_node.addClass('taxon-hide');}})})效果:
调用模型$cate=newCateModel();$res=$cate->catetree();模型实现递归publicfunctioncatetree(){$cateres=$this->select();return$this->sort($cateres);}publicfunctionsort($data,$pid=0,$level=0){static$arr=array();foreach($dataas$key=>$v){if($v['pid']==$pid){$v['level']=$level;$arr[]=$v;$this->sort($data,$v['id'],$level+1);}}return$arr;}3.视图输出<!--表格显示-->{volistname="res"id="vo"}<tr><tdalign="center">{$vo.id}</td><tdalign="left"><?phpechostr_repeat('--------|',$vo['level'])?>{$vo.catename}</td><tdalign="center"><!--/以数组的方式传送参数id,用$vo['参数']这种方式/--><ahref="{:url('edit',array('id'=>$vo['id']))}"class="btnbtn-primarybtn-smshiny"><spanclass="glyphiconglyphicon-edit"></span> 编辑</a><ahref="{:url('del',array('id'=>$vo['id']))}"onClick="window.confirm('确实要删除吗','index.html')"class="btnbtn-dangerbtn-smshiny"><spanclass="glyphiconglyphicon-trash"></span> 删除</a></td></tr>{/volist}