Retrieve data. Txt php in reverse

Asked

Viewed 144 times

4

I saved the user logins, but when printing these on the screen appear in order of insertion, but I wanted to present me the data from the last to the first.

The code I have to present 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>

1 answer

1


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

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

foreach($file_contents as $linha){
    echo $linha;
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.