【问题标题】:Get all post and count from db phalcon从 db phalcon 获取所有帖子和计数
【发布时间】:2016-07-30 05:10:54
【问题描述】:

[用例]

我的博客中有 2 个用户。

用户 1 有 3 个帖子,并具有以下浏览量:

  • 发布 1:20 次浏览
  • 帖子 2:40 次浏览
  • 发布 3:100 次浏览

用户 2 有 2 个帖子,并具有以下查看量:

  • 发布 1:15 次浏览
  • 帖子 2:40 次浏览

[问题]

我想计算phalcon 中每个用户的(所有帖子的)总浏览量。我想不通。我无法绑定或进行此查询。

[控制器]

public function index()
{   
  $pcount = Blogs::findBybauthor($this->session->get('uname'));
  $posted = count($pcount);
  $x = [];
  $v = Blogs::findBybauthor($this->session->get('uname'));
  foreach($v as $a)
  {
   echo($a->views.'<br/>');
  }    
}

【问题讨论】:

  • 你可以用count:Blogs::countBybauthor($this-&gt;session-&gt;get('uname'));代替find。这将返回authorblogs 中出现的次数
  • 实际上我想计算每个用户帖子中达到了多少人。这就是为什么我首先要计算每个用户有多少帖子,然后有多少人到达该用户帖子的每个帖子,然后添加它。
  • 那么您想要查看/用户的总数还是所有帖子的查看总数?
  • 是的!绝对正确!蒂莫西。我想要所有帖子的总浏览量/用户数

标签: php database count blogs phalcon


【解决方案1】:

我目前无法验证此示例。 But from looking at the docs,这应该会为您指明正确的方向。

我们不使用find,而是使用sum

这将返回一个包含总浏览量的数组,按作者分组。

$viewsPerAuthor = Blogs::sum(
    array(
        "column" => "views",
        "group"  => "bauthor"
    )
);

这将返回特定作者的总浏览量

$total = Blogs::sum(
    array(
        "column"     => "views",
        "conditions" => "bauthor = :author:",
        "bind"       => array('author' => $this->session->get('uname'))
    )
);

【讨论】:

    猜你喜欢
    • 2017-04-21
    • 2019-11-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-16
    • 1970-01-01
    • 2015-05-10
    • 1970-01-01
    相关资源
    最近更新 更多