Hello i have a blog codded in php/codeigniter and on my main page(where all the posts are displayed) i want to make an infinite scroll on my posts so that i can load 7 more when i reach the bottom of the page.
Here is my controller:
public function index()
{
$data['posts'] = $this->Model_cats->getLivePosts(7);
$data['cats'] = $this->Model_cats->getTopCategories();
$data['title'] = 'Welcome';
$data['main'] = 'public_home';
$this->load->vars($data);
$this->load->view('template', $data);
}
public function index_show_post()
{
$data['posts'] = $this->Model_cats->getLivePosts(7);
$data['cats'] = $this->Model_cats->getTopCategories();
$data['title'] = 'Welcome';
$data['main'] = 'public_home';
$data['main2'] = 'public_home_loadpost';
$this->load->vars($data);
$this->load->view('template', $data);
}
The function index_show_post() from my opinion showoul load 7 more posts for me! i don't know if this is the solution but i sure need help!
this is my model:
function getLivePosts($limit)
{
$data = array();
$this->db->limit($limit);
$this->db->where('status', 'published');
$this->db->order_by('pubdate', 'desc');
$query = $this->db->get('posts');
if($query->num_rows() > 0)
{
foreach($query->result_array() as $row)
{
$data[] = $row;
}
}
$query->free_result();
return $data;
}
and the view looks like this:
<?php
if ( count($posts) )
{
foreach ($posts as $key => $list)
{
echo "<div class='postedComment'>";
echo '<h2>'.$list['title'].'</h2>';
echo auto_typography( word_limiter($list['body'], 200) );
echo anchor('welcome/post/'.$list['id'],'read more >>');
echo "</div>";
}
echo '<br/><br/>';
}
?>
<div id='loadMoreComments' style="display:none;"></div>
and i jave the javascript file.. using ajax i want to make this happen:
$(window).scroll(function()
{
if( $(window).scrollTop() == $(document).height() - $(window).height() ){
$('div#loadMoreComments').show();
$.ajax({
url: "<?php echo base_url()/welcome/index_show_post?>?lastComment=" + $(".postedComment:last").attr("id"),
success: function(html){
if(html)
{
$("#postedComments").append(html);
$('div#loadMoreComments').hide();
}
else
{
$('div#loadMoreComments').replaceWith("Finished Loading the comments");
}
}
});
}
});
in this line of code:
url: "<?php echo base_url()/welcome/index_show_post?>?lastComment=" + $(".postedComment:last").attr("id"),
i would like to call the controller and it should work! but it doesn't!
Any suggestions?