<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>modifycando.com &#187; Csv</title>
	<atom:link href="http://www.modifycando.com/tag/csv/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.modifycando.com</link>
	<description>dal web per il web</description>
	<lastBuildDate>Mon, 06 Feb 2012 07:05:51 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Leggere da un file CSV con PHP</title>
		<link>http://www.modifycando.com/2010/07/leggere-da-un-file-csv-con-php/</link>
		<comments>http://www.modifycando.com/2010/07/leggere-da-un-file-csv-con-php/#comments</comments>
		<pubDate>Wed, 07 Jul 2010 16:16:00 +0000</pubDate>
		<dc:creator>Modify</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Csv]]></category>

		<guid isPermaLink="false">http://www.modifycando.com/?p=295</guid>
		<description><![CDATA[In questo articolo vedremo come utilizzare Php per leggere i dati contenuti in un fil CSV per stamparli su una tabella HTML.
L'utility che creeremo potrebbe essere utile ad esempio per inserire all'interno di un sito statico, dellenews, o una serie di eventi, o qualsiasi cosa che possa essere aggiornata semplicemente aggiornando il file CSV, senza bisogno di installare pesanti CMS.]]></description>
			<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop Automatic --><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><div class='shareaholic-like-buttonset' style='float:none;height:30px;'><a class='shareaholic-fblike' data-shr_layout='button_count' data-shr_showfaces='false' data-shr_href='http%3A%2F%2Fwww.modifycando.com%2F2010%2F07%2Fleggere-da-un-file-csv-con-php%2F' data-shr_title='Leggere+da+un+file+CSV+con+PHP'></a><a class='shareaholic-fbsend' data-shr_href='http%3A%2F%2Fwww.modifycando.com%2F2010%2F07%2Fleggere-da-un-file-csv-con-php%2F'></a><a class='shareaholic-googleplusone' data-shr_size='medium' data-shr_count='true' data-shr_href='http%3A%2F%2Fwww.modifycando.com%2F2010%2F07%2Fleggere-da-un-file-csv-con-php%2F' data-shr_title='Leggere+da+un+file+CSV+con+PHP'></a><a class='shareaholic-tweetbutton' data-shr_count='none' data-shr_href='http%3A%2F%2Fwww.modifycando.com%2F2010%2F07%2Fleggere-da-un-file-csv-con-php%2F' data-shr_title='Leggere+da+un+file+CSV+con+PHP'></a></div><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><!-- End Shareaholic LikeButtonSetTop Automatic --><div class="shr-publisher-295"></div><p style="text-align: justify;"><img class="alignleft  size-medium wp-image-300  " title="File csv" src="http://www.modifycando.com/wp-content/uploads/file-csv-300x288.png" alt="File csv" width="150" height="143" /><br />
In questo articolo vedremo come utilizzare Php per leggere i dati contenuti in un fil <a href="http://www.modifycando.com/tag/csv/" class="st_tag internal_tag" rel="tag" title="Post taggati con Csv">CSV</a> per stamparli su una tabella HTML.<br />
L&#8217;utility che creeremo potrebbe essere utile ad esempio per inserire all&#8217;interno di un sito statico, dellenews, o una serie di eventi, o qualsiasi cosa che possa essere aggiornata semplicemente aggiornando il file CSV, senza bisogno di installare pesanti CMS.<span id="more-295"></span></p>
<p>Un file CSV è in un semplice file di testo formattato secondo certe regole,compatibile con i principali programmi per la trattazione di fogli elettronici come Excel, il foglio di calcolo di open office.<br />
Ogni riga contiene i dati che sono separati tra loro dal carattere &#8220;;&#8221;</p>
<p>Creiamo il file dati.csv con all&#8217;interno l&#8217;elenco (per esempio ) delle news su  4 colonne ad esempio (idRiga, data, news, autore) per avere una struttura simie a quella di un database.<br />
Ci sono 2 modi per realizzare quanto detto.<br />
Nel primo metodo useremo la funzione php fopen() per leggere dal file e split() per recuperare i singoli campi</p>
<p><div style="padding-top:10px;padding-bottom:10px;" align=center><script type="text/javascript">
<!--
google_ad_client = "ca-pub-9021423780493122";
/* modifycando_468x60_nei_post */
google_ad_slot = "8443757002";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div></p>
<p><strong>I METODO</strong><br />
Quello che dobbiamo fare e&#8217; semplicemente leggere ogni riga del file e poi suddividere le varie colonne, spezzando ogni colonna con il metodo</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">split</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">';'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$riga</span><span style="color: #009900;">&#41;</span></pre></td></tr></table></div>

