Opencart表列无法排序

时间:2019-06-13 07:24:54

标签: php opencart-3

我最近尝试过opencart,所以我是新手。我设法获取数据并将其显示在html表中,但是当我单击列时,我被困在表的排序中。地址栏中的url确实正确显示了路由,指示了数据的ASC和DESC顺序,但是数据中的记录根本没有排序。我在哪里做错了?可能是我的模型还是控制器?谢谢

控制器:

<?php

    class ControllerPingPing extends Controller {
        private $error = array();

        public function index() {

            $this->load->language('ping/ping');
            $this->document->setTitle($this->language->get('heading_title'));
            $this->load->model('ping/ping');

            if (isset($this->request->get['sort'])) {
                $sort = $this->request->get['sort'];
            } else {
                $sort = 'e.event_id';
            }
            if (isset($this->request->get['order'])) {
                $order = $this->request->get['order'];
            } else {
                $order = 'ASC';
            }

            $url = '';
            if (isset($this->request->get['sort'])) {
                $url .= '&sort=' . $this->request->get['sort'];
            }
            if (isset($this->request->get['order'])) {
                $url .= '&order=' . $this->request->get['order'];
            }

            //getting all event records
            $filter_data = array();
            $results = $this->model_ping_ping->getEvents($filter_data);
            $url = '';
            foreach ($results as $result) {
                //display event details
                $data['events'][] = array(
                    'event_id'      => $result['event_id'],
                    'code'          => $result['code'],
                    'status'        => $result['status']
                );
            }
            // echo "<pre>";
            // print_r($data['events']);
            // echo "</pre>";
            $data['breadcrumbs'] = array();

            $data['breadcrumbs'][] = array(
                'text' => $this->language->get('text_home'),
                'href' => $this->url->link('common/dashboard', 'user_token=' . $this->session->data['user_token'], true)
            );

            $data['breadcrumbs'][] = array(
                'text' => $this->language->get('heading_title'),
                'href' => $this->url->link('ping/ping', 'user_token=' . $this->session->data['user_token'] . $url, true)
            );

            if ($order == 'ASC') {
                $url .= '&order=DESC';
            } else {
                $url .= '&order=ASC';
            }

            $data['sort_id'] = $this->url->link('ping/ping', 'user_token=' . $this->session->data['user_token'] . '&sort=e.event_id' . $url, true);
            $data['sort_code'] = $this->url->link('ping/ping', 'user_token=' . $this->session->data['user_token'] . '&sort=e.code' . $url, true);

            $url = '';
            if (isset($this->request->get['sort'])) {
                $url .= '&sort=' . $this->request->get['sort'];
            }
            if (isset($this->request->get['order'])) {
                $url .= '&order=' . $this->request->get['order'];
            }

            $data['header'] = $this->load->controller('common/header');
            $data['column_left'] = $this->load->controller('common/column_left');
            $data['footer'] = $this->load->controller('common/footer');

            $data['sort'] = $sort;
            $data['order'] = $order;

            $this->response->setOutput($this->load->view('common/pingTest',$data));
        }
    }

型号:

   <?php
    class ModelPingPing extends Model {
        public function getEvents($data = array()) {
            $sql = "SELECT * FROM " . DB_PREFIX . "event e";
            $query = $this->db->query($sql);
            return $query->rows;

            // if (!empty($data['filter_name'])) {
            //  $sql .= " AND e.event_id LIKE '" . $this->db->escape($data['filter_name']) . "%'";
            // }

            $sort_data = array(
                'e.event_id',
                'e.code'
            );

            if (isset($data['sort']) && in_array($data['sort'], $sort_data)) {
                $sql .= " ORDER BY " . $data['sort'];
            } else {
                $sql .= " ORDER BY e.event_id";
            }

            if (isset($data['order']) && ($data['order'] == 'DESC')) {
                $sql .= " DESC";
            } else {
                $sql .= " ASC";
        }
    }
}
?>

树枝文件:

{{ header }}{{ column_left }}
<div id="content">
    <div class="page-header">
        <div class="container-fluid">
            <h1>{{ heading_title }}</h1>
            <ul class="breadcrumb">
            {% for breadcrumb in breadcrumbs %}
                <li><a href="{{ breadcrumb.href }}">{{ breadcrumb.text }}</a></li>
            {% endfor %}
            </ul>
        </div>
    </div>

    <div class="container-fluid">
        <div class="panel panel-default">
            <div class="panel-heading">
                <h3 class="panel-title"><i class="fa fa-list"></i> {{ text_title }}</h3>
            </div>
            <div class="panel-body">
                <form action="{{ delete }}" method="post" enctype="multipart/form-data" id="form-download">
                    <div class="table-responsive">
                        <table class="table table-bordered table-hover">
                            <thead>
                                <tr>

                                  <td class="text-left">{% if sort == 'e.event_id' %}
                                    <a href="{{ sort_id }}" class="{{ order|lower }}">Event ID</a>
                                    {% else %}
                                    <a href="{{ sort_id }}">Event ID</a>
                                    {% endif %}</td>
                                  <td class="text-right">{% if sort == 'e.code' %}
                                    <a href="{{ sort_code }}" class="{{ order|lower }}">Event Code</a>
                                    {% else %}
                                    <a href="{{ sort_code }}">Event Code</a>
                                    {% endif %}</td>  

                                  <td class="text-right">Event Status</td>

                                </tr>
                            </thead>
                            <tbody>
                            {% for event in events %}
                                {% if event.status == 0 or event.status == 1 %}
                                <tr>

                                    <td class="text-left">{{ event.event_id }}</td>
                                    <td class="text-right">{{ event.code }}</td>
                                    <td class="text-right">
                                    {% if event.status == 0 %}
                                        <div class="aa-tag for-sale">Coomming Soon</div>
                                    {% endif %}
                                    {% if event.status == 1 %}
                                        <div class="aa-tag sold-out">Ongoing</div>
                                    {% endif %}
                                    </td>

                                </tr>    
                            </tbody>
                                {% endif %}
                            {% endfor %}
                        </table>
                    </div>
                </form>
                <div class="row">
                    <div class="col-sm-6 text-left">{{ pagination }}</div>
                    <div class="col-sm-6 text-right">{{ results }}</div>
                </div>
            </div>
        </div>
    </div>
</div>

1 个答案:

答案 0 :(得分:3)

我已经找到答案,我需要在控制器中添加以下几行代码:

$filter_data = array(
    'sort' => $sort,
    'order' => $order
);