Online display of values in a CSV file

Assuming that it is necessary to access information that is contained in a CSV file, the easiest way that I encountered is shown in the script below.

The source file (CSV)

Nume,Prenume,Functia,Vârsta
Adam,Eva,manager,30
Barbu,Sorin-Alin,economist,25
Serban,Dumitru,contabil,24
Vasiliu ,Viorel,jurist,50
Alba,Dana,asistent manager,20
Calina,Doina,analist,25
Tarca,Dumitrita,economist,35
Imbrea,Aurel,economist,45
Amanar,Misu,contabil,40

The Code

$csv = fopen('angajati.csv', 'r'); // fopen - access the file
while (($col = fgetcsv($csv)) !== false) // fgetcsv - get the lines from the CSV file
{echo ''.$col[0].' | '.$col[1].' | '.$col[2].' | '.'</br>';}
fclose($csv); // fclose - closethe file

To display the result, instead of using the variables $col[0], $col[1] etc, you can use implode:

echo implode(' | ', $result) . ' <br>';

The result

Nume | Prenume | Functia |
Adam | Eva | manager |
Barbu | Sorin-Alin | economist |
Serban | Dumitru | contabil |
Vasiliu | Viorel | jurist |
Alba | Dana | asistent manager |
Calina | Doina | analist |
Tarca | Dumitrita | economist |
Imbrea | Aurel | economist |
Amanar | Misu | contabil |

Author: Ovidiu.S

I am quite passionate about this professional area as if I know something - no matter how little - I want to share it with others.

Leave a Reply