<p>che ritorna un array con tutti valori separati da &#8220;;&#8221;.<br />
ad esempio se avessimo:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$riga</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;pippo;pluto;paperino&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$valori</span><span style="color: #339933;">=</span><span style="color: #990000;">split</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">';'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$riga</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>avremmo un array con i valori {pippo,pluto,paperino}</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">&lt;</span> ?php
<span style="color: #b1b100;">print</span> <span style="color: #0000ff;">&quot;&lt;table border=1 width=90% &gt;&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$fd</span><span style="color: #339933;">=</span> <span style="color: #990000;">fopen</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;dati.csv&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;r&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$x</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Intestazione Tabella</span>
<span style="color: #b1b100;">print</span> <span style="color: #0000ff;">&quot;&lt;tr&gt;&quot;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">print</span> <span style="color: #0000ff;">&quot;&lt;td&gt;ID&lt;/td&gt;&quot;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">print</span> <span style="color: #0000ff;">&quot;&lt;td&gt;Data&lt;/td&gt;&quot;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">print</span> <span style="color: #0000ff;">&quot;&lt;td&gt;News&lt;/td&gt;&quot;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">print</span> <span style="color: #0000ff;">&quot;&lt;td&gt;Autore&lt;/td&gt;&quot;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">print</span> <span style="color: #0000ff;">&quot;&lt;/tr&gt;&quot;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">// News in archivio</span>
<span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">feof</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$fd</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
   <span style="color: #000088;">$riga</span><span style="color: #339933;">=&lt;</span>strong<span style="color: #339933;">&gt;</span>fgets<span style="color: #339933;">&lt;/</span>strong<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fd</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">4096</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$riga</span><span style="color: #339933;">!=</span><span style="color: #0000ff;">&quot;&quot;</span><span style="color: #009900;">&#41;</span>
   <span style="color: #009900;">&#123;</span>
      <span style="color: #000088;">$arr</span><span style="color: #339933;">=&lt;</span>strong<span style="color: #339933;">&gt;</span>split<span style="color: #339933;">&lt;/</span>strong<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">';'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$riga</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #b1b100;">print</span> <span style="color: #0000ff;">&quot;&lt;tr&gt;&quot;</span><span style="color: #339933;">;</span>
      <span style="color: #b1b100;">print</span> <span style="color: #0000ff;">&quot;&lt;td&gt;&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$arr</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;&lt;/td&gt;&quot;</span><span style="color: #339933;">;</span>
      <span style="color: #b1b100;">print</span> <span style="color: #0000ff;">&quot;&lt;td&gt;&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$arr</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;&lt;/td&gt;&quot;</span><span style="color: #339933;">;</span>
      <span style="color: #b1b100;">print</span> <span style="color: #0000ff;">&quot;&lt;td&gt;&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$arr</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;&lt;/td&gt;&quot;</span><span style="color: #339933;">;</span>
      <span style="color: #b1b100;">print</span> <span style="color: #0000ff;">&quot;&lt;td&gt;&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$arr</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;&lt;/td&gt;&quot;</span><span style="color: #339933;">;</span>
      <span style="color: #b1b100;">print</span> <span style="color: #0000ff;">&quot;&lt;/tr&gt;&quot;</span><span style="color: #339933;">;</span>
      <span style="color: #000088;">$x</span><span style="color: #339933;">++;</span>
   <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #b1b100;">print</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">fclose</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fd</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p><div style="padding-top:10px;padding-bottom:10px;" align=center><script type="text/javascript">
<!--
google_ad_client = "ca-pub-9021423780493122";
/* modifycando_468x60_nei_post */
google_ad_slot = "8443757002";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div></p>
<p><strong>II METODO</strong><br />
Una funzione PHP molto interessante per il parsing dei file CSV è fgetcsv<br />
Con questa funzione si possono leggere file in formato CSV e ottenere un array già formattato con i campi, da poter leggere riga per riga!<br />
In questo modo evitiamo di dover usare la funzione split per diviere la riga.</p>
<p>vediamo l&#8217;esempio</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$file</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;dati.csv&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$delimiter</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;,&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$echo</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;&lt;table&gt;&quot;</span><span style="color: #339933;">;</span>  <span style="color: #666666; font-style: italic;">//variabile da stampare</span>
<span style="color: #666666; font-style: italic;">//apro il file</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fp</span> <span style="color: #339933;">=</span> <span style="color: #990000;">fopen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$file</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;r&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">!==</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
 <span style="color: #666666; font-style: italic;">//per ogni riga del file...</span>
 <span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span> <span style="color: #339933;">=</span> <span style="color: #990000;">fgetcsv</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fp</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">1000</span><span style="color: #339933;">,</span> <span style="color: #000088;">$delimiter</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">!==</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
 <span style="color: #666666; font-style: italic;">//...inserisco una riga nella tabella</span>
 <span style="color: #000088;">$echo</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">&quot;&lt;tr&gt;&quot;</span><span style="color: #339933;">;</span>
 <span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$data</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$el</span> <span style="color: #009900;">&#41;</span> <span style="color: #000088;">$echo</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">&quot;&lt;td&gt;&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$el</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;&lt;/td&gt;&quot;</span><span style="color: #339933;">;</span>
 <span style="color: #000088;">$echo</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">&quot;&lt;/tr&gt;&quot;</span><span style="color: #339933;">;</span>
 <span style="color: #009900;">&#125;</span>
 <span style="color: #990000;">fclose</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fp</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//restituisco la tabella</span>
<span style="color: #000088;">$echo</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">&quot;&lt;/table&gt;&quot;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$echo</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//restituisco la tabella</span>
<span style="color: #000088;">$echo</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$echo</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

</p>
<p><div style="padding-top:10px;padding-bottom:10px;" align=center><script type="text/javascript">
<!--
google_ad_client = "ca-pub-9021423780493122";
/* modifycando_468x60_nei_post */
google_ad_slot = "8443757002";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div><!-- FACEBOOK "MI PIACE" -->
<br>
<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.facebook.com%2F%23%21%2Fpages%2FModifycandocom%2F132259753483033&layout=standard&show_faces=true&width=350&action=like&colorscheme=light&height=80" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:550px; height:80px;" allowTransparency="true"></iframe>
<br></p>
<div class="shr-publisher-295"></div><!-- Start Shareaholic LikeButtonSetBottom Automatic --><!-- End Shareaholic LikeButtonSetBottom Automatic -->]]></content:encoded>
			<wfw:commentRss>http://www.modifycando.com/2010/07/leggere-da-un-file-csv-con-php/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
	</channel>
</rss>

