A.B.C.

You are here: Home > Web Dev > Read a CSV using PHP

Read a CSV file using PHP

To create the virtual herbarium (swiss flowers, spanish plants), I use one single php file, which reads a CSV file and that generates on demand all the html pages. This allows me to edit only the CSV file in case I add new plants. Let's look at how this can be done.

First of all, we need a CSV file containg data. Mine has the following columns:

  • Color = main flower color
  • Num = color number
  • ColorSort = color sort order. It's the concatanation of Num + Order + Latin
  • JPGName = image name. This is used to show the image in the html
  • Order = order and genus names
  • Latin = official latin name
  • OrderSort = order sort order. It's the concatanation of Order + Latin
  • Catalan = catalan name
  • Castilian = spanish name
  • French = french name
  • German = german name
  • Italian = italian name
  • English = english name
  • Heal = 1 if healing plant; default is 0
  • Latin2 = second latin name
  • Latin3 = third latin name
  • Latin4 = fourth latin name
  • Months = flowering month
  • SizePl = plant size
  • SizeFl = flower size
  • SizeFr = fruit size
  • Where = place where it grows

Then we need a php sort function, which gets the array (in this case the whole CSV content) and the sort key (here, the name of a column in the CSV; one of the above listed words). This function will return the sorted array.

// Sort function
function aasort (&$array, $key) {
$sorter=array();
$ret=array();
reset($array);
foreach ($array as $ii => $va) {
$sorter[$ii]=$va[$key];
}
asort($sorter);
foreach ($sorter as $ii => $va) {
$ret[$ii]=$array[$ii];
}
$array=$ret;
}

Now we need the read function, which will read the CSV into an array and sort it using the function above. It gets the filename and returns the sorted array.

// Read CSV file into array. The first line in the CSV is the columns header.
function csv_to_array($filename='', $delimiter=',')
{
$mySort = htmlspecialchars($_GET["mySort"]);
//echo 'mySort is: '.$mySort.'.<br>';
if(!file_exists($filename) || !is_readable($filename))
return FALSE;

$header = NULL;
$data = array();
if (($handle = fopen($filename, 'r')) !== FALSE)
{
while (($row = fgetcsv($handle, 1000, $delimiter)) !== FALSE)
{
if(!$header)
$header = $row;
else
$data[] = array_combine($header, $row);
}
fclose($handle);
}
// use one of the following: Color, ColorSort, JPGName, Order, Latin, OrderSort, Catalan, Castilian, French, German, Italian, English, Heal
aasort($data,$mySort);
return $data;
}

The array is created by calling the function like this:

$myArray = csv_to_array('./csv/PlantBase.csv');

It makes sense to remove empty records from the array:

$myArray = array_filter( $myArray );

Finally, we can create the html page content. Here it's a DIV CLASS. We do it for each row, as every row is a different plant.

foreach ($myArray as $row) {
echo "<div class=\"box\">\n";
echo "<div class=\"boxInner\">\n";
echo "<IMG SRC=\"img/" . $row['JPGName'] . "\" /></a>\n";
echo "<a href=\"http://ddg.gg/?q=" . $row['Latin'] . "\" target=_blank><div class=\"titleBox\"><i>" . $row['Order'] . "</i><br>" . $row['Latin'] . "<br>" . $row['Catalan'] . "<br>" . $row['Castilian'] . "<br>" . $row['French'] . "<br>" . $row['German'] . "</div></a>\n";
echo "</div>";
echo "</div>";
}

For the healing flowers page, which is a subset of the whole array, we add an IF clause to the generation:

// take only the value if Heal column is 1
foreach ($myArray as $row) {
if ($row['Heal'] == '1'){
echo "<div class=\"box\">\n";
echo "<div class=\"boxInner\">\n";
echo "<IMG SRC=\"img/" . $row['JPGName'] . "\" /></a>\n";
echo "<a href=\"http://ddg.gg/?q=" . $row['Latin'] . "\" target=_blank><div class=\"titleBox\"><i>" . $row['Order'] . "</i><br>" . $row['Latin'] . "<br>" . $row['Catalan'] . "<br>" . $row['Castilian'] . "<br>" . $row['French'] . "<br>" . $row['German'] . "</div></a>\n";
echo "</div>";
echo "</div>";
}
}

The different pages are generated by calls like this one, where we pass the sort order directly through the URL:

<a href="http://www.mountainpath.ch/FormPlants/index.php?mySort=Latin">Latin</a>

 

nach oben