阅读次数

This commit is contained in:
rainerosion 2022-02-05 18:23:52 +08:00
parent b285084411
commit 6c99c1142f
2 changed files with 30 additions and 2 deletions

View File

@ -81,4 +81,31 @@ function articleCount($cid)
$rs = $db->fetchRow($db->select('table.contents.text')->from('table.contents')->where('table.contents.cid=?', $cid)->order('table.contents.cid', Typecho_Db::SORT_ASC)->limit(1));
$text = preg_replace("/[^\x{4e00}-\x{9fa5}]/u", "", $rs['text']);
return mb_strlen($text, 'UTF-8');
}
/**
* 获取文章访问数
* @param $archive
* @return string|void
* @throws Typecho_Db_Exception
*/
function articleViewsNum($archive) {
$cid = $archive->cid;
$db = Typecho_Db::get();
$prefix = $db->getPrefix();
if (!array_key_exists('views', $db->fetchRow($db->select()->from('table.contents')))) {
$db->query('ALTER TABLE `' . $prefix . 'contents` ADD `views` INT(10) DEFAULT 0;');
echo 0;
return;
}
$row = $db->fetchRow($db->select('views')->from('table.contents')->where('cid = ?', $cid));
if ($archive->is('single')) {
$db->query($db->update('table.contents')->rows(array('views' => (int) $row['views'] + 1))->where('cid = ?', $cid));
}
if($row['views'] < 1000){
return $row['views'];
} else {
return ($row['views'] / 1000) .'k';
}
}

View File

@ -40,13 +40,14 @@ $this->need('base/article.style.php');
<?php _e(date('Y-m-d', $this->modified)) ;?>
</div>
<div class="info-break-policy"><i class="far fa-file-word fa-fw"></i>文章字数:&nbsp;&nbsp;
<!-- --><?// echo $this->cid;?>
<?php _e(articleCount($this->cid));?>
</div>
<div class="info-break-policy"><i class="far fa-clock fa-fw"></i>阅读时长:&nbsp;&nbsp; 1
</div>
<div id="busuanzi_container_page_pv" class="info-break-policy"><i
class="far fa-eye fa-fw"></i>阅读次数:&nbsp;&nbsp; <span
class="far fa-eye fa-fw"></i>阅读次数:&nbsp;&nbsp;
<?php _e(articleViewsNum($this));?>
<span
id="busuanzi_value_page_pv"></span></div>
</div>
</div>