Opencart home page display

I'm trying to split my opencart url list into 20 pages, but it still shows the entire list on page 1 and page 2. Please take a look at the codes, I must be doing something wrong.

The page display looks great

1 2 → |
Showing 1 to 20 of 31 (2 Pages)

The codes on the controller page are address.php

at protected function getList()

(I also tried public function index()

).

if (isset($this->request->get['page'])) {
     $page = $this->request->get['page'];
  } else {
     $page = 1;
  }      

  $data = array(              
     'start' => ($page - 1) * 20,
     'limit' => 20
  );

  $address_total = $this->model_account_address->getTotalAddresses($data);
  $results = $this->model_account_address->getAddresses();

  $pagination = new Pagination();
  $pagination->total = $address_total;
  $pagination->page = $page;
  $pagination->limit = 20; 
  $pagination->text = $this->language->get('text_pagination');
  $pagination->url = $this->url->link('account/address', 'page={page}', 'SSL');

  $this->data['pagination'] = $pagination->render();

      

and codes in model account address.php

public function getDetails($data = array()) {
  $sql = $this->db->query("SELECT * FROM `" . DB_PREFIX . "address` WHERE customer_id = '" . (int)$this->customer->getId() . "'");

  if (isset($data['start']) || isset($data['limit'])) {
     if ($data['start'] < 0) {
        $data['start'] = 0;
     }         

     if ($data['limit'] < 1) {
        $data['limit'] = 20;
     }   

     $sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
  }

  $query = $this->db->query($sql);

  $details = $query->rows;

return $details;
}   

      

This is displayed address_list.tpl

<?php echo $pagination; ?>

      

Please advise.

+3


source to share


1 answer


The problem is in your model. You will run a query before filling in LIMIT

and OFFSET

change your model to this:

public function getDetails($data = array()) {
    $sql = 'SELECT * FROM `' . DB_PREFIX . 'address` WHERE customer_id = ' . (int)$this->customer->getId();

    if (isset($data['start']) || isset($data['limit'])) {
        if ($data['start'] < 0) {
            $data['start'] = 0;
        }

        if ($data['limit'] < 1) {
            $data['limit'] = 20;
        }

        $sql .= ' LIMIT ' . (int)$data['start'] . ',' . (int)$data['limit'];
    }

    $query = $this->db->query($sql);

    return $query->rows;
}

      

This should do it.

In fact, after your first call $sql = $this->db->query("...");

(first line in the model method), the variable $sql

does not contain the SQL query, but the OpenCart DB resource object containing the properties row

(first line), rows

(all rows fetched) and num_rows

(number of rows fetched). So your second call $query = $this->db->query($sql);

should give you an error - if it doesn't show up, you may have hidden PHP errors. Check your PHP error log (or OpenCart error log) to find out.

EDIT



Then in your controller you have this logic error: you set LIMIT

and OFFSET

for getTotalAddresses($data)

, which should read ALL addresses, but this will always return a number . On the other hand, the method that needs to receive these parameters does not receive them: 20

getAddresses();

So fix your controller to do this:

$address_total = $this->model_account_address->getTotalAddresses();
// no $data here as we want all the addresses to be counted ----^^
$results = $this->model_account_address->getAddresses($data);
// we pass $data here so the LIMIT is applied --------^^^^^

      

And one more thing: in your controller you are calling , but in your model it is defined ... Are you sure you don't want to rename to ? getAddresses

getDetails

getDetails

getAddresses

+3


source







All Articles