Jump to content

ORDER and LIMIT in text files.


DeathStar

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/53964-order-and-limit-in-text-files/
Share on other sites

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 



}} ?>

 

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

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>';

?>

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.