首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Concrete5属性计数

Concrete5属性计数
EN

Stack Overflow用户
提问于 2013-08-16 23:56:00
回答 2查看 142关注 0票数 1

我们用Concrete5构建了一个最初在Joomla开发的站点。我们的工作是把所有的东西都带来,然后把它变成现实。这个网站的主要部分是大约1200个音频教学,每个教学都有不同的属性,如主题、作者、程序、位置等。

一些教导可能有多个属性分配,例如多个关键字或主题。

我想对所有的属性进行计数,以便访问者能够看到某一作者的教导有多少,或者有多少是关于某一特定主题的,即:

  • 道德操守(20)
  • 恐惧(42)
  • 感激(55)

我原来的代码被无意中听到了太多,对这么多的教导和许多属性来说都是实用的。基本上,我遍历了每个属性,并根据PageList计数对总计数进行了查找。我们讨论的是每一页加载的数百个查找。在这里打开缓存似乎没什么帮助。

在大量页面上聚集属性计数方面,是否有其他策略被证明是成功的?

这里是供参考的站点:http://everydayzen.org/teachings/

EN

回答 2

Stack Overflow用户

发布于 2013-08-18 14:41:45

我通常会说“不要直接访问数据库;使用API",但我认为您应该在这里使用DB。

查看[Collection|File]SearchIndexAttributes表。(我不确定教诲是档案还是书页。如果页面,则需要通过仪表板中的作业定期重新索引它们。)查看索引表比加入属性值表中的最新版本要容易得多。一旦看到该表,就可以在SQL中执行一些简单的GROUPing。

如果您想使用这个API,您可以像今天这样作为一个批处理执行它,执行适当的计算,然后缓存它。

缓存没有理由不起作用,但是第一个命中(当缓存是冷的)当然会花费大量的时间。您应该缓存我的IndexAttributes想法(完整的表读取和循环并不简单),但至少在冷缓存中,可能需要一秒到10秒或更长的时间,数百个页面列表调用可能会占用这些时间。

票数 1
EN

Stack Overflow用户

发布于 2016-07-05 14:13:15

我在Concrete5的一个求职网站上做了一些类似的事情,通过显示每个部门的数量,工作减少了。

即人力资源(32)、销售(12)等等

这是从助手那里获取的代码(这只是包含的相关功能):

代码语言:javascript
复制
<?php
class JobHelper {
/**
* GetDepartmentJobsCount
* Returns array of Department names with job count based on input Pages
* @param Array(Pages) - Result of a PageList->getPages
* @return Array
*/
public function getDepartmentJobsCount($pages) {
    $depts = $this->getDepartments();
    $cj = $this->setCounts($depts);        
    $cj = $this->setAttributeCounts($cj, $pages,'job_department');
    return $cj;
}

/**
* GetDepartments
* Return all available Departments
* @return Array(Page)
*/
public function getDepartmentPages(){
    $pld = new PageList();
    $pld->filterByPath('/working-lv'); //the path that your Teachings all sit under
    $pld->setItemsPerPage(0);
    $res = $this->getPage();
    $depts = array();
    foreach($res as $jp){
        $depts[$jp->getCollectionName()] = $jp;
    }
    ksort($depts);
    return $depts;
}

/**
* PopulateCounts
* Returns array of page names and counts
* @param Array - Array to feed from
* @return Array
*/
public function setCounts($v){
    foreach($v as $w){
        $a[$w]['count'] = 0;
    }
    return $a;
} 

/**
* PopulateCounts
* Returns array of page names, with counts added from attribute, and paths
* @param Array - Array to add counts and paths in to
* @param Array(Pages) - Pages to run through
* @param String - Attribute to also add to counts
* @param String - Optional - Job Search parameter, leave blank to get Page URL
* @return Array
*/
public function setAttributeCounts($cj, $pages, $attr){
    foreach($pages as $p) {
        $pLoc = explode('|',$p->getAttribute($attr));  // Our pages could have multiple departments pipe separated
        foreach($pLoc as $locName){
            $cj[$locName]['count']++;
        }           
    }
    return $cj;
}

然后,您可以从PageList模板执行以下操作

代码语言:javascript
复制
$jh = Loader::helper('job');
$deptCounts = $jh->getDepartmentJobsCount($pages);
foreach($deptCounts as $dept => $data) { 
    echo $dept . '(' . $data['count] . ')';
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18283705

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档