LQ Newbie
Registered: Aug 2005
Location: Fresno, CA
Distribution: Ubuntu, FC8 x64, Centos
Posts: 29
Rep:
|
PHP fgetcsv sorting
hello,
I'm trying to develop a way to take a csv file and display only certain lines in a php file. right now I have:
<?php
$filename = "equiplist_3.csv";
$id = fopen($filename, "r");
while ($data = fgetcsv($id, filesize($filename),";"))
$table[] = $data;
fclose($id);
$col_hdgs = array_shift($table);
usort($table, "cmp");
array_unshift($table, $col_hdgs);
echo "<table>\n";
foreach($table as $row)
{
echo "<tr>";
foreach($row as $data)
echo "<td>$data</td>";
echo "</tr>\n";
}
echo "</table>\n";
function cmp($a, $b) {
// This will make it sort on the second index of the array;
// remember, arrays start with zero (0)!
$column_to_sort_on = 0;
if ($a[$column_to_sort_on] == $b[$column_to_sort_on]) return 0;
return ($a[$column_to_sort_on] < $b[$column_to_sort_on])? -1 : 1; }
?>
all suggested from the php.net fgetcsv page.
this gives me the end result of:
J,30.38,CAT 325BL EXCAVATOR,30B,2170,Idle - Arizona- Tucson Office,Arizona,Arizona
A,1.746,FORD TAURUS,01A,,Central Valley,Central Vall,
A,1.861,FORD EXPLORER,01B,,Central Valley,Central Vall,
A,1.867,FORD TAURUS,01A,,Central Valley,Central Vall,
A,1.944,FORD TAURUS,01A,,Central Valley,Central Vall,
A,10.081,"HYSTR C340C 5-8 TON DD,A",10H,,Central Valley,Central Vall,
my goal is to only show lines that start with an A. any suggestions?
I feel I do not need a database for this project, because this data will change daily, and we don't care what was an A (available) yesterday, only today or tomorrow. and the csv file is generally very short, 40-50 lines.
as my name suggests, I am a php newb/hobbyist.
thanks.
|