Loop van XML naar CVS

Overzicht Reageren

Sponsored by: Vacatures door Monsterboard

Yoeri Achterbergen

Yoeri Achterbergen

11/10/2018 20:19:52
Quote Anchor link
Hi,

Ik heb 2 verschillende en afzonderlijk geprogrammeerd.
Script 1: Doorloop map met XML bestanden en geef deze weer
Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
6
7
8
<?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
Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<?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

Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
<?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?
Gewijzigd op 11/10/2018 22:03:20 door Yoeri Achterbergen
 
PHP hulp

PHP hulp

29/03/2024 14:21:34
 
Adoptive Solution

Adoptive Solution

11/10/2018 20:50:06
Quote Anchor link
Dit lijkt er op

https://www.exchangecore.com/blog/php-output-array-csv-headers/

Let op. Achter de tweede array van $data moet nog een komma op het eind.
Gewijzigd op 11/10/2018 20:57:45 door Adoptive Solution
 
Yoeri Achterbergen

Yoeri Achterbergen

11/10/2018 21:06:20
Quote Anchor link
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|
 
Adoptive Solution

Adoptive Solution

11/10/2018 21:38:12
Quote Anchor link
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.

Toevoeging op 11/10/2018 21:47:13:

Aanvullend.

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

Yoeri Achterbergen

11/10/2018 22:12:44
Quote Anchor link
Heb het zo gedaan

Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
<?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);

?>
 
Adoptive Solution

Adoptive Solution

12/10/2018 07:43:09
Quote Anchor link
Als dat het beoogde resultaat oplevert, dan is het goed.
 



Overzicht Reageren

 
 

Om de gebruiksvriendelijkheid van onze website en diensten te optimaliseren maken wij gebruik van cookies. Deze cookies gebruiken wij voor functionaliteiten, analytische gegevens en marketing doeleinden. U vindt meer informatie in onze privacy statement.