zeeshan_haider000 Posted March 9, 2009 Share Posted March 9, 2009 Hi, is there a way to read specific lines? Like open up fopen(textfile.txt, r) and read line 5 to 10 or 50 to 60 and so on...? I am trying the break down the data into parts like read first 5 lines and then when user click on 'next' then read the next 5 lines etc, i have everything set up except for i dont know how to make it read the lines i want it to...? Quote Link to comment https://forums.phpfreaks.com/topic/148545-solved-how-to-read-a-specific-amount-of-lines-fopen/ Share on other sites More sharing options...
Maq Posted March 9, 2009 Share Posted March 9, 2009 You can use file(). It returns an array of the lines in the file. So now you can just reference the element key number. Quote Link to comment https://forums.phpfreaks.com/topic/148545-solved-how-to-read-a-specific-amount-of-lines-fopen/#findComment-780060 Share on other sites More sharing options...
zeeshan_haider000 Posted March 9, 2009 Author Share Posted March 9, 2009 You can use file(). It returns an array of the lines in the file. So now you can just reference the element key number. I already read about file() here but i dont know how to do what you just said lol, can you show me an example if possible? I am a noob sorry... Quote Link to comment https://forums.phpfreaks.com/topic/148545-solved-how-to-read-a-specific-amount-of-lines-fopen/#findComment-780067 Share on other sites More sharing options...
.josh Posted March 9, 2009 Share Posted March 9, 2009 Example: // grabs every line in the file and puts it into an array $lines = file('somefile.txt'); // starting line $start = 4; // array keys start at 0 not 1, so fifth element is 4 // ending line $end = 9; // 10th element is 9 // loop through and echo each line in the range for ($key = $start; $key <= $end; $key++) { echo $lines[$key] . "<br/>"; } This obviously isn't very efficient. If you have thousands of lines in your text file, every single one of them is going to be loaded up into memory. Unless you know exactly how long every line of the file is, you cannot specifically pull out a certain range of lines from a file. You could alternatively use fopen to open the file, and use fread in a while loop to go through each line, incrementing a var until you reach $start, and then doing something like printing or saving to an array each line of the fread, until it reaches $end. This also isn't that efficient. Even though you aren't putting the whole file into memory, you're still having to loop through each line until you get to your range. That's fine on "page 1" but when you're on page 100.... Alternatively, you could split your file into smaller files x lines long, representative of individual pages. file1.txt would have lines 0-9, file2.txt would have lines 10-19, etc.. and when the user clicks the next page link or whatever, it loads the next file using file. Since each file is only 10 lines long, you are only holding 10 lines worth of stuff in memory at a time, and you're not having to loop through all the previous "pages" to get to it. Simple foreach loop on the file array and you're set. Main downside to this is that you can't just change some variable to change how many lines per page to display. Alternatively, you could, you know, use a database, since that's what they are meant for.... As far as how to go from page to page, which I know you will eventually ask...the search term you are looking for is "pagination" so google that term for tutorials to understand the concept (btw there is a pagination tutorial on phpfreaks; you can read it to learn the concept, but it's based on database not flatfile). Go to the tutorial section on the main site). Quote Link to comment https://forums.phpfreaks.com/topic/148545-solved-how-to-read-a-specific-amount-of-lines-fopen/#findComment-780087 Share on other sites More sharing options...
zeeshan_haider000 Posted March 9, 2009 Author Share Posted March 9, 2009 Example: // grabs every line in the file and puts it into an array $lines = file('somefile.txt'); // starting line $start = 4; // array keys start at 0 not 1, so fifth element is 4 // ending line $end = 9; // 10th element is 9 // loop through and echo each line in the range for ($key = $start; $key <= $end; $key++) { echo $lines[$key] . "<br/>"; } This obviously isn't very efficient. If you have thousands of lines in your text file, every single one of them is going to be loaded up into memory. Unless you know exactly how long every line of the file is, you cannot specifically pull out a certain range of lines from a file. You could alternatively use fopen to open the file, and use fread in a while loop to go through each line, incrementing a var until you reach $start, and then doing something like printing or saving to an array each line of the fread, until it reaches $end. This also isn't that efficient. Even though you aren't putting the whole file into memory, you're still having to loop through each line until you get to your range. That's fine on "page 1" but when you're on page 100.... Alternatively, you could split your file into smaller files x lines long, representative of individual pages. file1.txt would have lines 0-9, file2.txt would have lines 10-19, etc.. and when the user clicks the next page link or whatever, it loads the next file using file. Since each file is only 10 lines long, you are only holding 10 lines worth of stuff in memory at a time, and you're not having to loop through all the previous "pages" to get to it. Simple foreach loop on the file array and you're set. Main downside to this is that you can't just change some variable to change how many lines per page to display. Alternatively, you could, you know, use a database, since that's what they are meant for.... As far as how to go from page to page, which I know you will eventually ask...the search term you are looking for is "pagination" so google that term for tutorials to understand the concept (btw there is a pagination tutorial on phpfreaks; you can read it to learn the concept, but it's based on database not flatfile). Go to the tutorial section on the main site). Thanks for the detailed reply. Well i already know about pagination, here is the page i am working on: click . Everything works fine except for there's an empty row at the beginning and also on first page you only get 4 rows instead of 5... and one more thing is when i turn the error_reporting on i get this: Notice: Undefined offset: 3 in /homepages/46/d272861742/htdocs/imamraza.com/tools/pashto/index.php on line 119 Notice: Undefined offset: 2 in /homepages/46/d272861742/htdocs/imamraza.com/tools/pashto/index.php on line 119 Notice: Undefined offset: 1 in /homepages/46/d272861742/htdocs/imamraza.com/tools/pashto/index.php on line 119 <?php if ($page <= 1){ $linenum = 0;} else { $linenum = $limit*$page-$limit-1; } $line2 = file($file); $limit2 = $page * $limit-1; $file = 'temp.txt'; $fp2 = fopen($file, 'w+') or die ('ERROR: Unable to open file'); while (($linenum<$limit2) && ($linenum<=$lines)){ fwrite($fp2, $line2[$linenum]); $linenum++; } fclose($fp2); $fp = fopen('temp.txt', 'r') or die ('ERROR!'); while (!feof($fp)){ //Stop reading when end of file read (feof) $line = fgets($fp, 1024); //Use 2048 if the lines are long; $row = $line; $row++; list( $word, $pro, $en, $ur) = split('\|', $line); //THIS IS LINE 119 ?> Quote Link to comment https://forums.phpfreaks.com/topic/148545-solved-how-to-read-a-specific-amount-of-lines-fopen/#findComment-780527 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.