【小编推荐】destoon 产品总数,求购总数的统计办法,$db->count的原型

2014-10-14   |   发布者:梁国芳   |   查看:3320次

destoon

destoon 的首页上有这样的一段代码

<li>产品总数:<span>{php echo $db->count($DT_PRE.'sell_5', 'status=3', 1800);}</span></li>
<li>求购总数:<span>{php echo $db->count($DT_PRE.'buy_6', 'status=3', 1800);}</span></li>
<li>企业总数:<span>{php echo $db->count($DT_PRE.'company', '1', 1800);}</span></li>
<li>在线会员:<span>{php echo $db->count($DT_PRE.'online', '1', 1800);}</span></li>
这个几参数的原型在include/db_mysql.class.php中
第一个参数是表名,第二个参数是条件 ,第三个参数不太清楚具体的作用,从参数名ttl上看是应该生存期限,具体的实现,在文件中的原型

如果我们要统计当天的总数的话,就可以这样来实现   {php echo $db->count($DT_PRE.'sell_5', 'status=3 and TO_DAYS(now())=TO_DAYS(adddate)', 1);}条

 

[php]
  1. function get_one($sql$type = ''$ttl = 0) {  
  2.     $sql = str_replace(array('select '' limit '), array('SELECT '' LIMIT '), $sql);  
  3.     if(strpos($sql'SELECT ') !== false && strpos($sql' LIMIT ') === false) $sql .= ' LIMIT 0,1';  
  4.     $query = $this->query($sql$type$ttl);  
  5.     $r = $this->fetch_array($query);  
  6.     $this->free_result($query);  
  7.     return $r;  
  8. }  
  9.   
  10. function count($table$condition = ''$ttl = 0) {  
  11.     global $DT_TIME;  
  12.     $sql = 'SELECT COUNT(*) as amount FROM '.$table;  
  13.     if($condition$sql .= ' WHERE '.$condition;  
  14.     $r = $this->get_one($sql$ttl ? 'CACHE' : ''$ttl);  
  15.     return $r ? $r['amount'] : 0;  
  16. }