I have created a custom module and created a database table using install schema like below:

id      color     price_component
1       white     0
2       red       4
3       green     8
4       black     12

How can I display this table in my .phtml file?

I'm new to Magento. If someone can explain this step by step that would be really helpful.

  • You Mean Admin custom grid @Hemal Commented Sep 4, 2017 at 4:52
  • yes something like that i need to display that table in my frontend .phtml file
    – Hemal
    Commented Sep 4, 2017 at 5:13
  • Check Below i have updated you the answer @Hemal Commented Sep 4, 2017 at 5:38

2 Answers 2


You can used this for getting table in phtml file

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$resource = $objectManager->get('Magento\Framework\App\ResourceConnection');
$connection = $resource->getConnection();
$tableName = $resource->getTableName('your_table_name');

//Select Data from table
$sql = "Select * FROM " . $tableName;
$result = $connection->fetchAll($sql);
  • No if table are there than you can get directly.If you have model than called model instance of calling connection.
    – Impunkj
    Commented Sep 4, 2017 at 5:49
  • You can use whatever you want and $result is array that will get all value and print value by using foreach.
    – Impunkj
    Commented Sep 4, 2017 at 6:02
  • If you get your answer than please mark as solved
    – Impunkj
    Commented Sep 4, 2017 at 6:07
  • I think you are very new. used foreach ($reult as $results){ print_r($results); } Used under php
    – Impunkj
    Commented Sep 4, 2017 at 6:13

You need to create block, template and layout files for this. Create a new file app\code\Your_ExtensionName\Your_ModuleName\Block\Post.php

namespace Your_ExtensionName\Your_ModuleName\Block;
use Magento\Framework\View\Element\Template;
class Post extends Template
    protected $mymodulemodelFactory;
    public function __construct(
        Template\Context $context,
        \Your_ExtensionName\Your_ModuleName\Model\ResourceModel\Post\CollectionFactory $mymodulemodelFactory,
        array $data = []
    ) {

        parent::__construct($context, $data);
        $this->mymodulemodelFactory = $mymodulemodelFactory;
        $this->_isScopePrivate = true;

    public function getPosts(){
        $collection = $this->mymodulemodelFactory->create();
        return $collection;     

Now create layout file app\code\Your_ExtensionName\Your_ModuleName\view\frontend\layout\Your_ModuleName_index_index.xml

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
        <referenceContainer name="content">
            <block class="Your_ExtensionName\Your_ModuleName\Block\Post" name="posts" template="Your_ExtensionName_Your_ModuleName::post.phtml">                

Now create app\code\Your_ExtensionName\Your_ModuleName\view\frontend\templates\post.phtml

<?php $collection = $this->getPosts(); ?>
<div class="yourtable">
    <?php foreach ($collection as $item){ ?>
                 <div class="yourtable">   
                        // Display your data
     <?php  } ?> 
  • Hi @Learning_Coder is the above answer working fine for you, for me block is not calling Commented Mar 11, 2019 at 17:10

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.