如何从excel中删除重复值并尝试在Codeigniter中插入数据库?

时间:2015-03-05 07:07:24

标签: php mysql codeigniter

亲爱的朋友我使用以下功能在Codeigniter中创建导入功能但是当我从Excel中选择循环时,我不知道如何在我的数据上发生重复值时逃脱 主键仅用于 $ in_val [' in_no'] ,其他值可以自由插入。 如何逃避重复值并尝试将其他行从excel插入数据库?

这是我的功能

public function imported($file_paths) {
        $in_val = array();
        $i = 0;
        $cell_array = $this->csvimport->get_array($file_paths);
        foreach ($cell_array as $row) {
            if ($i === 7000)break; {
                $in_val['user_id'] = 70;
                $in_val['in_no'] = (int) preg_replace("/(\D+\d{4})0*([1-9]+\d*)/i", "$2\n", trim($row['IN_number']));
                $in_val['fi_no'] = trim($row['Fir_obtic']);
                $in_val['paid_by'] = trim($row['Paid_by']);
                $in_val['in_per'] = str_replace('/', '-', trim($row['In_date']));
                $in_val['sale_by'] = trim($row['Sale_by']);
                $in_val['billed'] = trim($row['Billed']);
                $in_val['in_date'] = str_replace('/', '-', trim($row['In_date']));
                $in_val['top_up_num'] = trim($row['T_P_number']);
                $in_val['ffrom'] = trim(str_replace('/', '-', $row['From']));
                $in_val['fto'] = str_replace('/', '-', trim($row['to']));
                $in_val['pay_du_date'] = str_replace('/', '-', trim($row['Pay_due_date']));
                $in_val['bill_amount'] = trim(str_replace('/', '-', $row['Bill_Amount']));
                $in_val['bill_to'] = trim($row['Cust_name']); //customer name
                $in_val['receiv_date'] = trim($row['R_C_date']);
                $in_val['adds'] = trim($row['Address']);
                $in_val['bill_item'] = str_replace('/', '-', trim($row['Bill_Item']));
                $in_val['descr'] = trim($row['Description']);
                $in_val['in_type'] = strtolower(preg_replace("/(\D+\d{0})0*([0-9]+\d*)/i", "$1\n", trim($row['IN_number'])));
                $in_val['quant'] = trim($row['In_Quanity']);
                $in_val['unit_p'] = trim($row['In_Unit_P']);
                $in_val['amount'] = trim($row['Bill_Amount']);
                $in_val['total'] = trim($row['Total']);
                $in_val['status'] = 1;
                $in_val['amc'] = trim($row['Amount_Description']);
                $pri_val['user_id'] = 70;
                $pri_val['type'] = strtolower(preg_replace("/(\D+\d{4})0*([1-9]+\d*)/i", "$1\n", trim($row['IN_number'])));
                $pri_val['parent'] = 1;
                $pri_val['status'] = 1;
                $pri_val['in_no'] = (int) $in_val['in_no'];
                $pri_val['total'] = (int) trim($row['Total']);
                $pri_val['in_date'] = $in_val['in_date'];
                $this->db->insert('dbfinan_invoice', $in_val);
                $this->db->insert('prifix', $pri_val);
                $i++;
            }
        }
    }

请帮忙

1 个答案:

答案 0 :(得分:0)

尝试添加条件和功能以检查表格如下:

if($this->[yourmodal]->isExist($in_val['in_no'])==false)
{
  $this->db->insert('dbfinan_invoice', $in_val);
  $this->db->insert('prifix', $pri_val);
}

因此表格中的$ in_val [' in_no']可以是唯一的