php_joe Posted November 8, 2006 Share Posted November 8, 2006 Hi,If I have a database stored on a text file that looks like this:animal|dog|cat|mouseeats|cats|mice|grasspoops in|my yard|my house|my cerealI can sort the table by the first column like this:[code]<?$data = file($database);sort($data);?>[/code]Is there an easy way to sort the database by the second or third column?Thanks!Joe Link to comment https://forums.phpfreaks.com/topic/26593-sorting-a-textfile-database/ Share on other sites More sharing options...
brainstem Posted November 8, 2006 Share Posted November 8, 2006 Need a little bit more Joe.are you trying to sort the lines or the data within the lines? or both?and are you trying to sort just a specific line or all lines? Link to comment https://forums.phpfreaks.com/topic/26593-sorting-a-textfile-database/#findComment-121631 Share on other sites More sharing options...
php_joe Posted November 8, 2006 Author Share Posted November 8, 2006 I'm just trying to make a table that I can sort by any column. I don't want to change the information in the lines.I've been playing around with it a bit and I [i]could[/i] reorder the lines[code]<?foreach($data as $key => $value){list($one, $two, $three, $four) = explode("|", $value);$array = array($four, $three, $two, $one);$data[$key] = implode("|", $array);}$table = implode("\n", $data);sort($data);?>[/code]But it seemed a bit cumbersom and I was hoping that there was a better way.Joe Link to comment https://forums.phpfreaks.com/topic/26593-sorting-a-textfile-database/#findComment-121638 Share on other sites More sharing options...
Orio Posted November 8, 2006 Share Posted November 8, 2006 Can you give an example of how a table look like in the txt file? (like 3-4 columns)Orio. Link to comment https://forums.phpfreaks.com/topic/26593-sorting-a-textfile-database/#findComment-121656 Share on other sites More sharing options...
php_joe Posted November 8, 2006 Author Share Posted November 8, 2006 It's just several lines, seperated by whatever:[code]animal|dog|cat|mouseeats|cats|mice|grasslives in|my yard|my house|my attic[/code]Joe Link to comment https://forums.phpfreaks.com/topic/26593-sorting-a-textfile-database/#findComment-121662 Share on other sites More sharing options...
Nicklas Posted November 8, 2006 Share Posted November 8, 2006 Here´s a solution, use a [url=http://www.php.net/usort]usort()[/url] and a callback function. Then split the string by the column you want to sort on.ex:[code]<?phpfunction do_sort($a, $b) {// Sort by the second columnlist( , $test_a) = explode('|', $a);list( , $test_b) = explode('|', $b); if ($test_a == $test_b) return 0; return ($test_a < $test_b) ? -1 : +1;}$array[] = array("animal|dog|cat|mouse");$array[] = array("eats|cats|mice|grass");$array[] = array("lives in|my yard|my house|my attic");usort($array, "do_sort");print_r($array);?>[/code] Link to comment https://forums.phpfreaks.com/topic/26593-sorting-a-textfile-database/#findComment-121684 Share on other sites More sharing options...
php_joe Posted November 8, 2006 Author Share Posted November 8, 2006 Thanks Nicklas.Just one question:If I use the above function then I would need to know the number and the content of the lines, right? So that, if I added or edited a line, I'd have to also modify the code in the function?Again, thanks!Joe Link to comment https://forums.phpfreaks.com/topic/26593-sorting-a-textfile-database/#findComment-121687 Share on other sites More sharing options...
Nicklas Posted November 8, 2006 Share Posted November 8, 2006 If you want to sort on the second column, then you need to make sure that all lines have atleast 2 columns, that´s all you need to know. Number of lines doesnt matter. Link to comment https://forums.phpfreaks.com/topic/26593-sorting-a-textfile-database/#findComment-121760 Share on other sites More sharing options...
php_joe Posted November 9, 2006 Author Share Posted November 9, 2006 [quote author=Nicklas link=topic=114288.msg465084#msg465084 date=1163018882]If you want to sort on the second column, then you need to make sure that all lines have atleast 2 columns, that´s all you need to know. Number of lines doesnt matter.[/quote]Oh yes, I see.Thank you very much! :)Joe Link to comment https://forums.phpfreaks.com/topic/26593-sorting-a-textfile-database/#findComment-121938 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.