2012-03-28 137 views
2

我需要在Opencart中的Opencart產品插入/添加表單中將自定義文件輸入字段添加到其自己的選項卡中,以將csv文件上載到mysql數據庫。我已經將tab/field添加到視圖文件中,更新了語言文件,但我不確定我需要在控制器中完全做什麼,並且爲了將數據從上傳的csv傳遞到數據庫表中而建模。向產品插入表單添加新的輸入字段Opencart

product_pins表:

pin_id (AI) | pin_product_id | pin_pin_number 

CSV文件中的數據(例如):

342353535345 
345345346346 
235434534634 

我在哪裏至今:

控制器admin/controller/catalog/product.php(約行807):

if (isset($this->request->post['product_pins'])) { 
    $this->data['product_pins'] = is_uploaded_file($this->request->post['product_pins']); 
} else { 
    $this->data['product_pins'] = ''; 
} 

型號admin/model/catalog/product.php(大約7號線):

if (isset($this->data['product_pins'])) { 
    $handle = fopen($this->data['product_pins'], "r"); 
    while (($pins = fgetcsv($handle, 1000, ",")) !== false) { 
     foreach ($pins as $pin) { 
      $this->db->query("INSERT INTO " . DB_PREFIX . "product_pins SET pin_product_id = '" . (int)$product_id . "', pin_pin_number = '" . $this->db->escape($pin) . "'"); 
     } 
    } 
    fclose($handle); 
} 

我希望得到任何幫助。

+0

嗨,你能發佈一個鏈接或至少一個生成的html的pastebin,我的猜測是這是簡單的,因爲opencart字段名沒有任何主要的技巧。 – Jonathan 2012-03-28 13:36:17

+0

您是否收到錯誤?當你嘗試輸入時會發生什麼? – Cleverbot 2012-07-06 21:29:08

回答

1

首先 - CSV處理部分應該在控制器內,而不是在模型類中。模型(在說到正確的MVC時)應該只檢索或設置數據並將它們傳遞給控制器​​或從控制器傳遞 - 然後應該操作和控制它們並轉發或從前端視圖(模板)中獲取數據。其次:在OpenCart中提交的文件出現在$this->request->files陣列中。

最後:方法is_uploaded_file()返回boolean值,因此我不知道如何解析boolean並從中創建文件句柄。

所以,讓我們來看看它...嘗試下面的代碼。

控制器:

if (is_uploaded_file($this->request->files['product_pins']['tmp_name'])) { 
    $handle = fopen($this->request->files['product_pins']['tmp_name'], "r"); 
    while (($pins = fgetcsv($handle, 50, ",")) !== false) { // If we know there is only a 10 chars PIN per line it is better to lower the expected line length to lower the memory consumption... 
     $this->data['product_pins'][] = $pins; // there is only one PIN per line 
    } 
    fclose($handle); 
} else { 
    $this->data['product_pins'] = array(); 
} 

現在你(應該)都從CSV文件中的識別碼加入到$this->data['product_pins']陣列 - 假設您然後傳遞$this->data這個模型,它應該包含這樣的代碼:

型號:

if (!empty($this->data['product_pins'])) { 
    foreach ($this->data['product_pins'] as $pin) { 
     $this->db->query("INSERT INTO " . DB_PREFIX . "product_pins SET pin_product_id = '" . (int)$product_id . "', pin_pin_number = '" . $this->db->escape($pin) . "'"); 
    } 
} 

希望這有助於...