cakephp - cake - Confused about how to count data -
i know question simple. fail find in google. i'm new cake afterall.
for example have 2 model: product
, category
.
in product index
want show list of available categories , amount of products in category. below:
food (10) beverages (7) ...
i looped available categories, not sure how count it. below loop:
<?php foreach($categories $c): ?> <li> ... <?php echo $this->$c->product->find('count') ?> </li> <?php endforeach; ?> // ... part echo-ing category name
i tried many variations , keep getting error arrayhelper not found
or producthelper not found
anyone have solution?
thanks
edit
this productcontroller code before added foreach
loop suggested.
public function index() { //the first 2 lines default cake bake $this->product->recursive = 0; $this->set('products', $this->paginate()); $categories = $this->product->category->find('all'); $this->set('categories', $categories); }
you trying call model
in view file, instead add product count array controller itself, like, example code be: in controller:
$categories = $this->product->category->find('all',array('recursive'=>-1)); foreach($categories $key => $category){ $categories[$key]['productcount'] = $this->product->find('count', array( 'conditions'=>array('product.category_id'=>$category['category']['id']) ) ); }
where productcount
can replaced name of product
Comments
Post a Comment