首页 文章

如何显示主类别及其子类别下的所有帖子?

提问于
浏览
1

我有一个自定义的php / mysql博客 . 在单个帖子页面中,我有一个侧栏,我在显示类别 . 我想显示那些属于主要类别及其子类别的帖子数量,如我们在WordPress中常见的那样 . 现在我有

Music
Pop
Nature
Lakes
Sea

我想实现

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

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

类别表看起来像(请注意,它具有第n级子类别):

enter image description here

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

enter image description here

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

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');

现在我想知道需要添加什么代码才能获取属于主要类别及其子类别的帖子数量(帖子计数)?虽然我试图获取它们但结果并不准确 . 先感谢您 .

EDIT:

我通过以下修改修改了函数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)

显然,父类别的帖子总数与其直接相关,而不是与其子项相关联 . 怎么解决?

2 回答

  • 1

    首先,您可以为每个类别中的数量填充关联数组,如: category_id => 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;
    }
    
  • 0

    我能想到的方法:

    • post_parent 上的帖子表上执行分组 .

    • 从该表单形成一个关联数组,索引为 category_id ,对值为 post_count .

    防爆 . $ category_post_counts = array(“4”=> 5,“5”=> 10)

    • 现在为所有类别ID运行 foreach 循环并获取其对应的父级 .

    • 使用索引更新帖子计数 .

    假设类别id 4(pop)有5个帖子,音乐中有10个帖子 . 现在在循环中,如果类别parent是除0之外的任何其他内容,则更新数组中的post计数 .

    防爆 . $category_post_counts["<category_parent_id>"] = $category_post_counts["<category_parent_id>"] + $category_post_counts["<category_child_id>"]

    这样,您的最终结果将是一个关联数组,其中包含类别ID和所有帖子计数 .

    Pseudo Code

    //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.
    

相关问题