php统计某些作者在表中发表的文章

php统计某些作者在表中发表的文章,php,foreach,count,Php,Foreach,Count,可能很简单,但我有几个问题 我所要做的就是“数一数”某个“作者”从某个“类别”中拥有多少帖子 表格设置: id | author | category | full | short 1 jim 2,3 ... ... 2 josh 5,9 ... ... 3 jim 3,9 ... ... 4 jim 1,9 ... ... 5

可能很简单,但我有几个问题

我所要做的就是“数一数”某个“作者”从某个“类别”中拥有多少帖子

表格设置:

id | author | category | full | short

1      jim       2,3      ...     ...
2      josh      5,9      ...     ...
3      jim       3,9      ...     ...
4      jim       1,9      ...     ...
5      josh      3,4      ...     ...
6      trina     2        ...     ...
我知道如何提问

但是,如何编写“foreach”代码

如果我查询,则统计有多少篇文章(示例查询)

因此,它会向我展示该作者的帖子,这没什么大不了的,但我如何展示:

如果$author=“jim”

Jim(第9类中的2个帖子)

或者如果$author=“josh”

Josh(第9类1篇帖子)

提前谢谢

这是我用过的,谢谢

<?php echo $row_Recordset1['author']; echo '(' . $totalRows_Recordset1 . ')'; ?>

您可以创建一个多维数组,将作者作为索引,每个作者索引可以是另一个类别数组和多少篇文章

$postCounts = array();
foreach($results as $result) {
    if (!array_key_exists($result['author'], $postCounts)) {
        $postCounts[$result['author']] = array();
    }

    if (!array_key_exists[$result['category'], $postCounts[$result['author']])) {
        $postCounts[$result['author']][$result['category'] = 1; // first post seen
    } else {
        $postCounts[$result['author']][$result['category'] += 1; // increment count
    }
}
但是,从查看分类结果来看,您可能需要用逗号分解它们,并在循环中添加第二个数组\u key\u exists块。但这可以在PHP foreach中实现您想要的功能

最后,您会得到如下数组:

$postCounts = Array(
    'jim' => Array(9 => 2) // 2 posts in category 9 for jim
    'josh' => Array(9 => 1) // 1 post in category 9 for josh
    'bob' => Array(9 => 3, 2 => 5) // 3 posts in category 9, 5 posts in category 2.
)

您可以创建一个多维数组,该数组将作者作为索引,每个作者索引可以是另一个类别数组和多少篇文章

$postCounts = array();
foreach($results as $result) {
    if (!array_key_exists($result['author'], $postCounts)) {
        $postCounts[$result['author']] = array();
    }

    if (!array_key_exists[$result['category'], $postCounts[$result['author']])) {
        $postCounts[$result['author']][$result['category'] = 1; // first post seen
    } else {
        $postCounts[$result['author']][$result['category'] += 1; // increment count
    }
}
但是,从查看分类结果来看,您可能需要用逗号分解它们,并在循环中添加第二个数组\u key\u exists块。但这可以在PHP foreach中实现您想要的功能

最后,您会得到如下数组:

$postCounts = Array(
    'jim' => Array(9 => 2) // 2 posts in category 9 for jim
    'josh' => Array(9 => 1) // 1 post in category 9 for josh
    'bob' => Array(9 => 3, 2 => 5) // 3 posts in category 9, 5 posts in category 2.
)

您只需通过sql查询即可完成,无需计算foreach。没有经过测试你可以这样做吗

(未测试)

然后你就可以直接用

mysql_num_rows

您只需通过sql查询即可完成,无需计算foreach。没有经过测试你可以这样做吗

(未测试)

然后你就可以直接用

mysql_num_rows

这是家庭作业吗?如果是这样的话,让我们这样标记。这是家庭作业吗?如果是这样的话,让我们这样标记。