1

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.

3
  • You Mean Admin custom grid @Hemal – Learing_Coder Sep 4 '17 at 4:52
  • yes something like that i need to display that table in my frontend .phtml file – Hemal Sep 4 '17 at 5:13
  • Check Below i have updated you the answer @Hemal – Learing_Coder Sep 4 '17 at 5:38
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);
4
  • No if table are there than you can get directly.If you have model than called model instance of calling connection. – Impunkj Sep 4 '17 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 Sep 4 '17 at 6:02
  • If you get your answer than please mark as solved – Impunkj Sep 4 '17 at 6:07
  • I think you are very new. used foreach ($reult as $results){ print_r($results); } Used under php – Impunkj Sep 4 '17 at 6:13
0

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

<?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">
    <body>
        <referenceContainer name="content">
            <block class="Your_ExtensionName\Your_ModuleName\Block\Post" name="posts" template="Your_ExtensionName_Your_ModuleName::post.phtml">                
            </block>
        </referenceContainer>               
    </body>
</page>

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">   
                    <?php 
                        // Display your data
                    ?>
                 </div>
     <?php  } ?> 
</div>
1
  • Hi @Learning_Coder is the above answer working fine for you, for me block is not calling – shivashankar m Mar 11 '19 at 17:10

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.