0
  1. We have created a form using Magento CMS page. As you know, Form has many fields like Name, Email, etc. In our form, we have an input field called School.

  2. We have a list of schools in the database.

Now, we want to load the school database records into the Form(CMS page). How can I do that?

Could anyone please help me throughout this?

Thanks in advance. Hope I will get a solution for this.

Siva
  • 1,120
  • 1
  • 14
  • 33

1 Answers1

1

Below two way available for get data

1) I Assume you have created Model and Collection file associated with that tables.

2) In a Block PHP file constructor add one argument (Dependency Injection) like below and store it in a class member variable.

 public function __construct(
    Context $context,
    \Namespace\Modulename\Model\ModelNameFactory $modelNameFactory,

    array $data = array()
) {
    $this->_modelFactory = $modelFactory;
    parent::__construct($context, $data);
}

3) Prepare a public method in your block to access collection like below.

public function getCollection(){

    return $this->_modelFactory->create()->getCollection();

}

4) Loop through each of the collection result.

or You can directly get custom table using objectmanager concept,

$objectManager =   \Magento\Framework\App\ObjectManager::getInstance();
    $connection = $objectManager->get('Magento\Framework\App\ResourceConnection')->getConnection('\Magento\Framework\App\ResourceConnection::DEFAULT_CONNECTION'); 
    $result1 = $connection->fetchAll("SELECT * FROM tablename");

echo "<pre>";print_r($result1);

Note: Don't use Object Manager instance directly check this for more details: Link

Hope, this will help you.

Rakesh Donga
  • 5,344
  • 2
  • 24
  • 57
  • Many thanks for your information. Certainly, I will check as per the steps you mentioned. For your info: I didn't create the Model and Collection file yet. Can you please point me out how can I do it? – Siva Jan 31 '19 at 08:28
  • @Magentovsmarttec ref link : http://bilalusean.com/create-read-update-delete-crud-magento-2/ – Rakesh Donga Jan 31 '19 at 08:30