Hello. Help, how to break such a thing into pags? Very necessary!

$filelogchat = file( 'f_say.dat' ); foreach($filelogchat as $logchat ){ $string=explode(" ",$logchat); $nabo= $nabo+1; } for($si=sizeof($filelogchat)-1;$si+1>sizeof($filelogchat)-$nabo; $si--) { $string=explode(" ",$filelogchat[$si]); echo $string[0].'<br/>'; } 
  • 3
    What are the pags? o_O - khaos
  • 3
    > What other pags? apparently, it is from the word Page. Probably the author seems very witty. Although, I happened to hear someone instead of "framework" saying "transom", considering that this is a cool word from the cooler box arsenal - DreamChild
  • @dreamchild, Thank you for the "transom")))) - dlarchikov
  • Framuga - FIRE! Need to make a tag! - Artem
  • @ Philip Sinkevich, If you are given an exhaustive answer, mark it as correct (click on the check mark next to the selected answer). - Astor

1 answer 1

 <? $x = explode(" ",file_get_contents('f_say.dat')); //загрузка и разбор данных $per_page = 10; //значений на странице $p = (int)$_GET['p']; //тут может быть warning, поставить @ ) $total_p = (int)(count($x) / $per_page); //страниц всего $p = $p > $total_p ? $total_p : $p; //проверка не несуществующую страницу $p = $p < 0 ? 0 : $p; for($i=$p*$per_page;$i<$p*$per_page+$per_page; $i++) { // Вывод стриницы if (isset($x[$i])) { //проверка записи (последняя страница неполная) print $x[$i]."<br>"; //вывод строки } } print "<br>Страницы: "; //вывод пагера for($i=0;$i<=$total_p; $i++) { print "<a href='?p=$i'>".($i+1)."</a>&nbsp;"; // если вместо i+1 сделать i, страницы будут идти с 0ля. } ?> 
  • why so? > $ p = (int) $ _ GET ['p']; // there may be a warning, put @) easier> if (isset ($ _ GET ['p'])) $ p = (int) $ _ GET ['p']; else $ p = 0; ## and> for ($ i = $ p * $ per_page; $ i <$ p * $ per_page + $ per_page; $ i ++) This is called a loop load! each iteration does a whole bunch of actions, although this is not necessary, all the variables need to be done before and sent to the cycle, otherwise in the future it will be porridge and it will be difficult to optimize, because there will be no errors, but the code is initially bad, very bad. \ + to everything { this is an extra syntax both in the condition and in the loop. and why is there 2 cycles at all, only one is needed - Artem
  • Why so? $ p = (int) $ _ GET ['p']; // there may be a warning, put @) So as not to do IF I do not know about the load on the cycle. I read of course - I did not think that the internal optimizer is so poorly made that it cannot precompute it itself and substitute a constant. It seems to be the basics of compilers and they already did it in the 80s. 2 cycles - 1st for outputting lines with text, and 2nd for outputting pager (1 2 3 4) {just to make it easy for the newcomer to add and remove something there. - cromax
  • thank you, very much helped out, now I'll save it in my bookmarks)) - Philip Sinkevich