Jump to content

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 



}} ?>

 

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

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

?>

 

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.