2013-10-21 22 views
0

所以我想設置一個類似MVCL的網站使用Opencart,但是一旦我看到了我的過濾列表,我選擇索引和結果我希望檢索,但它只是結果顯示一切。結果頁面沒有顯示過濾的項目opencart

它適用於其他種類,如按客戶分類。

而不是顯示大寫和代碼量我會解釋我認爲可能是問題的原因。

在顯示過濾列表之前,用戶會在名爲套件的索引內顯示該項目的一組選項。

這個指數然後循環,用戶將選擇他們選擇的選項(比如說豐田可能導致所有的豐田汽車),但是它會顯示所有沒有應用過濾器的產品。

型號:

<?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; 
     } 
    } 

} 
?> 

控制器:

$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); 

回答

0

好吧,我最後想到了它。看起來沒有數據顯示的原因是因爲過濾器沒有工作。 爲了改變這一點,我不得不在product.php中創建一個新的函數來過濾數據並顯示產品。

1

if (!isset($this->data['kit'][$key])) { 
    $this->data['categories'][$key]['kit'] = $key; 
} 

是沒用的,因爲$this->data['categories'][$key]['kit'] = $key;總是被這個

$this->data['categories'][$key]['kit'] = array(
    'name' => $result['kit'], 
    'href' => $this->url->link('product/kit/info', 'kit_id=' . $key) 
); 
覆蓋

$this->data['kit'][$key]設置在哪裏?

和你的模型很容易受到SQL注入

$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "product_custom WHERE kit='" . $kit_id . "'"); 

應該轉變成

$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "product_custom WHERE kit='" . (int)$kit_id . "'"); 

$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "product_custom WHERE kit='" . $this->db->escape($kit_id) . "'"); 
+0

到目前爲止,謝謝你,我甚至沒有意識到這個漏洞。我在原始代碼中增加了一些 –

+0

是否有任何理由說明爲什麼我得到正確的索引,但是一旦我選擇了它,查詢結果就是檢索所有的 –

+0

發佈負責加載結果的代碼後,您選擇在選擇框中設置一個具體套件...以及如何處理模板中的$ this-> data ['categories'] [$ key] ['kit']'? – shadyyx

0

它很難看到你正在嘗試做,但我猜你在你的MySQL中有一個名爲product_c的表ustom,並且在表中有一個名爲「kit」的列...然後您試圖從該表中返回所有具有「kit」值的行?

如果是的話,我會嘗試以下

目前它說以下內容:

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) 
     ); 
    } 

而且你可以將其更改爲以下內容:

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) 
      ); 
     } 
    } 

至少這將意味着查詢結果只會添加到數組中,如果該特定行具有「套件」的值

正如我所說,很難說沒有完全看到你正在嘗試做的,知道你的表格佈局等

希望這有助於

問候

傑里米,

相關問題