Get data .Txt php inverted

4

I saved the users' logins, but when printing these on the screen they appear in order of insertion, but I wanted to present myself the data from the last one to the first one.

The code I have to display and paginate the results is as follows:

<table class="table table-bordered table-striped">
      <thead>
      <tr>
        <th>
        Username</th>
        <th>
        Entrada</th>
      </tr>
      </thead>

      <tbody>
        <?php
global $PHP_SELF;

@$pagina = $_REQUEST['pagina'];
@$exibe = $_REQUEST['exibe'];

if ($pagina == "") {
    $pagina = "1";
}

if ($exibe == "") {
    $exibe = "5";
}

$arquivo_linhas = file("../includes/userlog.txt");
$conta_linhas = count($arquivo_linhas);
$total_paginas = ceil(($conta_linhas/$exibe));

$linha_chegar = (($pagina-1)*$exibe);

for ($linha = 0; $linha != $linha_chegar; $linha++) {
    list ($num_linha, $conteudo_linha) = each ($arquivo_linhas);
}

$ultima_linha = ($linha_chegar + $exibe);
if ($ultima_linha > $conta_linhas) {
    $ultima_linha = $conta_linhas;
}
$parar = "não";
while ($parar == "não") {
    list ($numlinha, $conteudolinha) = each ($arquivo_linhas);
    echo $conteudolinha;

    if (($numlinha + 1) == $ultima_linha) {
        $parar = "sim";
    }
}


?>

      </tbody>


      <!-- Modal -->
    </table>
    <ul class='pager'>
    <?php
    $navegacao = 1;

    while ($navegacao <= $total_paginas) {
        if ($navegacao != $pagina) {
        echo '<li><a href="'.$PHP_SELF.'?pagina='.$navegacao.'"> '.$navegacao.' </a></li>';
        }
        $navegacao++;
    }
    ?>

    </ul>
    
asked by anonymous 29.06.2015 / 16:53

1 answer

0

You can use array_reverse to invert the order from last to first.

$file_contents = array_reverse(file("../includes/userlog.txt"));

foreach($file_contents as $linha){
    echo $linha;
}
    
29.06.2015 / 17:11