Jump to content

How do I read a text file and display just one line at a time?


Recommended Posts

Ok guys, I need to find a way that I can read a text file but display a single line from the file and have "Next" and  "Previous" buttons or something like that to load the next line from the file. I understand how to get the file into and array and display the entire file contents but I want to limit this to one line at a time with controls. Simple code example below that I use to get and display the entire file:

 

<?php
$names=file('smdr.log');
foreach($names as $name)
{
   echo $name;
}
?>

 

 

Use javascript with AJAX. Maintain the current position in a variable and pass this minus one or this plus one in the AJAX call. On the server end of the call, get the data and pass back the required name from the array. Decide what happens when you reach the ends - do you wrap around or disable the prev or next button?

Use javascript with AJAX. Maintain the current position in a variable and pass this minus one or this plus one in the AJAX call. On the server end of the call, get the data and pass back the required name from the array. Decide what happens when you reach the ends - do you wrap around or disable the prev or next button?

Ah man I am hoping to stay within my range of "knowledge" which is PHP but if AJAX is better for this then I guess I need to get to reading!

Non-ajax version

<?php
$pos = $_GET['N'] ?? 0;

$names = file('names.txt', FILE_IGNORE_NEW_LINES);
$maxpos = count($names) - 1;

if ($pos > $maxpos) $pos = 0;
if ($pos < 0) $pos = $maxpos;

$chosen_name = $names[$pos];
$prev = $pos-1;
$next = $pos+1;
?>
<!DOCTYPE html>
<html>
<head>
<charset:utf-8>
</head>

<body>
<form>
<table>
<tr>
  <td>
    <button name='N' type='submit' value='<?=$prev?>' >Previous</button>
  </td>
  <td>
    <?=$chosen_name?>
  </td>
  <td>
    <button name='N' type='submit' value='<?=$next?>' >Next</button>
  </td>
</tr>
</table>
</form>
</body>
</html>
  • Like 1

I will try that, didn't think of using a form.

 

It doesn't have to be a form. You can decide to implement the user controls to advance from one line to another in whatever fashion you wish. You can just as easily create hyperlinks with additional parameters to navigate forward/backward. Here are two ways to do that:

 

1. First, implement a session variable to store the current line being displayed. Then, add two links to move forward or backward. E.g.

 

<A href="showline.php?move=next">Next</a>
<A href="showline.php?move=prev">Prev</a>

When the user clicks the link, get the parameter from the $_GET array and the current line from the $_SESSION array and determine the next line accordingly.

 

2. Another method is to determine the next/prev line numbers when displaying the current line and use the numbers as the parameter. This method does not require a session value. E.g.

 

//When showing line 6
<A href="showline.php?lineno=7">Next</a>
<A href="showline.php?lineno=5">Prev</a>

 

I linke option #2 the best as it gives you the flexibility to add the ability to jump to a line if you wish

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.