1

I want SELECT COUNT (*) FROM name_table WHERE result LIKE 'likes%' to be executed, and a numeric value is displayed on the page.How do I do it? I'm a beginner. I'll be very grateful for your help!

/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->addAttributeToFilter('result', ['like' => 'test'])
        ->load();
    $collection->count();



    return $collection;
  }
 }

/Model/ResourceModel/Reviews.php

<?php
namespace Training\Reviews\Model\ResourceModel;
class Reviews extends 
\Magento\Framework\Model\ResourceModel\Db\AbstractDb
{

protected function _construct()
{
$this->_init('training_reviews', 'news_id');
 }
}

Model/ResourceModel/Reviews/Collection.php

<?php
namespace Training\Reviews\Model\ResourceModel\Reviews;

class Collection extends 
\Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection
{

protected function _construct()
{
$this->_init(
    'Training\Reviews\Model\Reviews',
    'Training\Reviews\Model\ResourceModel\Reviews'
);
 }

} 

/Model/Reviews.php

<?php
namespace Training\Reviews\Model;
use Magento\Framework\Model\AbstractModel;
class Reviews extends AbstractModel
{

protected function _construct()
{
    $this->_init('Training\Reviews\Model\ResourceModel\Reviews');
}



} 

/view/frontend/templates/reviews.php

<?php
$data->getProductCollection();

?>
0

Update the code of

/Block/Reviews.php

with below and give a try.

<?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' => '%test%']);
        return $collection;
    }
 }

You can add $collection->count() function if you want.

1
  • Your information has helped! Please tell me how to implement so that I can see the number of these lines, not their own – pixartN Feb 9 '20 at 12:56

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.