如何使用if else条件导入php mysql数据库中的csv数据

时间:2015-09-10 05:57:06

标签: php mysql codeigniter csv

我需要在数据库中导入csv数据,其中我的Product表有两列代码和价格。我正在使用此脚本导入数据,该脚本查找产品代码,然后更新产品价格 -

function update_price()
{
    $this->sma->checkPermissions('csv');
    $this->load->helper('security');
    $this->form_validation->set_rules('userfile', lang("upload_file"), 'xss_clean');

    if ($this->form_validation->run() == true) {



        if (isset($_FILES["userfile"])) {

            $this->load->library('upload');

            $config['upload_path'] = $this->digital_upload_path;
            $config['allowed_types'] = 'csv';
            $config['max_size'] = $this->allowed_file_size;
            $config['overwrite'] = TRUE;

            $this->upload->initialize($config);

            if (!$this->upload->do_upload()) {

                $error = $this->upload->display_errors();
                $this->session->set_flashdata('error', $error);
                redirect("products/update_price");
            }

            $csv = $this->upload->file_name;

            $arrResult = array();
            $handle = fopen($this->digital_upload_path . $csv, "r");
            if ($handle) {
                while (($row = fgetcsv($handle, 1000, ",")) !== FALSE) {
                    $arrResult[] = $row;
                }
                fclose($handle);
            }
            $titles = array_shift($arrResult);

            $keys = array('code', 'price');

            $final = array();

            foreach ($arrResult as $key => $value) {
                $final[] = array_combine($keys, $value);
            }
            $rw = 2;
            foreach ($final as $csv_pr) {
                if (!$this->products_model->getProductByCode(trim($csv_pr['code']))) {
                    $this->session->set_flashdata('message', lang("check_product_code") . " (" . $csv_pr['code'] . "). " . lang("code_x_exist") . " " . lang("line_no") . " " . $rw);
                    redirect("product/update_price");
                }
                $rw++;
            }
        }

    }

    if ($this->form_validation->run() == true && !empty($final)) {
        $this->products_model->updatePrice($final);
        $this->session->set_flashdata('message', lang("price_updated"));
        redirect('products');
    } else {

        $this->data['error'] = (validation_errors() ? validation_errors() : $this->session->flashdata('error'));

        $this->data['userfile'] = array('name' => 'userfile',
            'id' => 'userfile',
            'type' => 'text',
            'value' => $this->form_validation->set_value('userfile')
        );

        $bc = array(array('link' => base_url(), 'page' => lang('home')), array('link' => site_url('products'), 'page' => lang('products')), array('link' => '#', 'page' => lang('update_price_csv')));
        $meta = array('page_title' => lang('update_price_csv'), 'bc' => $bc);
        $this->page_construct('products/update_price', $meta, $this->data);

    }
}

但是这里我们的产品价格更新并更换旧价值,但我想检查价格的旧价值和 如果旧值大于上传值,则不替换和 如果旧值低于上载的价格值,则更新/替换该值。 意味着在所有条件下我们的价格总是最高的。 我们怎么做???有人请帮忙..

0 个答案:

没有答案