Skip to content

Instantly share code, notes, and snippets.

@softsolution
Created February 11, 2016 07:52
Show Gist options
  • Save softsolution/6262cb8bf3ca9851a49d to your computer and use it in GitHub Desktop.
Save softsolution/6262cb8bf3ca9851a49d to your computer and use it in GitHub Desktop.
<?php
ini_set('max_execution_time', 0);
//ini_set('memory_limit', '128M');
$file = $_SERVER['DOCUMENT_ROOT'].'/import.csv';
$csv_file = @fopen($file, 'r');
if (!$csv_file) { $error = 'Ошибка открытия файла. Код: 003'; }
$encoding = 'UTF-8';
$separator = ';';
$quote = 'quot';
$quote = ($quote=='quot' ? '"' : "'");
$rows_start = 1;
$rows_count = 0;
$data_struct = 'href,description,urlofilials,urlbankomats';
$current_row = 0;
$rows_loaded = 0;
if ($csv_file){
$data_struct = explode(',', $data_struct);
foreach($data_struct as $key=>$val){ $data_struct[$key] = trim($val); }
//Импорт объектов в цикле
while(!feof($csv_file)){
//увеличим номер текущей строки
$current_row++;
//читаем строку
$row = fgets($csv_file); if (!$row) { continue; }
//если не достигли начала, пропускаем импорт и в начало
if ($current_row < $rows_start){ continue; }
//увеличим счетчик строк
$rows_loaded++;
//проверяем превышение лимита
if ($rows_loaded > $rows_count && $rows_count > 0) { break; }
//конвертим кодировку
if ($encoding != 'UTF-8') { $row = iconv($encoding, 'UTF-8', $row); }
$row_struct = array();
if ($separator == 't') { $separator = "\t"; }
//разбиваем строку на поля
$row_struct = explode($separator, $row);
//очищаем поля от кавычек
foreach($row_struct as $key=>$val){
$val = trim($val);
$val = ltrim($val, $quote);
$val = rtrim($val, $quote);
$row_struct[$key] = $val;
}
$item = array();
foreach($data_struct as $num=>$field){
//пропускаем не нужные колонки
if ($field == '---') { continue; }
$item[$field] = $row_struct[$num];
}
if ($item) {
$items[] = $item;
}
}//while
//@unlink($file);
if($items){
$importcnt = 0;
foreach($items as $item){
$citybank_id = $inDB->get_field('cms_content_citybanks', "href='".$item['href']."'", 'id');
if($citybank_id){
$importcnt++;
$item['description'] = $inDB->escape_string($item['description']);
$inDB->update('cms_content_citybanks', array('description' => $item['description']), $citybank_id);
} else {
continue;
}
}
}
}//if csv file
echo 'Обработка завершена. ';
if($importcnt){
echo 'Имортировано объектов - '.$importcnt;
} else {
echo 'Ничего не импортировано';
}
@softsolution
Copy link
Author

` ini_set('max_execution_time', 0);
//ini_set('memory_limit', '128M');
$file = $_SERVER['DOCUMENT_ROOT'].'/all_links_wq.csv';
$csv_file = @fopen($file, 'r');
if (!$csv_file) { $error = 'Ошибка открытия файла. Код: 003'; }
$encoding = 'UTF-8';
$separator = ';';
$quote = 'quot';
$quote = ($quote=='quot' ? '"' : "'");
$rows_start = 1;
$rows_count = 0;
$data_struct = 'href,officeurl,bankomatyurl';
$current_row = 0;
$rows_loaded = 0;
if ($csv_file){
$data_struct = explode(',', $data_struct);
foreach($data_struct as $key=>$val){ $data_struct[$key] = trim($val); }
//Импорт объектов в цикле
while(!feof($csv_file)){
//увеличим номер текущей строки
$current_row++;
//читаем строку
$row = fgets($csv_file); if (!$row) { continue; }
//если не достигли начала, пропускаем импорт и в начало
if ($current_row < $rows_start){ continue; }
//увеличим счетчик строк
$rows_loaded++;
//проверяем превышение лимита
if ($rows_loaded > $rows_count && $rows_count > 0) { break; }
//конвертим кодировку
if ($encoding != 'UTF-8') { $row = iconv($encoding, 'UTF-8', $row); }
$row_struct = array();
if ($separator == 't') { $separator = "\t"; }
//разбиваем строку на поля
$row_struct = explode($separator, $row);

        //очищаем поля от кавычек
        foreach($row_struct as $key=>$val){
            $val = trim($val);
            $val = ltrim($val, $quote);
            $val = rtrim($val, $quote);
            $row_struct[$key] = $val;
        }
        $item = array();
        foreach($data_struct as $num=>$field){
            //пропускаем не нужные колонки
            if ($field == '---') { continue; }
            $item[$field] = $row_struct[$num];
        }
        if ($item) {
            $items[] = $item;
        }
    }//while

    //update cms_content_citybanks table
    if($items){
        $importcnt = 0;
        foreach($items as $item){
            $citybank_id = $inDB->get_field('cms_content_citybanks', "href='".$item['href']."'", 'id');
            if($citybank_id){
                $importcnt++;
                $update = array();
                if($item['officeurl']){
                    $update['officeurl'] = $inDB->escape_string($item['officeurl']);
                }
                if($item['bankomatyurl']){
                    $update['bankomatyurl'] = $inDB->escape_string($item['bankomatyurl']);
                }
                $inDB->update('cms_content_citybanks', $update, $citybank_id);
            } else {
                continue;
            }
        }
    }
}//if csv file

echo 'Обработка завершена. ';
if($importcnt){
    echo 'Обновлено объектов - '.$importcnt;
} else {
    echo 'Ничего не обновлено';
}`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment