Reputation: 41
I have multiple <div>
tags with the same ID. Based on day and time, I statements trigger to select relevant content.
I also monitor the Database constantly for new content and need to refresh the <div>
to display the updated or new content.
I have tried something like $("#myDiv").load('myPHPPage.php?);
with no success.
My PHP Code sample:
if($dayofweek == "Monday" && $timeofday >= "08:00:00" && $timeofday <= "11:59:59"){
//Monday morning timeslot
echo "<div id='slideshow'>";
$stmm= $conn->prepare("SELECT `Monday_Morning`
FROM `adsList`");
$stmm->execute();
while($resultst = $stmm-> fetch()){
$mondaymoringlist = $resultst["Monday_Morning"];
echo "<div class='slideshow'>";
//Display ad as an image
//<object data='/$adlist'></object>
echo "<object data='/$mondaymoringlist'></object>";
echo "</div>";
}
echo "</div>";
}
//Monday Afternoon
if ($dayofweek == "Monday" && $timeofday >= "12:00:00" && $timeofday <= "16:59:59"){
//Monday afternoon timeslot
echo "<div id='slideshow'>";
$stma= $conn->prepare("SELECT `Monday_Afternoon`
FROM `adsList`");
$stma->execute();
while($resultst = $stma-> fetch()){
$mondayafternoonlist = $resultst["Monday_Afternoon"];
echo "<div class='slideshow'>";
//Display ad as an image
//<object data='/$adlist'></object>
echo "<object data='/$mondayafternoonlist'></object>";
echo "</div>";
}
echo "</div>";
}
//Monday Evening
else if ($dayofweek == "Monday" && $timeofday >= "17:00:00" && $timeofday <= "22:00:00"){
//Monday Evevening timeslot
echo "<div id='slideshow'>";
$stme= $conn->prepare("SELECT `Monday_Evening`
FROM `adsList`");
$stme->execute();
while($resultst = $stme-> fetch()){
$mondayeveninglist = $resultst["Monday_Evening"];
echo "<div class='slideshow'>";
//Display ad as an image
//<object data='/$adlist'></object>
echo "<object data='/$mondayeveninglist'></object>";
echo "</div>";
}
echo "</div>";
}
//End of Monday
Every day of the week has similar timeslots.
I currently use location.reload();
but it is no ideal as there is another <div>
on the page which is also refreshing separately based on its own data check. With the other <div>
I am able to do $("#myDiv").load('myPHPPage.php?);
because it has a unique ID.
AJAX
function adshandler() {
var d = new Date();
var cache = d.getTime();
$.getJSON("check_time.php?cache=" + cache, function(update) {
if (update.count === true) {
$("#slideshow").load();
}
});
}
setInterval(adshandler, 600000);
Upvotes: 0
Views: 1104
Reputation: 40038
The only difference between ID and className is that the className (aka class) can be associated with more than one element. By definition, each ID on a page must be unique.
When multiple elements have the same ID, changes will happen only to the first element (with that ID) on a page.
Change the IDs to classes.
For regular polling of databases, use a recursive setTimeout and an AJAX code block.
Upvotes: 1