thinkphp3文章分类统计文章

文章描述:

thinkphp3统计栏目下面的文章数量

SQL

SET FOREIGN_KEY_CHECKS=0;

-- ----------------------------
-- Table structure for `tp_category`
-- ----------------------------
DROP TABLE IF EXISTS `tp_category`;
CREATE TABLE `tp_category` (
  `catid` int(11) NOT NULL AUTO_INCREMENT,
  `catname` varchar(50) COLLATE utf8_bin DEFAULT NULL,
  `catsmall` varchar(200) COLLATE utf8_bin DEFAULT NULL,
  PRIMARY KEY (`catid`)
) ENGINE=MyISAM AUTO_INCREMENT=3 DEFAULT CHARSET=utf8 COLLATE=utf8_bin;

-- ----------------------------
-- Records of tp_category
-- ----------------------------
INSERT INTO `tp_category` VALUES ('1', '产品', '产品描述');
INSERT INTO `tp_category` VALUES ('2', '新闻', '新闻描述');

SET FOREIGN_KEY_CHECKS=0;

-- ----------------------------
-- Table structure for `tp_article`
-- ----------------------------
DROP TABLE IF EXISTS `tp_article`;
CREATE TABLE `tp_article` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `cat_id` int(11) DEFAULT NULL,
  `title` varchar(50) COLLATE utf8_bin DEFAULT NULL,
  `smalltext` varchar(200) COLLATE utf8_bin DEFAULT NULL,
  `content` mediumtext COLLATE utf8_bin,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=5 DEFAULT CHARSET=utf8 COLLATE=utf8_bin;

-- ----------------------------
-- Records of tp_article
-- ----------------------------
INSERT INTO `tp_article` VALUES ('1', '1', '苹果手机', '苹果手机是美国生产', 0xE79BAEE5898DE79A84E78988E69CACE69C896970686F6E6534E380816970686F6E6535);
INSERT INTO `tp_article` VALUES ('2', '1', 'oppo手机', 'oppo手机来着中国', 0x6F70706F2052313153);
INSERT INTO `tp_article` VALUES ('3', '1', 'vovo手机', 'vovo手机来着韩国', 0xE99FA9E59BBDE7BE8EE5A5B3E5A49A);
INSERT INTO `tp_article` VALUES ('4', '2', '限购政策', '国家规定限购政策', 0xE59BBDE5AEB6E8A784E5AE9AE99990E8B4ADE694BFE7AD96E4B9B0E688BF);

控制器

namespace Home\Controller;
use Think\Controller;
class IndexController extends Controller {
    public function index(){
        $category_model = M("category");
        $article_model = M("article");
        $condition = '';
        $cateCount = $category_model->where($condition)->count();
        $cate = $category_model->field('catid,catname')->where($condition)->select();

        for($i=0;$i<$cateCount;$i++)
        {
            $cate[$i]['count'] = $article_model->where(array('cat_id'=>$cate[$i]['catid']))->count();
            //dump($article_model->getLastSql());
        }
        $this->assign('cate',$cate);
        //dump($cate);
        $this->display();
    }

}

 

视图

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<ul>
    <volist name="cate" id="vo">
        <li><a>{$vo['catname']}</a>({$vo['count']})</li>
    </volist>
</ul>
</body>
</html>
发布时间:2021/07/06

发表评论