0

/Block/Reviews.php

<?php
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,
    \Training\Reviews\Model\ResourceModel\Reviews\CollectionFactory 
$productCollectionFactory,
    array $data = []
) {
    $this->_productCollectionFactory = $productCollectionFactory;

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

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

/view/frontend/templates/reviews.php

<?php
$productCollection = $this->getProductCollection();
foreach ($productCollection as $product) {
print_r($product->getData());
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

0

Use count() function.

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

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 that you have read and understand our privacy policy and code of conduct.

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