I have created a custom module and there are some drop downs in it. I have done all the things in .phtml file as pure php.

How can I change that to correct way code in my .phtml file:

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

$tableColor = $resource->getTableName('color_price');

$sql = "SELECT * FROM " . $tableColor;
$color = $connection->fetchAll($sql);

drop down like below its in .phtml file too:

<p><br>Frame Color</p>
<select id="color">
foreach($color as $c){ ?>
    <option value="<?php print_r($c['price_component']);?>"><?php print_r($c['color']);?></option>

<b><p id="framecolor"></p></b>

below code is my layout.xml file

<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
        <referenceContainer name="content">
            <block class="Test2\Helloworld\Block\Helloworld" 
            name="helloworld" template="helloworld.phtml" />

what is the correct way of doing this. now all the things in .phtml file

  • Could you please share your layout xml ? – Pankaj Pareek Sep 13 '17 at 11:47
  • @Pankaj Pareek Can u look the edited question – Hemal Sep 14 '17 at 4:13

In controller file,

$om = \Magento\Framework\App\ObjectManager::getInstance();
$persistor = $om->get('Magento\Framework\App\Request\DataPersistorInterface');
$data = array('test1','test2');
<!-- set data in persistor object -->
$persistor->set('coloroption', $data);

In template file,

<!-- get data in persistor object -->  

  $om = \Magento\Framework\App\ObjectManager::getInstance();
  $persistor = $om->get('Magento\Framework\App\Request\DataPersistorInterface');
  $colorOption =     $persistor->get('coloroption');
  • Please let me know if you have issue – Rakesh Jesadiya Sep 14 '17 at 7:23
  • if u can please tell me how ur code working i dont understand how it works. how can i connect ur code with mine im new to magento if u can please help me – Hemal Sep 14 '17 at 8:08

Try this may be it help you :

  • wher shoud i put those things – Hemal Sep 14 '17 at 5:06
  • In your controller , Also you can manipulate syntax as per your need . – Shashank Gupta Sep 14 '17 at 8:05

In your module block class Test2\Helloworld\Block\Helloworld add the code as below:

namespace Test2\Helloworld\Block;
use Magento\Framework\ObjectManagerInterface;

class Helloworld extends \Magento\Framework\View\Element\Template {
     * @var ObjectManagerInterface
    private $objectManager;

     * Constructor.
public function __construct(
    \Magento\Framework\View\Element\Template\Context $context,
    \Magento\Framework\ObjectManagerInterface $objectManager,
    array $data = []
) {

    $this->objectManager = $objectManager;
    parent::__construct($context, $data);

    public function getFrameColorOption() {
        $resource = $this->objectManager->get('Magento\Framework\App\ResourceConnection');
        $connection = $resource->getConnection();
        $tableColor = $resource->getTableName('color_price');
        $sql = "SELECT * FROM " . $tableColor;
        $color = $connection->fetchAll($sql);
        return $color;

In your template file, You can call the function as below:

$color = $block->getFrameColorOption();

PS: We should not use ObjectManager directly. If you have created Model for color_price table then you should use Model instead of ObjectManager

  • I try that thing now but it will give a Fatal error. can u tell me how to solve that, – Hemal Sep 14 '17 at 6:48
  • I have updated my code. – Pankaj Pareek Sep 14 '17 at 6:53
  • so is that ok if i write query in block, dont i need to write those things in controller – Hemal Sep 14 '17 at 8:06
  • Yes template must take data from block instead of controller – Pankaj Pareek Sep 14 '17 at 8:14

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.