Hi,

Ik heb 2 verschillende en afzonderlijk geprogrammeerd.
Script 1: Doorloop map met XML bestanden en geef deze weer

<?php
$files = glob('articles/*');
foreach($files as $file) {
    	$article = simplexml_load_file($file);    
    	$article_name = $article->LV_ARTICLES_DESCRIPTIONANDMEASURE->LV_ARTICLES_LANG_NL;   
    	$article_specification = $article->LV_ARTICLES_SPECIFICATION->LV_ARTICLES_LANG_NL;
}
?>

Script 2: Maak CSV bestand d.m.v. array's

<?php
header('Content-Type: text/csv');
header('Content-Disposition: attachment; filename="sample.csv"');
$data = array(
    		array(
    			'ID',
    			'Type',
    			'Naam',
                ),

    		array(
    			'1',
    			'Product',
    			'Testproduct',
                ),
);

$fp = fopen('php://output', 'wb');
foreach ( $data as $line ) {
    fputcsv($fp, $line);
}
fclose($fp);
?>




Nu wil ik deze combineren zodat de informatie van XML naar CSV word omgezet


<?php
header('Content-Type: text/csv');
header('Content-Disposition: attachment; filename="sample.csv"');
$files = glob('articles/*');

$data = array(
    		array(
    		'artikelnaam',
    		'Specs'
    		)
);
foreach($files as $file) {
    	$article = simplexml_load_file($file);    
    	$article_name = $article->LV_ARTICLES_DESCRIPTIONANDMEASURE->LV_ARTICLES_LANG_NL;   
    	$article_specification = $article->LV_ARTICLES_SPECIFICATION->LV_ARTICLES_LANG_NL;

  

    		
    	$data = array(
    		array(
    			"$article_name",
    			"$article_specification"
    		)
		);  

    	
$fp = fopen('php://output', 'wb');
foreach ( $data as $line ) {
    fputcsv($fp, $line);
}
}
fclose($fp);

?>


Het werkt, hij leest de xml bestanden en zet de waarde netjes in lijnen, maar de bovenste array wil ik ook mee hebben, dat is namelijk de kop van de CSV file.
Hoe kan ik dat oplossen?
De bovenste array die mag maar 1 keer voorkomen in de hele csv file dat moet de bovenste regel worden.

zoals hieronder
------------|----------
|artikelnaam|Prijs |
------------|----------
|loop naam |loop prijs|
------------|----------
|loop naam |loop prijs|
------------|----------
|loop naam |loop prijs|
------------|----------
|loop naam |loop prijs|
Waarom niet even oefenen.

Regel 17-20 in het artikel doet wat u wilt.

Met wat geknutsel kunt u het zo inpassen in uw eigen code.

Of een ander voorbeeld zoeken.

De interwebs staat er vol mee, zag ik.

[size=xsmall]Toevoeging op 11/10/2018 21:47:13:[/size]

Aanvullend.

Regel 28 zet u voor regel 12 met daarachter
fputcsv($fp, $data);

Heb het zo gedaan


<?php
header('Content-Type: text/csv');
header('Content-Disposition: attachment; filename="sample.csv"');
$files = glob('articles/*');

$datahard = array(
            array(
            'artikelnaam',
            'Prijs'
            ),
);
$fp = fopen('php://output', 'wb');

foreach ( $datahard as $line ) {
    fputcsv($fp, $line);
}

foreach($files as $file) {
        $article = simplexml_load_file($file);    
        $article_name = $article->LV_ARTICLES_DESCRIPTIONANDMEASURE->LV_ARTICLES_LANG_NL;   
        $article_price_incl = number_format((float)$article->LV_ARTICLES_PRICE_SHOW, 2, '.', '');

  

            
        $data = array(
            array(
                "$article_name",
                "$article_price_incl"
            )
        );  

        

foreach ( $data as $line ) {
    fputcsv($fp, $line);
}
}


fclose($fp);

?>
Als dat het beoogde resultaat oplevert, dan is het goed.

Reageren