DeathStar Posted June 2, 2007 Share Posted June 2, 2007 Hello. I have been trying to search php.net and various other sites to find a way to do two things, Order output of a text file, And limit. Just liek you would in mysql, but how can I do it? Example: I have a txt file with some datain it like. 12,user,none,1 I list it into variables after getting the info from the file. But how can I limit it to say 20, and order it by numbers? ~Thankyou Quote Link to comment https://forums.phpfreaks.com/topic/53964-order-and-limit-in-text-files/ Share on other sites More sharing options...
shocker-z Posted June 2, 2007 Share Posted June 2, 2007 http://uk.php.net/sort and then to limit you could use somthing simple like $i=0; $max=20; while ($i < $max) { echo $array[$i]; $i++; } Quote Link to comment https://forums.phpfreaks.com/topic/53964-order-and-limit-in-text-files/#findComment-266770 Share on other sites More sharing options...
DeathStar Posted June 2, 2007 Author Share Posted June 2, 2007 The Sort function will not work with what i have here. As for the limit. It displays an error. Warning: Invalid argument supplied for foreach() in C:\**\**\**.php on line 128 $lines = file('file.txt'); if(!is_array($lines)) die("File could not be opened"); $i=0; $max=20; foreach($line as $line) { list($a, $b, $c, $d, $e, $f) = explode(',', $line); $b = urldecode($b); $b = addslashes($b); while ($i < $max) { ?> <tr> <td><?php echo $f; ?></td> <td><a href="<?php echo $a; ?>.php"><?php echo $b; ?></a></td> <td><a href="">None</a></td> <td><?php echo $points; ?></td><td><?php echo $d; ?></td><td><?php echo $e; ?></td> </tr> <?php }} ?> Quote Link to comment https://forums.phpfreaks.com/topic/53964-order-and-limit-in-text-files/#findComment-266774 Share on other sites More sharing options...
shocker-z Posted June 2, 2007 Share Posted June 2, 2007 thats because your saying $line as $line they have to be different otherwise it go's from an array to a string the first time it loops then oviusly its not a array. you need somthing like foreach ($line as $value) { Regards Liam Quote Link to comment https://forums.phpfreaks.com/topic/53964-order-and-limit-in-text-files/#findComment-266778 Share on other sites More sharing options...
DeathStar Posted June 2, 2007 Author Share Posted June 2, 2007 oh yes, sorry my mistake. Still does not work, it doesn't limit, or show data. Just alot of empty rows. Quote Link to comment https://forums.phpfreaks.com/topic/53964-order-and-limit-in-text-files/#findComment-266780 Share on other sites More sharing options...
shocker-z Posted June 2, 2007 Share Posted June 2, 2007 try this mate $lines = file('file.txt'); if(!is_array($lines)) die("File could not be opened"); $i=0; $max=20; foreach($lines as $line) { $i++; list($a, $b, $c, $d, $e, $f) = explode(',', $line); $b = urldecode($b); $b = addslashes($b); if ($i < $max) { ?> <tr> <td><?php echo $f; ?></td> <td><a href="<?php echo $a; ?>.php"><?php echo $b; ?></a></td> <td><a href="">None</a></td> <td><?php echo $points; ?></td><td><?php echo $d; ?></td><td><?php echo $e; ?></td> </tr> <?php } } ?> i dont think you will be able to sort the array properly.. Regards Liam Quote Link to comment https://forums.phpfreaks.com/topic/53964-order-and-limit-in-text-files/#findComment-266787 Share on other sites More sharing options...
DeathStar Posted June 2, 2007 Author Share Posted June 2, 2007 No errors. only one row with no data. Quote Link to comment https://forums.phpfreaks.com/topic/53964-order-and-limit-in-text-files/#findComment-266790 Share on other sites More sharing options...
Barand Posted June 2, 2007 Share Posted June 2, 2007 I'd use $ar = array_slice($lines, $start, $limit); which will pull $limit lines starting at $start (just like mysql limit does). Then sort the $ar array and output it <?php $ar = array ( '10,user,some,2', '22,user,none,1', '1,user,some,2', '2,client,any,4', '38,user,none,28', '12,user,none,1', '43,client,any,4', '18,user,none,28' ); natsort($ar); echo '<table border="1">'; foreach ($ar as $line) { echo '<tr><td>', join ('</td><td>', explode(',', $line)), '</td></tr>'; } echo '</table>'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/53964-order-and-limit-in-text-files/#findComment-266797 Share on other sites More sharing options...
DeathStar Posted June 2, 2007 Author Share Posted June 2, 2007 wow. Thankyou, shorter and better. Still just need to find a way to order it. Quote Link to comment https://forums.phpfreaks.com/topic/53964-order-and-limit-in-text-files/#findComment-266803 Share on other sites More sharing options...
Barand Posted June 2, 2007 Share Posted June 2, 2007 Still just need to find a way to order it. natsort() as above Quote Link to comment https://forums.phpfreaks.com/topic/53964-order-and-limit-in-text-files/#findComment-266804 Share on other sites More sharing options...
DeathStar Posted June 2, 2007 Author Share Posted June 2, 2007 Opps, forgot that. Thankyou Quote Link to comment https://forums.phpfreaks.com/topic/53964-order-and-limit-in-text-files/#findComment-266807 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.