Re: Exporter le résultat d'une boucle en csv

Bruno Bergot <[email protected]>
Newsgroups gmane.comp.web.spip.devel
Message-ID <[email protected]>
Hop,

Le 15/07/2020 à 15:03, CSI a écrit :
> 
> Le code ci-dessus affiche le résultat dans le navigateur :-( il ne
> télécharge pas le résultat ... pourtant c'est du code que j'utilise dans
> un projet PHP et ça marche .. Y-a-t-il une interaction SPIP que j'oublie
> et qui empêche ça de fonctionner ?
> 

Il faut que tu utilises la balise http_header pour ça, exemple 
#HTTP_HEADER{Content-Type: text/csv; charset=#CHARSET} et un autre 
exemple pour forcer le download dans geodiversité cf 
https://github.com/geodiversite/geodiversite/blob/master/geol_kml.html#L2

De mon côté, voici comment je fais pour réaliser des squelettes d'export 
CSV en utilisant l'API de inc/exporter_csv :

dans mon squelette export_csv.html

#HTTP_HEADER{Content-Type: text/csv; charset=#CHARSET}
[(#SESSION{statut}|=={0minirezo}|sinon_interdire_acces)]
[(#SET{items,#ARRAY})]
<BOUCLE_items(ARTICLES){id_rubrique=#CONFIG{avp/rub_fiches,1385}}{par 
titre}>
[(#SET{items,#GET{items}|push{#LISTE{#TITRE*,
#PRENOM*,
#ACTIVITE_NOM*,
#ACTIVITE_DESC*,
#COMMUNE*,
#TEL,
#EMAIL}}})]
</BOUCLE_items>
[(#VAL{export}|inc_exporter_csv_dist{#GET{items},';',#LISTE{nom,prenom,activite_nom,activite_desc,commune,tel,email}})]
</B_items>

et dans exprt_csv_fonctions.php

<?php

if (!defined('_ECRIRE_INC_VERSION')) {
	return;
}

include_spip('inc/exporter_csv');

Ainsi on génère le CSV à partir d'un array.

++
b_b
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.