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);}条
- function get_one($sql, $type = '', $ttl = 0) {
- $sql = str_replace(array('select ', ' limit '), array('SELECT ', ' LIMIT '), $sql);
- if(strpos($sql, 'SELECT ') !== false && strpos($sql, ' LIMIT ') === false) $sql .= ' LIMIT 0,1';
- $query = $this->query($sql, $type, $ttl);
- $r = $this->fetch_array($query);
- $this->free_result($query);
- return $r;
- }
-
- function count($table, $condition = '', $ttl = 0) {
- global $DT_TIME;
- $sql = 'SELECT COUNT(*) as amount FROM '.$table;
- if($condition) $sql .= ' WHERE '.$condition;
- $r = $this->get_one($sql, $ttl ? 'CACHE' : '', $ttl);
- return $r ? $r['amount'] : 0;
- }