Current location: Hot Scripts Forums » Programming Languages » PHP » Ordering output of fgets() from text file


Ordering output of fgets() from text file

Reply
  #1 (permalink)  
Old 03-18-04, 11:13 AM
AutoObsession87 AutoObsession87 is offline
Newbie Coder
 
Join Date: Mar 2004
Location: Mahopac, NY
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
Arrow Ordering output of fgets() from text file

Hello all,
First of all, I have found this site very useful thus far and am proud to be a new member of the forum.

Now on to my question. I have a text file which includes the following samples:
1/25/2005
a
9/17/2003
b
3/26/2004
c
3/18/2004
d
3/16/2004
e

Each letter corresponds to the date above it.

Right now I am accessing all the data in a PHP script using a while loop and fgets(). Is there an easy way to put this data in date order before I output it to the screen?

Thanks in advance!!

-Scott Petricig
Reply With Quote
  #2 (permalink)  
Old 03-19-04, 12:11 AM
blaw's Avatar
blaw blaw is offline
Junior Code Guru
 
Join Date: Dec 2003
Location: Vancouver, BC, Canada
Posts: 550
Thanks: 0
Thanked 0 Times in 0 Posts
Hi there,

Here's what I'd do:

PHP Code:

<?php


// Get each line of the file date.txt into an array $a_lines.
$a_lines file('./date.txt');

// Now we want to have an array of [alphabet] => [date] - use a for loop to do this.
// Notice that we have to use step 2 (i.e. $i += 2).
for ($i 0$i count($a_lines); $i += 2) {
    
// As for key, you need the second line (alphabet) and the first for the date.
    
$a_lines_assoc[$a_lines[($i 1)]] = $a_lines[$i];
}

// Now we have to sort the array, based on the date.
// Simple sort() wouldn't work, so use natsort().
// Finally, $a_lines_assoc has what you wanted.
natsort($a_lines_assoc);

?>
date.txt contains the following:

Code:
1/25/2005
a
12/17/2003
b
3/26/2004
c
3/18/2004
d
3/16/2004
e
BEFORE natsort(), $a_lines_assoc looks like this:

PHP Code:

Array

(
    [
a] => 1/25/2005
    
[b] => 12/17/2003
    
[c] => 3/26/2004
    
[d] => 3/18/2004
    
[e] => 3/16/2004

And AFTER:

PHP Code:

Array

(
    [
a] => 1/25/2005
    
[e] => 3/16/2004
    
[d] => 3/18/2004
    
[c] => 3/26/2004
    
[b] => 12/17/2003

As you see, it's sorted...... WAIT. I just found a flaw. Because you have the year in the end, you can't simply natsort() it and get what you want... Try this instead:

PHP Code:

<?php


// Get each line of the file date.txt into an array $a_lines.
$a_lines file('./date.txt');

// Now we want to have an array of [alphabet] => [date] - use a for loop to do this.
// Notice that we have to use step 2 (i.e. $i += 2).
for ($i 0$i count($a_lines); $i += 2) {
    
// As for key, you need the second line (alphabet) and the first for the date.
    // Use strtotime() to convert the string date into UNIX timestamp.
    
$a_lines_assoc[trim($a_lines[($i 1)])] = strtotime(trim($a_lines[$i]));
}

// Now because we have UNIX timestamp, we can use asort().
// Also, don't use simple sort() or your keys (alphabets) will be replaced with numbers.
asort($a_lines_assoc);

?>
And when echoing the contents, use date('n/j/Y', $your_date_value) like this:

PHP Code:

foreach ($a_lines_assoc as $key => $val) {

    echo 
'Key (alphabet): '.$key.' and Date (m/d/yyyy): '.date('n/j/Y'$val).'<br />';

Then you'll get this:

PHP Code:

Key (alphabet): and Date (m/d/yyyy): 12/17/2003

Key 
(alphabet): and Date (m/d/yyyy): 3/16/2004
Key 
(alphabet): and Date (m/d/yyyy): 3/18/2004
Key 
(alphabet): and Date (m/d/yyyy): 3/26/2004
Key 
(alphabet): and Date (m/d/yyyy): 1/25/2005 
More or less, here's what you wanted. Sorry about the first half of the post being useless but after typing them up I just couldn't delete it. =)

Good luck.
__________________
Blavv =|
Reply With Quote
  #3 (permalink)  
Old 03-25-04, 01:26 PM
AutoObsession87 AutoObsession87 is offline
Newbie Coder
 
Join Date: Mar 2004
Location: Mahopac, NY
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks very much for the help!! It worked beautifully!!
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
PHP code to edit a text file mdhall Script Requests 12 12-23-10 04:03 AM
Upload file to table so ONLY files tied to primary key are displayed in record? grafixDummy PHP 4 12-20-03 04:28 PM
picking random entries with a filter... Double selection problem dsumpter PHP 7 11-16-03 07:19 PM
Editing a text file. cjh_60 PHP 1 10-14-03 03:59 PM
Writes to a text file gamextremer2003 JavaScript 4 09-11-03 09:43 AM


All times are GMT -5. The time now is 10:20 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.