繁体   English   中英

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

[英]How to display all posts under main category and its subcategories?

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

Music
Pop
Nature
Lakes
Sea

我想实现

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

如您所见,“音乐”类别下有5个帖子,而“流行音乐”下有3个帖子,但这并不意味着现实中有8个帖子。 实际上,只有5个帖子,因为Pop是主要类别Music的子类别。 2个帖子直接位于音乐的主要类别下,而3个帖子属于其子类别Pop。 因此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)

显然,父类别具有与之直接相关而不与其子类别直接相关的职位总数。 如何解决?

我能想到的方法:

  • post_parent上的posts表进行分组。
  • 从中形成一个关联数组,索引为category_id ,对值为post_count

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

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

    假设类别4(流行音乐)有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.

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM