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

Asked

Viewed 544 times

0

I looked at some sites and saw some things about it, but I’m not getting 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 "press more" button it shows the first record, which already had, and four more. Only with this code that I made, it will not load 5 more if I click again, I want to know how to do for whenever I click the button, it show 5 more records to my posting system.

2 answers

1

  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>
    
  2. PHP code

    <?php 
    
    $numPaginas = 5;
    
    $stmt = $conecta->prepare("select * from postagem order by idpost desc limit $numPaginas " );
    echo $stmt->execute();
    
  • Lendro, the idea is that when you click on the "press more" button automatically appear 5 more posts below the existing ones. It’s a site where there will be a lot of people doing a lot of posts, like facebook, but without automatic scrolling. From what I saw this code that you put will only show up to 5 posts at most. The idea is not to use select in this case. Have some other idea?

  • Sorry, after your comment I realized that I misunderstood your question. I edited the answer, but it won’t do much good. When the person clicks on the button, a request will be made for your file. php, in the file will be made a query and you will return the result of this query pro your html file, and you have to handle the result of the query within the function success(), very likely to be received a array and the ideal is to treat it with a foreach.

0


Your problem is in understanding SQL, more specifically about the "LIMIT" clause. See how it works in the link: https://www.w3schools.com/php/php_mysql_select_limit.asp

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

1) on your page, set a variable "PAGE" which will store the page number you want to get the results from. The first results, corresponds to page 0 "zero". Each time you click the PRESS BUTTON increments the variable "PAGE"

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

To understand the technique I describe, visit the link: http://blog.thiagobelem.net/entendendo-a-paginacao-de-registros-no-mysql


# 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’ve helped

  • Oops, it helped a lot! I made some changes in PHP, but it worked out what I wanted. Thanks for your help.

Browser other questions tagged

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