Create pagination by displaying the current records at the top of the list

0

I am creating a page that contains pagination, but I have a problem, the pagination works perfect but it brings the oldest records first and I would like the current records to be displayed first, could anyone help me?

Here is the code I created as an example below:

    <?php  
        mysql_connect("localhost","root","root");
        mysql_select_db("paginacao");


    ?>

        <table border="1" cellpadding="10" cellspacing="10">
            <tr>
                <th width="60">ID</th>
                <th width="150">Descrição</th>
                <th width="80">Valor</th>
            </tr>

            <?php  

                $limite = 3;
                $sql_count = mysql_query("SELECT COUNT('id') FROM produtos;");

                $sql_result = ceil(mysql_result($sql_count, 0) / $limite);

                $pg = (isset($_GET["pg"])) ? (int) $_GET["pg"] : 1;

                $start = ($pg - 1) * $limite;

                $sql_produtos = mysql_query("SELECT * FROM produtos LIMIT $start, $limite");
                while ($lh = mysql_fetch_array($sql_produtos)){
            ?>
            <tr>
                <td><?php echo $lh['id'] ?></td>
                <td><?php echo utf8_encode($lh['descricao']); ?></td>
                <td align="right"><?php echo $lh['valor'] ?></td>

            </tr>
            <?php } ?>

            <tr>

                <td align="center" colspan="3" >
                    <div class="container">
                        <ul class="pagination"> 
                            <?php 
                                if ($sql_result > 1 && $pg<=$sql_result) {
                                    for($i=1; $i <= $sql_result; $i++){
                                        echo "<li><a href='?pg=$i'> $i </a></li>";
                                    }
                                }
                            ?>
                        </ul>
                    </div>

                </td>
            </tr>
        </table>

        <?php echo isset($_GET["pg"]); ?>
    </body>
    
asked by anonymous 15.06.2016 / 19:14

1 answer

2

Friend, what will determine this is the result of your query to the bank. Therefore, use ORDER BY with DESC to invert the result. Just choose which field determines which records are most recent.

It would look something like this:

$sql_produtos = mysql_query("SELECT * 
                               FROM produtos 
                              ORDER BY meu_campo DESC 
                              LIMIT $start, $limite");

I hope I have helped.

    
15.06.2016 / 19:25