$('#read-more').on('click', function() {
var dataajax = {'read-more-from': $("#read-more-from").val()};
// gọi ajax
$.ajax({
url : "/news/readmore",
type : 'POST',
data : dataajax,
dataType : 'json',
success: function(data){
// gọi function append vào html
addNewsAjax(data.arrNews);
$("#read-more-from").val(data.offset);
}
});
});
=================================
public function readmoreAction() {
if ($this -> request -> isPost()) {
$this -> view -> disable();
// lấy dữ liệu truyền thông qua ajax
$offset = $this->request -> getPost('read-more-from');
$news = new News();
// lấy dữ liệu từ DB
$newsList = $news -> findByLimit(10, $offset);
// khai báo array chứa dữ liệu để loop ngoài html
$arrNews = array();
foreach ($newsList as $news) {
$arr = array('id' => $news -> id,
'title' => $news -> title,
'description' => $news -> description,
'content' => $news -> content,
'url' => $news -> url,
'image' => $news -> image,
'updated_at' => date('d-m-Y', strtotime($news -> updated_at)));
array_push($arrNews, $arr);
}
// output json ra ngoài javascript
echo json_encode(array('arrNews' => $arrNews, 'offset' => ($offset + 10)));
}
}
==================================
<a href="javascript:void(0)" id="read-more">Xem tiếp >></a>