On-demand paging with button and POST method in PHP using PDO

0

I looked at some sites and saw some things about it, but I can not understand the logic behind the page on demand.

jQuery(document).ready(function(){
        jQuery('#btnpaginas').click(function(){
            var dados = jQuery( this ).serialize();

            jQuery.ajax({
                type: "POST",
                url: "paginacao.php",
                data: dados,
                success: function(data)
                {
                                $(".paginacao").html(data);
                }
            });

            return false;
        });
    });

<button id="btnpaginas">Carregar mais</button>

PHP

$inicio = 5;

$stmt = $conecta->prepare("select * from postagem order by idpost desc limit $inicio " );
    $stmt->execute();

I know I'm doing it wrong. It already starts with 1 record and when I press the "load more" button it shows the first record, which it already had, and four more. But with this code I did, it will not load 5 more if I click again, I want to know how I do it whenever I click the button, it will show 5 more records for my posting system.

    
asked by anonymous 01.09.2017 / 03:06

2 answers

0

Your problem is in understanding SQL, more specifically in the "LIMIT" clause. See how it works at the link: link

To work the way you need it, do the following:

1) On your page, set a "PAGE" variable that will save the page number you want the results to be. The first results correspond to page 0 "zero". Each time you click on the LOAD MORE button, increase the "PAGE"

2) whenever you make a request to the server, send page number

To understand the technique I describe, go to the link: link

# Code

jQuery(document).ready(function(){
var pagina = 0;
jQuery('#btnpaginas').click(function(){
    pagina++;
    var dados = jQuery( this ).serialize();

    jQuery.ajax({
        type: "POST",
        url: "paginacao.php?pagina="+pagina,
        data: dados,
        success: function(data)
        {
            $(".paginacao").html(data);

        }
    });
    return false;
});

PHP

<?php
$itensPorPagina = 10;
$pagina = ( !isset( $_GET['pagina'] ) ? 0 : ((int)$_GET['pagina']) - 1);
$inicio = $pagina * $itensPorPagina;


$stmt = $conecta->prepare("SELECT * FROM postagem ORDER BY idpost DESC LIMIT $inicio, $itensPorPagina " );
$stmt->execute();

I hope I have helped

    
01.09.2017 / 19:16
1
  • HTML / JS code

    <script type="text/javascript">
    $(document).ready(function(){
      jQuery('#btnpaginas').click(function(){
        var dados = jQuery( this ).serialize();
        jQuery.ajax({
            type: "POST",
            url: "teste.php",
            data: dados,
            success: function(data)
            {
                            $(".paginacao").html(data);
            }
        });
    
        return false;
        });
    });
    </script>
    
    <button id="btnpaginas">Carregar mais</button>
    
    <div class="paginacao"></div>
    
  • PHP code

    <?php 
    
    $numPaginas = 5;
    
    $stmt = $conecta->prepare("select * from postagem order by idpost desc limit $numPaginas " );
    echo $stmt->execute();
    
  • 01.09.2017 / 03:39