csv

CSV Выгрузка

21.11.2012 Bitrix, php, php скрипты , , ,

<?
require($_SERVER["DOCUMENT_ROOT"]."/bitrix/modules/main/include/prolog_before.php");

	CModule::IncludeModule("iblock");

		$handle = fopen("tb.csv", "r");
		$csv    = array();

		while ( !feof( $handle ) ) {

			$line  = fgets($handle);
			$data  = explode(";", $line);
			$csv[] = $data;

		}

		$rs = new CDBResult;
		$el = new CIBlockElement;

		$rs->InitFromArray($csv);
		$rs->NavStart(1);

		$naumberPageGet = $rs->NavNum;
		$thisPage       = $_GET["PAGEN_".$naumberPageGet];

		if( !isset( $_GET["PAGEN_".$naumberPageGet] ) ){

			$nextPge = "PAGEN_".$naumberPageGet."=2";

		}else{

			if($rs->NavPageCount > $thisPage){
				$thisPage+=1;
				$nextPge = "PAGEN_".$naumberPageGet."=".$thisPage;
			}

		}

		if($rs->IsNavPrint())
		{
			 echo "<p>"; $rs->NavPrint("Элементы"); echo "</p>";
		}

		####
		$clearArray = array();

		$el = new CIBlockElement;

		while($test = $rs->Fetch()){

			$PROP = array();
			$PROP['RATE_GROUP']     = $test[0];
			$PROP['SUM']            = $test[1];
			$PROP['COEFF_DAMAGE'][] = Array("VALUE" => $test[2],"DESCRIPTION" => "");
			$PROP['COEFF_DAMAGE'][] = Array("VALUE" => $test[3],"DESCRIPTION" => "");
			$PROP['COEFF_DAMAGE'][] = Array("VALUE" => $test[4],"DESCRIPTION" => "");
			$PROP['COEFF_DAMAGE'][] = Array("VALUE" => $test[5],"DESCRIPTION" => "");
			$PROP['COEFF_DAMAGE'][] = Array("VALUE" => $test[6],"DESCRIPTION" => "");
			$PROP['COEFF_DAMAGE'][] = Array("VALUE" => $test[7],"DESCRIPTION" => "");

			$arLoadProductArray     = Array(
				"MODIFIED_BY"       => $USER->GetID(), // элемент изменен текущим пользователем
				"IBLOCK_SECTION_ID" => false,          // элемент лежит в корне раздела
				"IBLOCK_ID"         => 34,
				"PROPERTY_VALUES"   => $PROP,
				"NAME"              => $test[0],
				"ACTIVE"            => "Y",            // активен
			);

			// if($PRODUCT_ID = $el->Add($arLoadProductArray))
				// itdbg("New ID: ".$PRODUCT_ID);
			// else
				// itdbg("Error: ".$el->LAST_ERROR,true);

			itdbg($arLoadProductArray,false,2);
			echo '<pre>';
			var_dump($arLoadProductArray);
			echo '</pre>';

		}
		###

		$nPage = $rs->NavPageNomer;

		if($thisPage == $nPage){
			itdbg('exi',true, 2);
		}
?>
		<script>
		function second_passed() {
			window.document.location.href = "<?=$APPLICATION->GetCurPageParam($nextPge, array("PAGEN_".$naumberPageGet,"MUS_IMPORT","MUS_TYPE_PROCEDURE")); ?>";
		}
		setTimeout(second_passed, 1000);
		</script>

Парсинг csv

18.03.2012 Парсинг No comments ,

<?
$handle = fopen("catalog.csv", "r");
$dataFirst = fgetcsv($handle,0,";");
while (($data = fgetcsv($handle,0,";")) !== FALSE) {
	$c = array_combine($dataFirst, $data);
    ?>
	<pre>
	<?print_r($c);?>
	</pre>
	<?
}
?>

или

<?
$handle = fopen("catalog.csv", "r");
$dataFirst = fgetcsv($handle,0,";");

while (!feof($handle)) {
	$line = fgets($handle);
	$data = explode(";", $line);
	$c = array_combine($dataFirst, $data);
        ?>
	<pre>
	<?print_r($c);?>
	</pre>
	<?
}
?>