LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-17-2007, 03:31 PM   #1
phpnewb72
LQ Newbie
 
Registered: Aug 2005
Location: Fresno, CA
Distribution: Ubuntu, FC8 x64, Centos
Posts: 29

Rep: Reputation: 15
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.
 
Old 12-17-2007, 07:37 PM   #2
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Code:
<?php
$string = "A,1.746,FORD TAURUS,01A,,Central Valley,Central Vall,";
$getfirst = substr($string,0,2);
if ( $getfirst == "A," ){
  echo "skip";
}
/* 
or 
echo $getfirst[0];
echo $getfirst[1];
if ( $getfirst[0] == "A" && $getfirst[1] =="," ){
  ...
}

*/
?>
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
sorting 2D data elsheikhmh Programming 10 07-04-2007 09:02 AM
Sorting Question D-KNUCKLES Linux - General 4 02-16-2007 01:40 PM
I need help sorting an array in PHP! socceroos Programming 14 05-09-2006 02:37 AM
sorting a map (C++) Thinking Programming 6 09-13-2005 04:52 PM
Sorting Beppe83 Linux - Software 7 06-21-2004 09:10 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:06 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration