Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

189
Visualizações
Ajax database extract - hide load more when all items loaded

I am retrieving data from a mysql database, 5 items each time.

$query = $pdo->prepare("SELECT * FROM names WHERE id < ? ORDER BY id DESC LIMIT 5");
$query->execute([$_POST["id"]]);
while($row = $query -> fetch()) {
    echo "<div id="$row["id"].">".$row["name"]."</div>";
});

The following javascript is retrieving data from/via the above mysql query. I am passing through the last id of each div and the sql is just taking ones with smaller/older ids to append them below.

function request(id) {
    var data1 = "id=" + id;
    $.ajax({
        type: "POST",
        url: "https://example.com/api",
        data: data1,
        cache: false,
        success: function(html) {
            $(".div").append(html);
        },
        error: function(XHR){
        }
    });
};

request();

$(document).on("click",".more",function(){
    var id = $(".div").children( ":last" ).attr("id");
    request(id);
});

Is there a clean way to flag the last result within the php loop so i can pick it up with javascript/jquery and hide the load ".more" button?

about 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

You can apply next steps:

  • get the count of rows in the names table and store it in hidden input
$query = $pdo->prepare("SELECT COUNT(*) C FROM names"); 
...
<input type="hidden" value="<?= $row['C'] ?>" id="table_rows_count" />
  • create additional hidden input for counter value
<input type="hidden" value="0" id="counter" />
  • create hidden div container and store new items in it
<div style="display:none;" id="hidden_div"></div>
  • get length of children in the hidden container
  • update counter value
  • compare count of rows and children length and if they are equal then hide the .more button
  • clear hidden div container
...
        success: function(html) {
            $(".div").append(html);
            
            $("#hidden_div").append(html);

            var len = $("#hidden_div")[0].children.length, 
                coun = $('#counter').val(), 
                new_counter = coun + len, 
                table_rows = $('#table_rows_count').val();

            $('#counter').val(new_counter);

            if (table_rows == new_counter) $('.more').css('display','none');

            $("#hidden_div").html('')
        },
...

Note: the output of your $.ajax() request has to be present only as a list of divs tags (<div>...</div>), nothing else. Also, you've forgot to pass an argument into your first-call function request().

about 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda