So I am trying to set up a MVCL-like site using Opencart however once I have seen my filtered list I choose the index and the results I wish to retrieve but instead it just results in showing EVERYTHING.
It works for other sorts such as sort by customer.
Instead of showing reams and reams of code I'll explain what I believe may be the cause of the issue.
Prior to showing the filtered list the user is shown a set of options for the item within an index called kit.
This index is then looped and the user is to select their chosen option(say for example Toyota would lead to all cars that are of the make Toyota) however it instead shows ALL the products without the applied filter.
Model:
<?php
class ModelCatalogKit extends Model {
public function getKit($kit_id) {
$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "product_custom WHERE kit='" . $kit_id . "'");
return $query->row;
}
public function getKits($data = array()) {
if ($data) {
$sql = "SELECT * FROM " . DB_PREFIX . "product_custom group by kit ";
$sort_data = array(
'name',
'sort_order'
);
if (isset($data['sort']) && in_array($data['sort'], $sort_data)) {
$sql .= " ORDER BY " . $data['sort'];
} else {
$sql .= " ORDER BY name";
}
if (isset($data['order']) && ($data['order'] == 'DESC')) {
$sql .= " DESC";
} else {
$sql .= " ASC";
}
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;
} else {
$kit_data = $this->cache->get('kit.' . (int)$this->config->get('config_store_id'));
if (!$kit_data) {
$query = $this->db->query("SELECT distinct kit FROM " . DB_PREFIX . "product_custom ORDER BY kit");
$kit_data = $query->rows;
$this->cache->set('kit.' . (int)$this->config->get('config_store_id'), $kit_data);
}
return $kit_data;
}
}
}
?>
Controller:
$this->data['categories'] = array();
$results = $this->model_catalog_kit->getKits();
foreach ($results as $result) {
$key = $result['kit'];
$this->data['categories'][$key]['kit']= array(
'name' => $result['kit'],
'href' => $this->url->link('product/kit/info', 'kit_id=' . $key)
);
}
$kit_info = $this->model_catalog_kit->getKit($kit_id);
var_dump($kit_info);
Okay I've figured it out in the end. It seems that the reason no data was showing was because the filter was not working. In order to change this I had to create a new function within the product.php that would filter the data and display the products.
This
if (!isset($this->data['kit'][$key])) {
$this->data['categories'][$key]['kit'] = $key;
}
is useless because the $this->data['categories'][$key]['kit'] = $key;
is always overwritten by this
$this->data['categories'][$key]['kit'] = array(
'name' => $result['kit'],
'href' => $this->url->link('product/kit/info', 'kit_id=' . $key)
);
Where is $this->data['kit'][$key]
set?
And Your model is vulnerable to SQL injection:
$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "product_custom WHERE kit='" . $kit_id . "'");
should be transformed into
$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "product_custom WHERE kit='" . (int)$kit_id . "'");
or
$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "product_custom WHERE kit='" . $this->db->escape($kit_id) . "'");
Its hard to see what you are trying to do, but i guess you have a table in your MySQL called product_custom and you have a column in that table called "kit"... and then you are trying to return all the rows from that table that have a value for "kit"?
If so, i would try the following
currently it says the following:
foreach ($results as $result) {
$key = $result['kit'];
$this->data['categories'][$key]['kit']= array(
'name' => $result['kit'],
'href' => $this->url->link('product/kit/info', 'kit_id=' . $key)
);
}
And you could change it to the following:
foreach ($results as $result) {
if (isset($result['kit'])) {
$key = $result['kit'];
$this->data['categories'][$key]['kit']= array(
'name' => $result['kit'],
'href' => $this->url->link('product/kit/info', 'kit_id=' . $key)
);
}
}
At least that would mean that the results of the query would only be added to the array if that particular row had a value for "kit"
As i say, its hard to say without fully seeing what you are trying to do and knowing your table layout etc
Hope this helps,
Regards
Jeremy,