diff --git a/functions.php b/functions.php index 9f437a4..a9c9d55 100644 --- a/functions.php +++ b/functions.php @@ -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'; + } } \ No newline at end of file diff --git a/post.php b/post.php index 316a1ea..60d21d6 100644 --- a/post.php +++ b/post.php @@ -40,13 +40,14 @@ $this->need('base/article.style.php'); modified)) ;?>