Dranreb
Dranreb

Reputation: 313

Php: Display something if First Record is Reached

I have a database where it has 10+ records:

Accid    |    Firstname    |    Lastname
1.              John                  Marshall
2.              Sherlock            Holmes
3.              Random            Dude
...

I'd display this using echo on php but with AJAX... it first loads up 5 users, and when the user has scrolled at the bottom of the page, it will load another 5 on the list (it adds the Offset's value +=5). Here's my display code:

$sql = "SELECT * FROM users ORDER BY lastname DESC LIMIT 5 OFFSET 5";
$result = mysqli_query($connection,$sql);

While($row=mysqli_fetch_assoc) {
    echo $row['lastname']." ".$row['firstname']."<br/>";
 }

This list could be very long if I have 100 users let's say. Whenever the user scrolls at the bottom, another 5 users pops up. Now, if I reached the end of the whole records in the USERS database, I'd like to display something like - "End of User List"

How can I achieve this?

Jquery Code

$.ajax({
   type: "GET",
   url: "getusers.php",
   data: {
      'offset': 4
   },
success: function(data){
   $('#displayusers').append(data);
}
});

Upvotes: 0

Views: 159

Answers (2)

NoobishPro
NoobishPro

Reputation: 2549

I'd like to disagree with @PressingOnAlways answer.

You can just send back a different response from PHP and check it in javascript.

$sql = "SELECT * FROM users ORDER BY lastname DESC LIMIT 5 OFFSET 5";
$result = mysqli_query($connection,$sql);

if(mysqli_num_rows($result) == 0){
  die("last");
}

While($row=mysqli_fetch_assoc) {
    echo $row['lastname']." ".$row['firstname']."<br/>";
 }

Now you can just check it in javascript:

if(response == "last"){
  mydiv.append('This is the end');
}

Now, I would like to show you my way of doing things, which (imo) is a lot cleaner:

First, your ajax calls

We're going to make sure our data will be in json format automatically from now on:

  $.ajax({
    type: 'GET',
    url: "getusers.php",
    cache: false,
    data: {'offset': 4},
    dataType: "json"
  }).done(function(json){

    if(json.hasOwnProperty("last"){
      //No more results
      //do your thang;
      return false;
    }

    if(getLength(json) < 5){
      //Smaller then 5, must have hit the last. Do your thang;
      return false;
    }

    //It came here, so it's all good. Go on
    $('#displayusers').append(data);
  });

Secondly: Your PHP side

It's never a good plan to echo html over AJAX. It's way more efficient (takes up less servertime + sends smaller amounts of data over the internet highway) than doing it in PHP.

$sql = "SELECT * FROM users ORDER BY lastname DESC LIMIT 5 OFFSET 5";
$result = mysqli_query($connection,$sql);
$lastResponse = array("last" => "last");

if(mysqli_num_rows($result) == 0){
  //Always send back json or it'll give you an error
  die(json_encode($lastResponse));
}

$return = array();

While($row=mysqli_fetch_assoc) {
    $return[] = $row['lastname']." ".$row['firstname'];
 }

echo json_encode($return);

Third: A js function to check the arrayLength

//Checks the length of a json object or array
//Returns false if length is 0 or unable to check it
function getLength(obj) {
  if (typeof obj == 'undefined') {
    return false;
  }
  var l = 0;
  if (typeof obj == 'object') {
    l = Object.keys(obj).length;
  }
  else {
    l = obj.length;
  }
  return (l == 0 ? false : l);
}

Upvotes: 3

PressingOnAlways
PressingOnAlways

Reputation: 12356

The best place to implement this feature would be on the JS client side. Since your PHP script has no way of knowing if it is the end of the list or not, you need to do this on the client. The JS code should check if the results returned from your php script is less than 5, if so, the "End of User List" should be printed.

Upvotes: 0

Related Questions