如何通过get方法使用limit和offset创建过滤器api?

时间:2017-07-11 06:46:32

标签: opencart

class ControllerSystemFilter extends Controller {
    public function getfilter() {
        $results = array();
        $query = $this->db->query("SELECT *, (SELECT name FROM " . DB_PREFIX . "filter_group_description fgd WHERE fd.filter_group_id = fgd.filter_group_id AND fgd.language_id = '" . (int)$this->config->get('config_language_id') . "') AS `group` FROM " . DB_PREFIX . "filter_group_description fgd  LEFT JOIN " . DB_PREFIX . "filter_description fd ON (fgd.filter_group_id = fd.filter_group_id) WHERE  fd.language_id ='" . (int)$this->config->get('config_language_id')."' LIMIT 10 OFFSET 15");
        $results =$query->rows;

        if(count($results )){
            $json['success']    = true;
            $json['filters']    = $results ;
        } else {
            $json['success']    = false;
        }

        if ($this->debugIt) {
            echo '<pre>';
            print_r($json);
            echo '</pre>';
        } else {
            $this->response->setOutput(json_encode($json));
        }
    }
}

我正在开发opencart,在这段代码中,我尝试使用限制和偏移通过get方法编写过滤器api的代码,但是当我运行此代码时,它向我显示输出成功:false。 如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

如上所述,$ json的值为viewWillAppear(_),因为数组为空。这是因为success:false不是获取行数据的有效代码。要解决此问题,请使用以下代码:

$query->rows

上面的代码将获取行数据并将其存储到数组$query = $this->db->query("SELECT *, (SELECT name FROM " . DB_PREFIX . "filter_group_description fgd WHERE fd.filter_group_id = fgd.filter_group_id AND fgd.language_id = '" . (int)$this->config->get('config_language_id') . "') AS `group` FROM " . DB_PREFIX . "filter_group_description fgd LEFT JOIN " . DB_PREFIX . "filter_description fd ON (fgd.filter_group_id = fd.filter_group_id) WHERE fd.language_id ='" . (int)$this->config->get('config_language_id')."' LIMIT 10 OFFSET 15"); $results= array(); foreach($query->rows as $row){ $results[] = $row; }

请注意,检查数组时,请使用$results代替empty,如此

count