首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用if else条件导入php mysql数据库中的csv数据

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

Stack Overflow用户
提问于 2015-09-10 13:57:06
回答 1查看 457关注 0票数 1

我需要在数据库中导入csv数据,我的Product表有两列code和price。我正在用这个脚本导入数据,它会找到产品代码,然后更新产品价格-

代码语言:javascript
复制
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);

    }
}

但是在这里我们的产品价格更新并替换旧的值,但我想检查旧的价格值,如果旧的值大于上传的值,则不替换,如果旧的值低于上传的价格值,则更新/替换该值。意思是在任何情况下我们的价格都是最高的。我们该怎么做呢?谁来帮帮忙..。

EN

回答 1

Stack Overflow用户

发布于 2015-09-10 17:14:39

我认为您应该深入了解updatePrice方法,并修改其中的mysql查询。

Mysql有一个合适的Update if语句。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32494176

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档