Reputation: 1207
I am following MVC structure on PHP which I have downloaded from github which is very helpful for me.
Now I have implemented Ajax functionality for lazy loading to load more post on user click(load more).
I have implemented on this structure and it working perfect but I am getting small issue. The issue is when there is no records available its still show me Load more content and then display No More Content to Load
I tried to solve this issue but i can't solved that's why I came here.
My code works:
index.php
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.0/jquery.min.js"></script>
<script type="text/javascript">
$(function () {
$('.more_button').live("click", function () {
var getId = $(this).attr("id");
if (getId) {
$("#load_more_" + getId).html('<img src="<?php echo URL; ?>public/img/load_img.gif" style="padding:10px 0 0 100px;"/>');
$.ajax({
type: "POST",
url: "<?php echo URL; ?>home/loadSong",
data: "getLastContentId=" + getId,
cache: false,
success: function (html) {
$("ul#load_more_ctnt").append(html);
$("#load_more_" + getId).remove();
}
});
} else {
$(".more_tab").html("<div class='all_loaded'>No More Content to Load</div>");
}
return false;
});
});
</script>
<div class='main_div'>
<ul class="load_content" id="load_more_ctnt">
<?php foreach ($songs as $song) { $id = $song->id; ?>
<li> <a href="#"><?php if (isset($song->artist)) echo $song->artist; ?></a> </li>
<?php } ?>
</ul>
<div class="more_div">
<a href="#">
<div id="load_more_<?php echo $id; ?>" class="more_tab">
<div class="more_button" id="<?php echo $id; ?>">Load More Content</div>
</div>
</a>
</div>
</div>
home.php (controller)
public function loadSong() {
if(isset($_POST['getLastContentId'])) {
// load model, perform an action on the model
$getLastContentId=$_POST['getLastContentId'];
$songs_model = $this->loadModel('HomeModel');
$songs_model->loadmoreSong($getLastContentId);
}
}
homemodel.php (model)
public function loadmoreSong($getLastContentId) {
if(isset($_POST['getLastContentId'])) {
$getLastContentId=$_POST['getLastContentId'];
$sql = "select id, artist, track, link from song WHERE id <".$getLastContentId." ORDER BY id DESC LIMIT 10";
$query = $this->db->prepare($sql);
$query->execute();
$count = $query->fetchAll();
if($count>0) {
$id = "";
for($i=0; $i<count($count); $i++){
$id = $count[$i]->id;
$message = $count[$i]->artist;
?>
<li>
<a href="#"><?php echo $message; ?></a>
</li>
<?php
}
?>
<a href="#">
<div id="load_more_<?php echo $id; ?>" class="more_tab">
<div id="<?php echo $id; ?>" class="more_button">Load More Content</div>
</div>
</a>
<?php
}
}
}
My live demo link: Load More Demo
I hope you understand my question. Need your help to fix this issue.
Thanks.
Upvotes: 2
Views: 1581
Reputation: 1949
Add SQL_CALC_FOUND_ROWS to your SELECT query like:
select SQL_CALC_FOUND_ROWS id, artist, tra....
Then check the found rows with:
SELECT FOUND_ROWS()
Store the result in a var $found_rows.
Then, at the end of homemodel.php you'll have:
if ( $count < $found_rows ) {
?>
<a href="#">
<div id="load_more_<?php echo $id; ?>" class="more_tab">
<div id="<?php echo $id; ?>" class="more_button">Load More Content</div>
</div>
</a>
<?php
} else {
?>
<a href="#">
<div id="load_more_<?php echo $id; ?>" class="more_tab">
<div class='all_loaded'>No More Content to Load</div>
</div>
</a>
<?php
}
Upvotes: 0
Reputation: 12305
Just edit this piece of code:
<a href="#">
<div id="load_more_<?php echo $id; ?>" class="more_tab">
<div id="<?php echo $id; ?>" class="more_button">Load More Content</div>
</div>
</a>
To this:
<?php
if($id!=""){
?>
<a href="#">
<div id="load_more_<?php echo $id; ?>" class="more_tab">
<div id="<?php echo $id; ?>" class="more_button">Load More Content</div>
</div>
</a>
<?php
}
?>
Upvotes: 2