There is a file and data in it. I need to break it into pieces, for example, 10 pieces each and display it in the list. I wrote a small function that seems to work, but it does not display the last 10 how to fix it, I got confused at all.

$p = 1; if(!empty($_GET['p'])) $p = intval($_GET['p']); // Полный путь к файлу $file0 = file($_SERVER['DOCUMENT_ROOT']."/arrays/file.log"); /* file.log Number 1 Number 2 Number 3 ... Number 999 Number 1000 */ // Количество показываемых данных на странице $lstvars= 10; // Считает сколько массивов в переменной $score = count($file0); $pages = $score/10; if($score > $lstvars){ $sarray = "0"; if($p > 1){ $end = $p.$sarray; if($end > $lstvars){ $start = $end - $lstvars; $sarray = $start; } } for ($i=0; $i < $lstvars; $i++) { echo $file0[$sarray]; $sarray =$sarray +1; } }else{ foreach ($file0 as $key => $value) { echo $value; } } echo "<br>"; for ($i=1; $i < $pages; $i++) { echo "<a href='?p=".$i."'>Page ".$i."</a> | "; } 
  • Sorry, could not correct the code - jcmax
  • one
    Tolley, I began to think in the evening, I did not understand your task. $ file_array = file ($ _ SERVER ['DOCUMENT_ROOT']. "/ arrays / file.log"); $ file_parts = array_chunk ($ file_array, 10); // broke the array into 10 Need, for example, the third page, take $ file_parts [3] and output all data in a loop. - Deonis
  • I don’t need to make line-by-line navigation 1,2,3,4,5,6,7 from an array, and if I click on 10 (page 10) for example, it will display from 90 to 100, for example, if I click on 7, it will display c 60 70. Well, I decided to add the problem by adding $ pages = $ score / 10 + 1; but thanks for the feature. If someone can cut it throw here. - jcmax
  • one
    > I don’t need to do line-by-line navigation from an array ... @jcmax, do not believe it, but that’s what I said. - Deonis

1 answer 1

Judging by the text of the problem ...

 //Чтение всего файла в массив $file0 = file($_SERVER['DOCUMENT_ROOT']."/arrays/file.log"); //С какой позиции выводить $start=(isset($_REQUEST['p'])) ? intval($_REQUEST['p']) : 0; //Количество позиций на странице $length=10; //Количество страниц $count=ceil(count($file0)/$length); //Вывод из файла for ($i = $start; $i <= $start+$length; $i++){ if(!isset($file0[$i])) break; echo $file0[$i].'<br>'; } //Разметка номеров страниц for ($i = 0; $i < $count; $i++){ echo '<a href="?p='.($i*$length).'">'.($i+1).'</a>&nbsp;|&nbsp;'; } 

This approach is bad because all the text of the file needs to be transferred to an array ... fraught with large amounts of text.

I would look in the direction of moving around the file with the help of pointers and outputting only the necessary lines, although I don’t remember if I can jump to the pointer, whether the pointer can “jump” exactly in the lines.

Criticism of the answer welcome.

  • plus Only here when I checked how the function array_chunk () works; I have some sort of nonsense. did not understand and left the old code. and your function is much shorter and more likely to use it. Thanks - jcmax