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!

0

Use count() function.

$c = count($productCollection);
echo "totle count is ".$c;
|improve this answer|||||
  • Thank you very much! – pixartN Feb 9 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

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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