Re: [PHP] sorting a csv text file in PHP

2001-02-15 Thread Richard Lynch

 Hi list,
 I have a csv text file that looks like this:

 Mike,1,Monday 12th of February 2001 02:14:09 PM
 Bob,3,Monday 12th of February 2001 02:14:17 PM
 Marry,2,Monday 12th of February 2001 02:16:38 PM

 I'm trying to get it to sort by the 2nd value (number
 descending and to show only the top two highest
 scores:
 Like this..

 Bob,3,Monday 12th of February 2001 02:14:17 PM
 Marry,2,Monday 12th of February 2001 02:16:38 PM

 This is what I have for code now:

 // show high scores
 $lines = file("quiz.txt");
 sort($lines);
 for ($ i=0; $i  count ($lines); $i++){
 $thisline = explode(",", $lines[$i]);

You should use that fgetcsv function... if it's in your version.
CSV is *not* simply comma-delimited.
Embedded commas and quote complicate it horribly.

 echo "br";
 for($j=0; $j  count($thisline);$j++)

 echo "$thisline[$j]" ;

 }

 Is this possible to do?
 I'm running linux with php3

It's possible to do, but you are currently sorting by the full line.
So "Bob" will always come before "Mike", no matter what their scores.
You'll need to get them all into a 2-D array with the fgetcsv, and use
http://php.net/usort

--
Visit the Zend Store at http://www.zend.com/store/
Wanna help me out?  Like Music?  Buy a CD: http://l-i-e.com/artists.htm
Volunteer a little time: http://chatmusic.com/volunteer.htm



-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]




[PHP] sorting a csv text file in PHP

2001-02-12 Thread Mike Mike

Hi list,
I have a csv text file that looks like this:

Mike,1,Monday 12th of February 2001 02:14:09 PM
Bob,3,Monday 12th of February 2001 02:14:17 PM
Marry,2,Monday 12th of February 2001 02:16:38 PM

I'm trying to get it to sort by the 2nd value (number
descending and to show only the top two highest
scores:
Like this..

Bob,3,Monday 12th of February 2001 02:14:17 PM
Marry,2,Monday 12th of February 2001 02:16:38 PM

This is what I have for code now:

// show high scores
$lines = file("quiz.txt");
sort($lines);
for ($ i=0; $i  count ($lines); $i++){
$thisline = explode(",", $lines[$i]);
echo "br";
for($j=0; $j  count($thisline);$j++)  { 
 echo "$thisline[$j]" ;

}

Is this possible to do?
I'm running linux with php3
Thank you
  --Mike

__
Do You Yahoo!?
Get personalized email addresses from Yahoo! Mail - only $35 
a year!  http://personal.mail.yahoo.com/

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]