Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Sorting a text file and display in decending order
Old 11-28-2005, 05:28 PM Sorting a text file and display in decending order
Junior Talker

Posts: 4
Trades: 0
I'm tring to get the contents of a textfile database and sort the data in column 10 from highest to lowest. The data in column to is numbers (AGE). The code I have been playing with isn't putting the data in the correct order .......

PHP Code:
if (!is_file("Test.txt")) { die(""); }
$file_contents file("Test.txt"); 
foreach(
$file_contents as $key=>$value){
  if(
$file_contents[$key] != ""){
    
$entry explode("|"$file_contents[$key]);
    
$entries = array('in'=>$entry[10]);
    
arsort($entriesSORT_NUMERIC);
    foreach(
$entries as $key=>$val){
      print 
"$val <br>";
    }  
  }


Last edited by 0beron; 11-28-2005 at 06:26 PM..
quickz is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 11-28-2005, 06:31 PM
0beron's Avatar
Defies a Status

Posts: 1,832
Location: Somewhere else entirely
Trades: 0
Your array $entries is being overwritten every time around the loop, so the inner loop only ever sees one value at a time. Separate out the loops so that one builds the array completely, put a sort in between and then let the second loop display them:
PHP Code:
if (!is_file("Test.txt")) { die(""); }
$file_contents file("Test.txt"); 
$entries = array();
foreach(
$file_contents as $key=>$value){
  if(
$value != ""){
    
$entry explode("|"$value);
    
$entries[] = $entry[10];
  }
}
rsort($entriesSORT_NUMERIC);
foreach(
$entries as $key=>$val){
  print 
"$val <br>";

Some explanation:
$entries = Array(); creates a blank array that the loop then fills. The syntax $entries[] means 'the next available index in $entries', so the first loop fills the array $entries with successive data items from the 10th column of the file. Also you can use $value in the first loop instead of indexing the array each time.
I use rsort here instead of arsort - $entries now has numeric keys and rsort ensures the values come out sorted when you loop through them.
The final loop should print out the values in order.
__________________
UPDATE 0beron SET talkupation = talkupation + lots WHERE post = 'helpful';

Please login or register to view this content. Registration is FREE
(aka MSN handwriting for forums)

Last edited by 0beron; 11-28-2005 at 06:35 PM..
0beron is offline
Reply With Quote
View Public Profile Visit 0beron's homepage!
 
Old 11-28-2005, 08:43 PM one more question..
Junior Talker

Posts: 4
Trades: 0
Thank you.....Works perfectly! One more question, how would you display the data in the other columns along with the sorted (AGE)?
quickz is offline
Reply With Quote
View Public Profile
 
Old 11-29-2005, 03:19 AM
0beron's Avatar
Defies a Status

Posts: 1,832
Location: Somewhere else entirely
Trades: 0
http://www.php.net/manual/en/functio...-multisort.php

^ Array multisort is what you need, in fact this PHP manual page has a code example to describe sorting database results, which is exactly what you're doing. Basically you assemble multiple arrays in the same way we assembled $entries, and then throw them all into array_multisort() with the 10th column array listed first.
__________________
UPDATE 0beron SET talkupation = talkupation + lots WHERE post = 'helpful';

Please login or register to view this content. Registration is FREE
(aka MSN handwriting for forums)
0beron is offline
Reply With Quote
View Public Profile Visit 0beron's homepage!
 
Old 11-29-2005, 03:50 PM
Junior Talker

Posts: 4
Trades: 0
o.k.. much progress but still not quite working. The "AGE" is not being displayed from highest to lowest. It simply reverses the file by column 10. I thought SORT_NUMERIC flag was supposed to do this? and the next flag SORT_DESC was supposed to reverse the list????


$file_contents = file("test.txt");
$entries = array();
foreach($file_contents as $key=>$value){
if($value != ""){
$entry = explode("|", $value);
$entries[] = array('one' => $entry[10], 'two' => $entry[4], 'three' => $entry[5]);
}
}
array_multisort($entries, SORT_NUMERIC, SORT_DESC);
foreach($entries as $key=>$val){
print "$key then $val[one] and $val[two]<br>";
}
}
quickz is offline
Reply With Quote
View Public Profile
 
Old 11-30-2005, 01:16 PM
0beron's Avatar
Defies a Status

Posts: 1,832
Location: Somewhere else entirely
Trades: 0
Hokay, I'll have another look at this and see if I can get something working in a test environment. Watch this space.
__________________
UPDATE 0beron SET talkupation = talkupation + lots WHERE post = 'helpful';

Please login or register to view this content. Registration is FREE
(aka MSN handwriting for forums)
0beron is offline
Reply With Quote
View Public Profile Visit 0beron's homepage!
 
Old 12-02-2005, 01:19 PM
0beron's Avatar
Defies a Status

Posts: 1,832
Location: Somewhere else entirely
Trades: 0
Sorry to have left this so long, been crazy busy at work and such.

Anyway, here is what I've been testing:
PHP Code:
<?php
$file_contents 
= array('val1|val2|34|spam','val2|val5|10|blah','val87|val54|88|foo','val2|val4|10|bar');
$entries_one = array();
$entries_two = array();
$entries_three = array();

foreach(
$file_contents as $key => $value){
  if(
$value != ""){
    
$entry explode("|"$value);
    
$entries_one[] = $entry[1];
    
$entries_two[] = $entry[2];
    
$entries_three[] = $entry[3];
  }
}
array_multisort($entries_threeSORT_NUMERICSORT_DESC$entries_two$entries_one);
?> <pre><?
print_r
($entries_one);
print_r($entries_two);
print_r($entries_three);
?></pre>
That gets me the output:
Code:
Array
(
    [0] => val4
    [1] => val5
    [2] => val2
    [3] => val54
)
Array
(
    [0] => 10
    [1] => 10
    [2] => 34
    [3] => 88
)
Array
(
    [0] => bar
    [1] => blah
    [2] => spam
    [3] => foo
)
This demonstrates the use of array multisort.

You'll need to change $file_contents back to getting the array from your file, and then you can change which column to sort on by adding things to the foreach loop in the middle. So it might look something like this (this hasn't been tested since I don't have your test file available):

PHP Code:
if (!is_file("Test.txt")) { die(""); }
$file_contents file("Test.txt");
$ages= array();
$names = array();
$otherdata = array();

foreach(
$file_contents as $key => $value){
  if(
$value != ""){
    
$entry explode("|"$value);
    
$ages[] = $entry[10];
    
$names[] = $entry[4];
    
$otherdata[] = $entry[5];
  }
}
array_multisort($agesSORT_NUMERICSORT_DESC$names$otherdata);
foreach(
$ages as $key => $value) {
  echo 
$names[$key] . " is aged " $value " and has data "$otherdata[$key] ."<br />";

__________________
UPDATE 0beron SET talkupation = talkupation + lots WHERE post = 'helpful';

Please login or register to view this content. Registration is FREE
(aka MSN handwriting for forums)
0beron is offline
Reply With Quote
View Public Profile Visit 0beron's homepage!
 
Reply     « Reply to Sorting a text file and display in decending order
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.82772 seconds with 12 queries