I have the following code:
<div id="uniquefriends">
<?php
$friends = $helper->getuniqueUser($uid, $TOKEN);
foreach($friends as $friend) {
echo $friend; // Facebook UID
}
?>
</div>
<a href="#" id="other"><img src="bilder/arrow_otherfriends.png" style="height: 30px;" alt="load other friends" /></a>
<script>
$(document).ready( function() {
$('#other').click(function() {
$.ajax({
type: 'POST',
url: 'otherfriends/',
data: { uid: "<?php echo $uid; ?>", token: "<?php echo $TOKEN; ?>" },
cache: false,
success: function(result) {
$('#uniquefriends').html(result);
},
});
});
});
</script>
Description:
$friends = $helper->getuniqueUser($uid, $TOKEN);
is getting all the friend of the user /me/ and compares it with the DB and array_diff() shows me the difference of the friends with the users that already using the app.
You can imagine, thatcalling this function will generate a huge overload, so it would be clever not to call it multiple times.
This function gives me just 4 uid's per call (array_slice($array, 0, 4)) (Can modify that)
Problem:
When a user clicks on "<a href="#" id="other"><img src="bilder/arrow_otherfriends.png" style="height: 30px;" alt="load other friends" /></a>
" the next set of users will be generated and given me out as the result (see ajax call).
The ajax call looks like this:
<?php
if(isset($_POST['uid']) && !empty($_POST['uid'])) {
$uid = $_POST['uid'];
$token = $_POST['token'];
$helper = new helper();
$friends = $helper->getuniqueUser($uid, $token);
foreach($friends as $friend) {
echo $friend;
}
}
?>
So I'm calling every time "$friends = $helper->getuniqueUser($uid, $TOKEN);"
when a user clicks the link. The performance goes down and the waiting time is very long.
Can I somehow Improve this code, to NOT calling this function so often? Are there any options?