bajangerry Posted November 9, 2017 Share Posted November 9, 2017 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; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/305612-how-do-i-read-a-text-file-and-display-just-one-line-at-a-time/ Share on other sites More sharing options...
Barand Posted November 9, 2017 Share Posted November 9, 2017 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? Quote Link to comment https://forums.phpfreaks.com/topic/305612-how-do-i-read-a-text-file-and-display-just-one-line-at-a-time/#findComment-1553622 Share on other sites More sharing options...
bajangerry Posted November 9, 2017 Author Share Posted November 9, 2017 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! Quote Link to comment https://forums.phpfreaks.com/topic/305612-how-do-i-read-a-text-file-and-display-just-one-line-at-a-time/#findComment-1553623 Share on other sites More sharing options...
Barand Posted November 9, 2017 Share Posted November 9, 2017 If you are not comfortable with using AJAX you can do the same thing by submitting a form with required position and reloading the page with single required name, Quote Link to comment https://forums.phpfreaks.com/topic/305612-how-do-i-read-a-text-file-and-display-just-one-line-at-a-time/#findComment-1553624 Share on other sites More sharing options...
Barand Posted November 9, 2017 Share Posted November 9, 2017 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> 1 Quote Link to comment https://forums.phpfreaks.com/topic/305612-how-do-i-read-a-text-file-and-display-just-one-line-at-a-time/#findComment-1553627 Share on other sites More sharing options...
bajangerry Posted November 10, 2017 Author Share Posted November 10, 2017 I will try that, didn't think of using a form. Quote Link to comment https://forums.phpfreaks.com/topic/305612-how-do-i-read-a-text-file-and-display-just-one-line-at-a-time/#findComment-1553645 Share on other sites More sharing options...
Psycho Posted November 10, 2017 Share Posted November 10, 2017 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 Quote Link to comment https://forums.phpfreaks.com/topic/305612-how-do-i-read-a-text-file-and-display-just-one-line-at-a-time/#findComment-1553651 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.