0

I am trying to get my custom table data on frontend but i was not getting any success please suggest me by looking at below code thanks in advance

<?php
namespace vendor\module\Block;

use Magento\Framework\View\Element\Template;

class Postform extends Template
{


        const CONFIG_CAPTCHA_PUBLIC_KEY = 'quickrfq/google_options/googlepublickey';
        const CONFIG_CAPTCHA_THEME = 'quickrfq/google_options/theme';
        const CONFIG_CAPTCHA_ENABLE = 'quickrfq/google_options/captchastatus';

        const CONFIG_FILE_EXT_UPLOAD = 'quickrfq/upload/allow';

    protected $scopeConfig;

    protected $_connection;

    public function __construct(
    \Magento\Framework\View\Element\Template\Context $context,
    \Magento\Framework\App\ResourceConnection $resource
    )
    {

        $this->scopeConfig = $context->getScopeConfig();
        $this->_connection = $resource->getConnection();
        parent::__construct($context);
    }

    public function getTableData()
    {
        $myTable = $this->_connection->getTableName('fme_quickrfq');
        $sql     = $this->_connection->select()->from(
             ["tn" => $myTable]
        ); 
        $result  = $this->_connection->fetchAll($sql);
        return $result;

        //print_r($result);
    }

    public function getFormAction()
    {
        return $this->getUrl('quickrfq/index/post', ['_secure' => true]);
    }



    public function getCaptchaTheme()
    {

        $theme = $this->scopeConfig->getValue(self::CONFIG_CAPTCHA_THEME);
        return $theme;
    }

    public function isCaptchaEnable()
    {

        $enable = $this->scopeConfig->getValue(self::CONFIG_CAPTCHA_ENABLE);
        return $enable;
    }

    public function getAllowedFileExtensions()
    {

        $ext = $this->scopeConfig->getValue(self::CONFIG_FILE_EXT_UPLOAD);
        return $ext;
    }
    public function getPublicKey()
    {

        return $this->scopeConfig->getValue(self::CONFIG_CAPTCHA_PUBLIC_KEY);
    }
}

now in phtml file

<?php


 $myTabledata = $block->getTableData();
    print_r($myTabledata);

?>

update

<?php
namespace vendor\Module\Block;

use Magento\Framework\View\Element\Template;

class Postform extends Template
{


        const CONFIG_CAPTCHA_PUBLIC_KEY = 'quickrfq/google_options/googlepublickey';
        const CONFIG_CAPTCHA_THEME = 'quickrfq/google_options/theme';
        const CONFIG_CAPTCHA_ENABLE = 'quickrfq/google_options/captchastatus';

        const CONFIG_FILE_EXT_UPLOAD = 'quickrfq/upload/allow';

    protected $scopeConfig;

    protected $resourceConnection;

    public function __construct(
    \Magento\Framework\View\Element\Template\Context $context,
    \Magento\Framework\App\ResourceConnection $resource
    )
    {

        $this->scopeConfig = $context->getScopeConfig();
        $this->resourceConnection = $resourceConnection;
        parent::__construct($context);
    }

    public function getTableData()
    {
        $connection  = $this->resourceConnection->getConnection();
        $tableName   = $connection->getTableName('fme_quickrfq');
        $query = 'SELECT * FROM ' . $tableName;
        $results = $this->resourceConnection->getConnection()->fetchAll($query);
        return $result;
    }

    public function getFormAction()
    {
        return $this->getUrl('quickrfq/index/post', ['_secure' => true]);
    }



    public function getCaptchaTheme()
    {

        $theme = $this->scopeConfig->getValue(self::CONFIG_CAPTCHA_THEME);
        return $theme;
    }

    public function isCaptchaEnable()
    {

        $enable = $this->scopeConfig->getValue(self::CONFIG_CAPTCHA_ENABLE);
        return $enable;
    }

    public function getAllowedFileExtensions()
    {

        $ext = $this->scopeConfig->getValue(self::CONFIG_FILE_EXT_UPLOAD);
        return $ext;
    }
    public function getPublicKey()
    {

        return $this->scopeConfig->getValue(self::CONFIG_CAPTCHA_PUBLIC_KEY);
    }
}

1 Answer 1

1

Try to use this below code :

<?php
namespace Vendor\Module\Block;

use Magento\Framework\View\Element\Template;

class Postform extends Template
{
    protected $resourceConnection;

    public function __construct(
        \Magento\Framework\App\ResourceConnection $resource
    )
    {

        $this->resourceConnection = $resourceConnection;
        parent::__construct($context);
    }

    public function getTableData()
    {
        $connection  = $this->resourceConnection->getConnection();
        $tableName   = $connection->getTableName('fme_quickrfq');
        $query = 'SELECT * FROM ' . $tableName;
        $results = $this->resourceConnection->getConnection()->fetchAll($query);
        return $result;
    }
}

Make sure data available in table.

Remove generated and clean cache.

9
  • then how to use it in phtml file??
    – Pramod
    Commented Mar 3, 2020 at 10:40
  • No need to change in phtml file. Your phtml file code you can use. Just need to change in block Commented Mar 3, 2020 at 10:40
  • ok let me try give me a min
    – Pramod
    Commented Mar 3, 2020 at 10:41
  • this is not working in my case
    – Pramod
    Commented Mar 3, 2020 at 10:49
  • check your db data available or not and did you call your block in layout? Commented Mar 3, 2020 at 10:50

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.