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

Asked

Viewed 66 times

0

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

Follow 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>

1 answer

2


Friend, what will determine this is the result of your consultation with the bank. So, use a 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’ve helped.

  • Thank you so much for your help.

Browser other questions tagged

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