Good day.
I have a page where in announcements are displayed.
In every minute I would like to check if there are any changes on the announcement.
if there are any changes I should update the content of the page.
Therefore I need to send back the old announcements to the controller for checking.
But my problem is I don't how to send an entire object from database back to the controller.
(I can send only plain single data type like string, int etc)
below is the overview of my code
Thank you in advance.
my view(php)
<div class="row " id="tabapp_news">
<div class="container">
<div class='tickercontainer'>
<div class='mask'>
<ul id="telop">
<?php
foreach ($announces as $announce) {
echo '<li>' . $announce->comment . '</li>';
}
?>
</ul>
</div>
</div>
</div>
</div>
controller
public function actionIndex()
{
$announces = [];
$desk_id = Yii::$app->request->get(DESK_KEYWORD);
if (!empty($desk_id)) {
$desk = Desk::find()
->where((['id' => $desk_id]))
->one();
if ($desk) {
$announces = Announce::getOpeningAnnouncesByDesk($desk->id);
}
}
return $this->render('announce', [
'announces' => $announces,
'desk' => $desk,
]);
}
public function actionCheckNewAnnounce()
{
$announces = Yii::$app->request->post('announces');;
$desk_id = Yii::$app->request->get(DESK_KEYWORD);
if (!empty($desk_id)) {
$desk = Desk::find()
->where((['id' => $desk_id]))
->one();
if ($desk) {
$new_announces = Announce::getOpeningAnnouncesByDesk($desk->id);
if (array_diff($new_announces, $announces)) {
return $this->render('announces', [
'announces' => $new_announces,
'desk' => $desk,
]);
}
}
}
return "";
}
my javascript/ajax
var interval = setInterval(function(){
$.ajax({
url: '/check-new-single',
dataType: 'html',
data: {announces: $("#info").data('announces')},
method: 'post',
success: function(response) {
if (response != "") {
$('#telop').empty();
$('#telop').append(response);
}
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
console.log("ajax communication failure.");
}
});
}, 60000);
Your View (index.php)
<div class="row " id="tabapp_news">
<div class="container">
<div class='tickercontainer'>
<div class='mask'>
<ul id="telop">
<?php
echo $this->render('annnounce',['announces' => $announces])
?>
</ul>
</div>
</div>
View (announce.php)
<?php
foreach ($announces as $announce) {
echo '<li>' . $announce->comment . '</li>';
}
?>
JS
var interval = setInterval(function(){
$.ajax({
url: '/check-new-announce',
dataType: 'html',
data: {announces: $("#info").data('announces')},
method: 'post',
success: function(response) {
if (response != "") {
$('#telop').html(response);
}
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
console.log("ajax communication failure.");
}
});
}, 60000);
Controller Action
Change render to render Ajax
public function actionCheckNewAnnounce()
{
$announces = Yii::$app->request->post('announces');;
$desk_id = Yii::$app->request->get(DESK_KEYWORD);
if (!empty($desk_id)) {
$desk = Desk::find()
->where((['id' => $desk_id]))
->one();
if ($desk) {
$new_announces = Announce::getOpeningAnnouncesByDesk($desk->id);
if (array_diff($new_announces, $announces)) {
return $this->renderAjax('announce', [
'announces' => $new_announces,
]);
}
}
}
return "";
}