public static function getbak($type,$fromTime,$endTime,$pageno=1,$pagesize){
if($pageno<1) $pageno = 1 ;//翻页函数:
if($type){
global $lang;
$type = intval($type); //$type是int格式,intval防止数字溢出,假如$type是字符串(包含汉字)使用trim()删除空白符
if(!$type) exit("$lang[plz_select_type]");
if($fromTime) $fromTime = str_replace("-",'' ,substr($fromTime,0,10));把2011-01-01 14:38:59 转化成20100101格式
else $fromTime='20080101';
if($endTime) $endTime = str_replace("-",'' ,substr($endTime,0,10));
else $endTime=date("Ymd",time());//条件判断输出的if(expr)else。。多个输出if(expr){。。。}else{。。。}
----------------*******************
if($fromTime and $endTime) {
$sqladd = " and `day` >= '$fromTime' and `day` <= '$endTime' ";
}elseif($fromTime){
$sqladd = " and `day` >= '$fromTime' ";
}elseif($endTime){
$sqladd = " and `day` =< '$endTime' ";
}
if($uid){
$sqladd = " and `uid` = '$uid' ";
}
--------------------**************
上面那段判读又等于下面这段:$sql后面条件的书写,认真体会:(and前面要有空格,$addsql.后面的连接符不要忘,'$fromTime'条件值一定要加单引号)
$addsql.="where 1=1";
($fromTime&&$endTime)&&$addsql.=" and `day` >= '$fromTime' and `day` <= '$endTime'";
$uid&&$addsql.="and uid='$uid'";
---------------------*************************
$sql = "select * from ".self::$_table . " where `type` = '$type' $sqladd limit ". ($pageno-1)*$pagesize .",$pagesize" ;//翻页变量
// return $sql;打印出来
$data = parent::get_all($sql);取出全部数据,下面把$data强制转换为array格式
foreach((array)$data as $k=>$v) {
$day = $v["day"];//对应二维数组,重新建立索引---时间字段
$data1[$day]["day"] = $day;
$soldierinfo='';//写把$soliderinfo写入内存,在读进数据,可以提高写入内存效率
$soldierinfo = unserialize($v["soldierinfo"]);//反序列化,=》转化成数组,serialize序列化把数组转化成数据库可存储格式
if(is_array($soldierinfo))//判断是否是数组,is_integer是否是整数,!empty()判断变量是否为空,isset()判断变量是否设置
{
foreach($soldierinfo as $k2=>$v){
$data1[$day]["soldierinfo"][$k2] += $soldierinfo[$k2];//求各个字段的值,采用了foreach循环,而不是逐个列出,方便以后即时加字段也不需修改代码
$data1[$day]["all"] += $v;//求各个字段的总和,使用了+=符号,自动累加
}
}
}
return $data1;
}
}
**********——————————————————————
对应sql语句的另外一种执行方式
$res=mysql_query($sql);
if(mysql_num_rows($res)<1)//判断取出数据是否存在
return false;
while($row=mysql_fetch_array($res)){
$time=$row['day'];
$return[$time]['type']=$row['type'];
$return[$time]['day']=$row['day'];
$soldierinfo='';
$soldierinfo = unserialize($row["soldierinfo"]);
if(is_array($soldierinfo)){
foreach($soldierinfo as $k=>$v){
$return[$time]['soldierinfo'][$k]=$soldierinfo[$k];
$return[$time]['all']+=$v;
}
}
}
return $return;
}
=======================
$soldierinfo打印出结果:
(Object)#0
1 = 392276
2 = 128176
3 = 921
4 = 271652
5 = 33948
6 = 0
7 = 0
查询类方法代码分析
最新推荐文章于 2021-02-12 17:48:59 发布