I want to create a new link/page in the backoffice left side menu. So i figuered i will create a controller.
controllers/admin/AdminPageController.php:
<?php
class AdminPageController extends AdminController
{
public function initContent()
{
parent::initContent();
$smarty = $this->context->smarty;
$smarty->assign('testpage', 'testpage');
}
}
?>
admin\themes\default\template\controllers\page\content.tpl
$con=mysqli_connect("localhost","root","password","prestashop");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM ps_customer");
echo "<table border='1'>
<tr>
<th>company</th>
<th>email</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['company'] . "</td>";
echo "<td>" . $row['email'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
At BackOffice -> Administration -> Menus -> i created the menu to my controller with Home as parent.
according to this, it should show the company & email column from the ps_customer table.
However, when i open that test page, i see this:
company email "; while($row = mysqli_fetch_array($result)) { echo ""; echo "" . $row['company'] . ""; echo "" . $row['email'] . ""; echo ""; } echo ""; mysqli_close($con); ?>
How come it's not display the table? what am i doing wrong?
Thanx
1) To Create a menu at backend: You can achieve this, by creating a tab object in the install function of your main file of your module.
eg:
public function install()
{
// Install Tabs
$parent_tab = new Tab();
$parent_tab->name[$this->context->language->id] = $this->l('Main Tab Example'); //Just put the name in the actual language
$parent_tab->class_name = 'AdminMainExample'; //Set the class name Your controller
$parent_tab->id_parent = 0; // Home tab
$parent_tab->module = $this->name; //Set the module from the menu
$parent_tab->add(); //Add the tab to the database
....
}
Similarly you have to delete the tab in you uninstall method
public function uninstall()
{
// Uninstall Tabs
$tab = new Tab((int)Tab::getIdFromClassName('AdminMainExample'));
$tab->delete();
...
}
2) To display the list at backend: You cannot use php code in tpl file directly as you did in your content.tpl. To load the customer list, You can use the helpers within your controller and have to mention table_name and class_name in the contructor of your controller (AdminPageController). Mention the fields in the method render_list and the helpers will do the rest.
Take a look at the example1 mentioned in the link prestashop document