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);
}
}
asked Mar 3, 2020 at 10:25
Pramod
1,4841 gold badge16 silver badges42 bronze badges
1 Answer 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.
answered Mar 3, 2020 at 10:34
Rohan Hapani
17.6k9 gold badges57 silver badges99 bronze badges
-
then how to use it in phtml file??Pramod– Pramod2020年03月03日 10:40:07 +00:00Commented 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 blockRohan Hapani– Rohan Hapani2020年03月03日 10:40:26 +00:00Commented Mar 3, 2020 at 10:40
-
ok let me try give me a minPramod– Pramod2020年03月03日 10:41:20 +00:00Commented Mar 3, 2020 at 10:41
-
this is not working in my casePramod– Pramod2020年03月03日 10:49:36 +00:00Commented Mar 3, 2020 at 10:49
-
check your db data available or not and did you call your block in layout?Rohan Hapani– Rohan Hapani2020年03月03日 10:50:31 +00:00Commented Mar 3, 2020 at 10:50
default