Opencart custom admin area -> queries only showing

2019-08-09 17:46发布

问题:

I am having a problem with some custom pages that I am creating in Opencart for a big school project. More specifically, it seems that the model is not passing any query data to the controller and the view is unable to display the data.

I read this post (How to create a custom admin page in opencart?) and started working from there. The idea is basically to create a portal where the admin can review all orders, all customers, all products and edit the products.

Now, as I am quite new to Opencart and MVC programming in general, I realize there must be better ways to achieve what I want than what I've done here, and I would really appreciate any help and/or advice anyone could offer. Here's my code for the model:

public function verkopenLijst() {

  $query = $this->db->query("SELECT * FROM `shop_order_product`");
    if($query->num_rows > 0){
    $verkopen = array();
       foreach($query->rows as $result){
            $verkopen[] = array(
                'name' => $result['name'],
                'model'      => $result['model'],
                'quantity'      => $result['quantity'],
                'price'     => $result['price'],
                'total'     => $result['total'],
                'tax'     => $result['tax']);
            return $verkopen;
        }
    }
}

public function klantenLijst() {

    //selecteer alles uit de tabel "order_product"
    $query = $this->db->query("SELECT * FROM `shop_customer`");

    //als er rijen bestaan, geef dan deze rijen terug aan de functie
    $klanten = $query->rows;
        if($query->num_rows > 0) {
            foreach($klanten as $rows) {
            return $rows;

            }
    } 
}

public function productenLijst() {
    $query = $this->db->query("SELECT * FROM `shop_product_description`");
    $klanten = $query->rows;
        if($query->num_rows > 0) {
            foreach($klanten as $rows) {
            return $rows;

            }
        } 
}

}

?>

And one of the controllers (the code looks the same for sales, customers and products for now):

class Controllercustomverkopen extends Controller{

    public function Index(){
    //hier roep ik de view "verkopen" aan
    $template="custom/verkopen.tpl"; // .tpl location and file
    //hier roep ik het model aan
    $this->load->model('custom/hoofdpagina');

     $this->template = ''.$template.'';
     $this->response->setOutput($this->render());

    }

    public function verkopenTonen(){
        $this->load->model('custom/hoofdpagina');
        $verkopen = $this->model_custom_hoofdpagina->verkopenLijst();
    }

}

The problem, more specifically, is that the view is not showing any data when I call the controller function that refers to the model.

I tried putting the model functions in the controller (I know this is not good practice but was getting desperate), and when I do that only the first record of the table shows up. However, when I run the queries in phpmyadmin they seem to work fine. I must be doing something (or multiple things) horribly wrong here, could anyone be of assistance?

回答1:

Put return $verkopen; after your foreach.

public function verkopenLijst() {

  $query = $this->db->query("SELECT * FROM `shop_order_product`");
     $verkopen = array();       
     if($query->num_rows > 0){
       foreach($query->rows as $result){
            $verkopen[] = array(
                'name' => $result['name'],
                'model'      => $result['model'],
                'quantity'      => $result['quantity'],
                'price'     => $result['price'],
                'total'     => $result['total'],
                'tax'     => $result['tax']);
        }
    }
    return $verkopen;
}


回答2:

That's because the default method index() is not sending any data to the template. When you access the page for this module, the url would be siteurl**/module/your_module**&token=...

This calls the default method being the index() To call another method you'd use module/your_module/klantenlijst&token=...

Put

$this->data['klantenlijst'] = $this->klantenLijst();

in the index method and you'll be able to use it in the .tpl as $verkopen; (OpenCart uses PHP's extract() method)

In OpenCart 2 you would use the $data array.

edit/addition: You should put the queries in the Model side of MVC and use

$klantenlijst = $this->model_module_your_module->klantenlijst();

in the controller where klantenlijst() is a method inside the Model.