Post by gerstavros » Thu Nov 11, 2021 1:44 am

Hello
I can't find any opencart documentation and I m trying blind to create a module with connection to DB.
On admin side, i ve created an ajax button which i want call a function from model file which created the DB table, but can't figure out how to do it
Tried:

Code: Select all

<button onclick="install()">Create tables in DB</button>
...
<script type = "text/javascript">
    function install() {
    $.ajax( { type : 'POST',
              data : { },
              url  : 'index.php?route=mymodule/form/install&user_token={{ user_token }}',              
              success: function() {
              },
              error: function() {
              }
            });
    }
  </script>
Model:

Code: Select all

	public function install() {
		$this->db->query("CREATE TABLE `" . DB_PREFIX . "_mymoduleitems` (
            `req_id` int(11) NOT NULL AUTO_INCREMENT,
            `status` tinyint(1) NOT NULL,
            `name` varchar(64) NOT NULL,
            `item0` varchar(32) NOT NULL,
            `item1` text,
            `date_added` datetime NOT NULL,
            PRIMARY KEY (`req_id`)
		) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;");
		
	}
But how to connect them from the controller?

New member

Posts

Joined
Mon Apr 04, 2016 12:44 am

Post by cmsroom » Thu Nov 11, 2021 4:58 am

Create a controller index.php?route=mymodule/form/install here with the install function . Load and call your model function in it.

You may like these extension : - https://www.opencart.com/index.php?rout ... r=cmsrooms


New member

Posts

Joined
Fri Sep 28, 2018 1:37 am
Who is online

Users browsing this forum: No registered users and 6 guests