doujianmin6527 2016-02-12 08:22
浏览 21
已采纳

cakephp:我如何显示每个类别的计数?

here is my tables

brands_tbls

  • id 1 2 3 4
  • name Levi's Lee Woodland Spyakr

product_tbls

  • id
  • brands_tbls_id
  • product_description
  • name
  • price

I m displaying my brands and also tried for product count by using this code.

PublicController.php

public function example()
{
    $this->loadModel("Brand");
    $data = $this->Brand->find('all');
    $this->set('brand', $data);
    $this->layout="display";


    $this->loadModel("Product");
    $data = $this->Product->find('count', array('conditions' =>
                                  array('brands_tbls_id' => '1')));
    $this->set('product1', $data);
    $this->layout="display";
}

below is my view file

example.ctp

<?php foreach($brand as $data) : ?>
  <?php foreach($data as $row) : ?>
   <li>
     <a href="#">
        <span class="pull-right">(<?php echo $product1;//--count   ?>)
        </span><?php echo $row["name"];//--brands   ?>
     </a>
   </li>
   <?php endforeach; ?>
<?php endforeach; ?>

by doing this i can get count of product from product_tbls where brands_tbls_id = 1

so it will display same product count for all brands.how can i get product count for all brands?

  • 写回答

2条回答 默认 最新

  • dongxiatuo9827 2016-02-12 12:43
    关注

    maybe what you want to achieve is to group your products by brand

    $data = $this->Product->find(
       'all', 
        array(
            'fields' => array('COUNT(*)'),
            'group' => 'brands_tbls_id',
            'contain' => array('Brand')
       )
    );
    

    so you can loop through $data

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 微信小程序 用oss下载 aliyun-oss-sdk-6.18.0.min client报错
  • ¥15 ArcGIS批量裁剪
  • ¥15 labview程序设计
  • ¥15 为什么在配置Linux系统的时候执行脚本总是出现E: Failed to fetch http:L/cn.archive.ubuntu.com
  • ¥15 Cloudreve保存用户组存储空间大小时报错
  • ¥15 伪标签为什么不能作为弱监督语义分割的结果?
  • ¥15 编一个判断一个区间范围内的数字的个位数的立方和是否等于其本身的程序在输入第1组数据后卡住了(语言-c语言)
  • ¥15 Mac版Fiddler Everywhere4.0.1提示强制更新
  • ¥15 android 集成sentry上报时报错。
  • ¥15 抖音看过的视频,缓存在哪个文件