public function getRandTable(){ $num = 5; //需要抽取的默认条数 $table = ‘t_marclog‘; //需要抽取的数据表 $countcus = Db::name($table)->count(); //获取总记录数 $min = Db::name($table)->min(‘id‘); //统计某个字段最小数据 if($countcus < $num){$num = $countcus;} $i = 1; $flag = 0; $ary = array(); while($i<=$num){ $rundnum = rand($min, $countcus);//抽取随机数 if($flag != $rundnum){ //过滤重复 if(!in_array($rundnum,$ary)){ $ary[] = $rundnum; $flag = $rundnum; }else{ $i--; } $i++; } } $list = Db::name($table)->where(‘id‘,‘in‘,$ary,‘or‘)->select(); $this->assign(‘list‘, $list); }
转载于:http://www.tjcms.top/index/show/id/144