2

I have products table and want to select all products. This is what I have done

$products = $this->Products->find('all', [
   'conditions' => [
       'status' => 1
   ]
]);
$this->set('products', $products);

and print products if fetched and No product found when no product is retrieved.

This is what I have done for this

if (!empty($products)):
   // show products
else:
   echo 'No Products Found';
endif;

But this is not working, even if no product is found else condition is not printed.

If condition is not even working in controller action. Is there something missing ?

I'm using CakePHP 3.2

Gaurav
  • 131
  • 12
  • 1
    Have you tried to dump the content of `$products`? – dlondero Aug 14 '16 at 07:56
  • do you mean `debug` ? If yes, Yes I tried to debug but there is no products. I tried even after emptying the table from `phpmyadmin` and even then it is not working – Gaurav Aug 14 '16 at 08:03
  • 2
    Which version of CakePHP are you using? You can't expect to get the appropriate assistance unless you provide these basic details.. – Indrasis Datta Aug 14 '16 at 12:47

3 Answers3

7

for check content that is null or not use function isEmpty() in CakePHP 3.0.5

if (!$products->isEmpty()) {
   // show products  
}
else
   echo 'No Products Found';
ashkufaraz
  • 5,179
  • 6
  • 51
  • 82
0

Try this..

if (!empty($products->toArray())):
   // show products
else:
   echo 'No Products Found';
endif;
0

If your cakephp is 3.0.5 or higher, read ashkufaraz's answer, else, you can use:

if(count($products) > 0)
 {
     //Print ya products
 }
else
 {
    echo 'No products found';
 }
Vini Antichrist
  • 236
  • 3
  • 6