namespace Training\Reviews\Block;
use Training\Reviews\Model\ResourceModel\Reviews\Collection;

use Magento\Framework\View\Element\Template;

class Reviews extends \Magento\Framework\View\Element\Template

public function _prepareLayout()
    $this->pageConfig->getTitle()->set(__('Users Reviews'));

    return parent::_prepareLayout();

protected $_productCollectionFactory;
protected $_productVisibility;

public function __construct(
    \Magento\Framework\View\Element\Template\Context $context,
    array $data = []
) {
    $this->_productCollectionFactory = $productCollectionFactory;

    parent::__construct($context, $data);

public function getProductCollection()
    $collection = $this->_productCollectionFactory->create();
    $collection->addFieldToFilter('result', ['like' => '%likes%']);
    return $collection;


$productCollection = $this->getProductCollection();
foreach ($productCollection as $product) {
echo "<br>";

This function outputs rows and information about them, I only need the number of rows.

I would appreciate your help!

2 Answers 2


Use count() function.

$c = count($productCollection);
echo "totle count is ".$c;
  • Thank you very much!
    – pixartN
    Feb 9, 2020 at 14:14

Use $productCollection->getSize() if you don't want to load the collection. It's for a better performance. Take a look at class \Magento\Framework\Data\Collection\AbstractDb::getSize() to see more details.

Difference between getSize() and count() on collection

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.