2012-03-28 123 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) . "'"); 
    } 
} 

希望这有助于...