xbase Posted September 13, 2006 Share Posted September 13, 2006 Ok i'm reading from a file that likes look so:GATE The Gate voodoo://chat.fearlab.net=GATE 0 250 0 GENPOTTER harry potter net voodoo://chat.fearlab.net=POTTER 0 30 0 GENSYCOSI Syc0sis voodoo://chat.fearlab.net=SYCOSI 1 30 0 GENHANG H a N g O uT voodoo://chat.fearlab.net=HANG 0 30 0 GENGUYZ Syc0 Guyz voodoo://chat.fearlab.net=GUYZ 0 30 0 MENSOUTH Southern Comfort Music Room voodoo://chat.fearlab.net=SOUTH 0 30 0 MUSits tab delimeted which isnt a problem, but when I do the for each statement , how can I make it sore by lets say $array[3] but throughout the entire file?Here is my coding:[code=php:0]<? function row_color($k){ $bg1 = "#909090"; // color one $bg2 = "#c0c0c0"; // color two if ( $k%2 ) { return $bg1; } else { return $bg2; }} $readfile = file("../../voodoo/rooms.txt");// create a loop that will read all elements of the array and print out// each field of the tab-delimited text fileecho "<table border=\"0\" cellspacing=\"0\" cellpadding=\"2\" width=\"650\" align=\"center\"><tr><td width=\"30\" class=\"header\">Number </td><td width=\"300\" class=\"header\">Room Name</td><td width=\"20\" class=\"header\">Users</td></tr>";for ($k=0; $k<=count($readfile)-1; $k++) { $fields = split("\t",$readfile[$k]); echo " <tr> <td width=\"30\" class=\"info\" bgcolor=".row_color($k).">$k</td> <td width=\"300\" class=\"info\" bgcolor=".row_color($k)."><a href=$fields[2]>$fields[1]</a></td> <td width=\"20\" class=\"info\" bgcolor=".row_color($k).">$fields[3]</td> </tr> "; }echo " <tr> <td colspan=3 class=\"footer\" align=right>Total Rooms: $k</td> </tr> </table>";?>[/code] Quote Link to comment Share on other sites More sharing options...
yaba Posted September 13, 2006 Share Posted September 13, 2006 I'm not sure you can do this once you call the split() function. What I would do is put the field I want to sort by as the first thing for every line in my file and then sort the $readfile array using one of the... array sort functions (sort, ksort, asort etc), before splitting... Quote Link to comment Share on other sites More sharing options...
Jenk Posted September 13, 2006 Share Posted September 13, 2006 something like..[code]<?php$file = file('/path/to/file.csv');foreach ($file as $line){ foreach (explode("\t", $line) as $num => $col) { echo '<p>' . htmlentities($col) . '=' . htmlentities($num) . '</p>'; }}?>[/code] [/code] Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.