Этот скрипт php имеет 4 функции.
- Функция getHeader() получает заголовок CSV-файла в виде массива.
- getCategories() снова выбирает категории из CSV-файла в виде массива.
- Функция getRecords() получает все записи (исключая заголовок) как двухмерный массив.
<?php
function getHeader()
{
$filename = "past_cms_csv_file.csv";
$contents = file_get_contents($filename);
$records = explode('EOREOR', $contents);
$header = explode(';', $records[0]);
array_splice($header, count($header)-1, 1);
return $header;
}
function getCategories()
{
$header = getHeader();
$index = array_search('v_categories_name_1_4', $header);
$filename = "past_cms_csv_file.csv";
$contents = file_get_contents($filename);
$records = explode('EOREOR', $contents);
$cats = array();
for($i=1;$i<count($records);$i++)
{
$record = $records[$i];
$items = explode('";"', $record);
array_push($cats,$items[$index]);
}
$newCats = array_unique($cats);
$mycats = [];
foreach($newCats as $cat)
{
array_push($mycats, $cat);
}
array_splice($mycats, count($mycats)-1 , 1);
return $mycats;
}
function getRecords()
{
$filename = "past_cms_csv_file.csv";
$contents = file_get_contents($filename);
$records = explode('EOREOR', $contents);
$header = getHeader();
$index = array_search('v_categories_name_1_4', $header);
$cats = getCategories();
for($i=1;$i<count($records);$i++)
{
$record = $records[$i];
$items = explode('";"', $record);
array_push($cats,$items[$index]);
for($j=0;$j<count($items);$j++)
{
$results[$i][$j] = $items[$j];
}
}
return $results;
}
function makecsvfile($filename , $start_index)
{
$header = getHeader();
$results = getRecords();
$active_index = array_search('v_status', $header);
$name_index = array_search('v_products_name_4', $header);
$desc_index = array_search('v_products_description_4', $header);
$meta_title_index = array_search('v_products_meta_title_4', $header);
$meta_keywords_index = array_search('v_products_meta_keywords_4', $header);
$meta_desc_index = array_search('v_products_meta_description_4', $header);
$price_index = array_search('v_products_price', $header);
$quantity_index = array_search('v_products_quantity', $header);
$category_index = array_search('v_categories_name_1_4', $header);
$subcategory_index = array_search('v_categories_name_2_4', $header);
$string = '';
$string .= '"ID";"Active";"Name";"Description";"Meta_title";"Meta_keywords";"Meta_Description";"Categories";"Quantity";"tax_rule";"Price";"Image URLs"';
$string .= PHP_EOL;
for($i=1;$i<count($results);$i++)
{
if($results[$i][$category_index] != 'Schoenen en sokjes' &&
$results[$i][$category_index] != 'Accessoires meisjes' &&
$results[$i][$category_index] != 'Accessoires jongens' &&
$results[$i][$category_index] != 'Communie -en doopkleding' &&
$results[$i][$category_index] != 'Aanbiedingen'
)
{
$string .= '"';
$string .= $i+$start_index;
$string .= '";"';
if($results[$i][$active_index] == 'Active')
$string .= 1;
else if($results[$i][$active_index] == 'Inactive')
$string .= 0;
$string .= '";"';
$string .= $results[$i][$name_index];
$string .= '";"';
$string .= $results[$i][$desc_index];
$string .= '";"';
$string .= $results[$i][$meta_title_index];
$string .= '";"';
if(strlen($results[$i][$meta_keywords_index]) < 255)
$string .= $results[$i][$meta_keywords_index];
else
{
$meta_key_words = substr($results[$i][$meta_keywords_index],0,254);
$last_comma_pos = strrpos($meta_key_words , ',');
$meta_key_words = substr($meta_key_words,0,$last_comma_pos);
$string .= $meta_key_words;
}
$string .= '";"';
$string .= $results[$i][$meta_desc_index];
$string .= '";"';
if($results[$i][$subcategory_index] == '' &&
$results[$i][$category_index] != 'Tiara\'s & Kroontjes' &&
$results[$i][$category_index] != 'Feest/Gala jurken' &&
$results[$i][$category_index] != 'Ringkussens' &&
$results[$i][$category_index] != 'Bolero\'s en Jasjes')
$string .= $results[$i][$category_index];
else if($results[$i][$category_index] == 'Tiara\'s & Kroontjes')
$string .= 'Kroontjes, diademen & tiara\'s';
else if($results[$i][$category_index] == 'Feest/Gala jurken')
$string .= 'Feest- en galajurken';
else if($results[$i][$category_index] == 'Ringkussens')
$string .= 'kado kussens';
else if($results[$i][$category_index] == 'Bolero\'s en Jasjes')
$string .= 'Bolero’s & jasjes';
else
$string .= $results[$i][$subcategory_index];
$string .= '";"';
$string .= $results[$i][$quantity_index];
$string .= '";"';
$string .= 53;
$string .= '";"';
$string .= $results[$i][$price_index];
$string .= '";"';
$string .= 'http://example.com/path/to/myimage.jpg';
$string .= '"';
$string .= PHP_EOL;
}
}
file_put_contents($filename, $string);
}
makecsvfile('products.csv' , 100);
?>
ок, спасибо, я решил свою проблемы- обычаем импорта/экспорта сценариев непосредственно между базами данных 2 PrestaShop в. – Adiasz
Не могли бы вы разместить свой скрипт? – user2360915