You're making it very difficult on yourself. You don't need to first collect ID's or to fetch each individual ad using ajax. By just getting everything in 1 post you don't have the problem with duplicates in the first place.
jpost_get_ads.php:
function limit_words($string, $word_limit)
{
$words = explode(" ",$string);
return implode(" ",array_splice($words,0,$word_limit));
}
$count = (int)$_POST['count'];
$query="SELECT id, title, description FROM ads1_adsmanager_ads ORDER BY RAND() LIMIT $count";
$db->setQuery($query);
$ad_details = array();
while ($row = $db->loadAssoc()) {
$key = array_search($ids, $row['id']);
$row['title'] = limit_words($row['title'],5);
$row['description'] = limit_words($row['description'],20);
$ad_details[$key] = $row;
}
header('Content-Type: application/json');
echo json_encode($ad_details);
exit();
The javascript
var count = $("td.load_ads").length;
$.post('/self_coded_helpers/jpost_get_ads.php', { count: count } function(ads){
$("td.load_ads").each(function(){
var ad = ads.unshift()
, content;
if (ad) {
content = ad.title + "<br>" + ad.description;
} else {
content = "Place a Free Ad Now!";
}
$(this).html(content);
});
});