【问题标题】:How to display all posts under main category and its subcategories?如何显示主类别及其子类别下的所有帖子?
【发布时间】:2016-02-18 14:55:15
【问题描述】:

我有一个自定义的 php/mysql 博客。在单个帖子页面中,我有一个显示类别的侧边栏。正如我们通常在 WordPress 中看到的那样,我想在括号中显示属于主要类别及其子类别的帖子数量。现在我有

Music
Pop
Nature
Lakes
Sea

我想实现

Music (5)
Pop (3)
Nature (8)
Lakes (2)
Sea (4)

如您所见,音乐类别下有 5 个帖子,流行有 3 个。但这并不意味着实际上有 8 个帖子。实际上只有 5 个帖子,因为流行是主要类别音乐的子类别。 2 个帖子直接属于主要类别音乐,而 3 个帖子属于其子类别流行。因此 2 + 3 = 5。

类别表如下所示(注意它有第 n 级子类别):

帖子表看起来像(请注意,特定帖子只能有一个类别父级,即帖子不能有多个父级)

我获取类别及其子类别的代码是

public function get_category_hierarchy(&$output, $parent_url, $parent = 0)
{
    $sql = "SELECT * FROM `tb_categories` WHERE `category_parent` = " . $parent . " ORDER BY `category_name`";
    $rows = $this->get_by_sql($sql);                    

    // Removing trailing slash
    $parent_url = rtrim($parent_url, "/");

    if(is_array($rows))
    {
        foreach($rows as $row) :
            $url = $parent_url . "/" . $row->category_url;
            $output.= "<li><a href='" . $url . "'>" . $row->category_name . "</a></li>";
            if($row->category_id != $parent)
            {
                $this->get_category_hierarchy($output, $url, $row->category_id);
            }
        endforeach;
    }
    return $output;     
}

在侧边栏,我通过以下代码显示这些类别:

$output = '';
// Create post object
$post_obj = new Post();
echo $post_obj->get_category_hierarchy($output, SITE_URL . 'category');

现在我想知道需要添加哪些代码来获取属于主要类别以及随后属于其子子类别的帖子数量(帖子计数)?虽然我试图获取它们,但结果并不准确。 提前谢谢你。

编辑:

我通过以下修改修改了函数 get_category_hierarchy():

foreach($rows as $row) :
    $url = $parent_url . "/" . $row->category_url;

    // Build database query
    $sql = "SELECT COUNT(*) AS 'total_posts' FROM `tb_posts` WHERE `post_parent` = " . $row->category_id . " GROUP BY `post_parent`";

    // Execute database query
    $rows = $this->get_by_sql($sql);

    $output[] = "<li><a href='" . $url . "'>" . $row->category_name . " (" . $rows[0]->total_posts . ")</a></li>";

但是哎呀!它给了我以下结果:

Music (2)
Pop (3)
Nature (2)
Lakes (2)
Sea (4)

显然,父类别的帖子总数与其直接关联,而不是与其子类别关联。如何解决?

【问题讨论】:

    标签: php mysql


    【解决方案1】:

    我能想到的方法:

    • 在post_parent 的帖子表上进行分组。
    • 由此形成一个关联数组,其索引为category_id,配对值为post_count。

      例如。 $category_post_counts = array("4" => 5, "5" => 10)

    • 现在为所有类别 ID 运行 foreach 循环并获取其 对应的父级。
    • 使用索引更新帖子计数。

      假设类别 id 4(pop) 有 5 个帖子,并且在音乐中有 是10个帖子。如果类别父级是任何东西,现在在循环中 否则除了 0 然后更新数组中的帖子计数。

      例如。 $category_post_counts["<category_parent_id>"] = $category_post_counts["<category_parent_id>"] + $category_post_counts["<category_child_id>"]

    这样你的最终结果 将是一个具有类别 ID 和所有帖子的关联数组 很重要。

    伪代码

    //exctracting all the data
    $rows = execute_this_query("SELECT COUNT(post_id) AS `post_count`, `post_parent` FROM `posts` GROUP BY `post_parent`");
    
    //forming the associative array
    foreach ($rows as $value) {
        $associative_array[value["post_parent"]] = $value["post_count"]; 
    }
    
    $data = execute_this_query("SELECT `category_id`,`category_parent` FROM `categories` WHERE `category_parent` <> 0");
    
    foreach ($data as $value) {
        $associative_array[$value["category_parent"]] += $associative_array[$value["category_id"]]; 
    }
    
    
    //Your final associative array has the exact category_id and net post count as you want.
    

    【讨论】:

    • 您能否根据我给定的代码提出您的想法?
    • 好的,但是你能检查一下我上面编辑的代码吗?我哪里做错了?
    • 试试我写的代码。您的代码使用了不同的方法。您只需将 execute_this_query 替换为您的 sql 执行和连接语句。
    • 好的,我重新配置了您的代码以适应我的情况。效果很好!我现在有一个具有精确 id 和净帖子计数的关联数组。但是正如您看到我的代码一样,我正在执行递归函数来获取类别层次结构,因此我需要在我的代码中匹配与类别 ID 对应的这些净帖子计数,这样我就不会丢失类别层次结构 URL 结构。
    • 对不起,我不接受这个答案,因为它无法正确计算间接属于父类别的帖子。我写了一个更好、更简单的解决方案来实现同样的目标。
    【解决方案2】:

    首先,您可以为每个类别中的数量填充一个关联数组,例如:category_id =&gt; post_qty。

    之后,您可以使用下面的函数来计算每个类别及其子类别:

    function count_posts($category_id, $cache) {
        $sum = 0;
    
        $sql = "SELECT * FROM `tb_categories` WHERE `category_parent` = " . $category_id;
        $rows = $this->get_by_sql($sql);        
    
        foreach ($rows as $child) {
            $sum += count_posts($child->category_id, $cache);
        }
        $sum += $cache[$category_id];
        return $sum;
    }
    

    【讨论】:

    • 我的函数get_category_hierarchy()需要做哪些修改?我无法理解你的概念。
    • 我发布的代码有一个名为sum 的变量,我在其中汇总了一个类别及其子类别的帖子。您的逻辑已经遍历所有类别,但您没有汇总帖子数。
    • 如果您在打印他们的孩子之后打印帖子计数,您的方法将只允许总和。
    • 哦,这让我有点困惑!那么我不需要任何额外的 mysql 查询来计算属于类别及其子项的帖子数吗?
    • 最初我以为我必须添加另一个 mysql 查询来计算循环中的总帖子数foreach($rows as $row) :
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多