我需要在Opencart产品插入/添加表单的Opencart中添加一个自定义文件输入字段到它自己的选项卡中,以便将csv文件上传到mysql数据库。我已经将选项卡/字段添加到视图文件中,更新了语言文件,但不确定在控制器和模型中需要做什么才能将上传的csv数据传递到数据库表中。
product_pins表:
pin_id (AI) | pin_product_id | pin_pin_numbercsv文件数据(示例):
342353535345
345345346346
235434534634我现在所处的位置:
Controller 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'] = '';
}Model 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);
}我很感谢你的帮助。
发布于 2013-04-05 10:21:20
首先,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文件中的所有PIN添加到$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) . "'");
}
}希望这能帮上忙。
https://stackoverflow.com/questions/9907145
复制相似问题