Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

182
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!