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?

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

Your records in $brand is the number of brands you have.

<?php echo count($brand); ?>

If you want to perform a count on the brands you can do it like this in Cakephp 2.x

$data = $this->Brand->find('count');

It's a bit unclear what you're trying to achieve.