I am planning to build a facebook like wall post system in my site. I am working on codeigniter mysql and jQuery.
Now here is my question: I am loading all posts once user come to home page. I created 2 more tables that is comments and like for like and comments of the posts.
If I am loading all posts using jQuery and json, how will I load all likes and comments of that particular post?
Here is my code:
Model
function load_all_activities()
{
$this->db->select('*')->from('activity_board');
$this->db->order_by("created_time", "desc");
$this->db->limit(20);
$this->db->join('profile', 'profile.user_id = activity_board.user_id');
$query = $this->db->get();
$data=array();
foreach ($query->result() as $row){
$row->createdtime=$this->timeago($row->created_time);
// $row->age=$this->birthday($row->birthday);
$data[]=$row;
}
return $data;
//return $query->result();
}
Controller:
function load_activities(){
$data=$this->users->load_all_activities();
echo json_encode($data);
}
View:
//Jquery load activities posts
function load_activities(){
$.post("<?=base_url()?>home/load_activities", function(data){
$(data).each(function(index, item) {
$(item).each(function(index, value) {
$("#wallposts").append('<div id="postContainer"> <div id="postPic"><a href=""><img src="'+value.picture_thumb+'"/></a></div> <div id="postMsg"><a href="#"><b>'+value.first_name +'</b></a> ' + value.message+'<br/><br/><a href="#">Like</a> · <a href="#">Comment</a> · '+value.createdtime+'<div id="deleteMsg">X</div> </div></div>');
});
});
},"json");
}
Here is my mysql tables
CREATE TABLE `activity_board` (
`activity_id` int(11) NOT NULL AUTO_INCREMENT,
`user_id` int(11) NOT NULL,
`message` text NOT NULL,
`created_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`activity_id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=6 ;
CREATE TABLE `activity_comments` (
`comment_id` int(11) NOT NULL AUTO_INCREMENT,
`comment` text NOT NULL,
`avtivity_id` int(11) NOT NULL,
`user_id` int(11) NOT NULL,
`created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`comment_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=2 ;
If you’re going to build the html using js, you should consider using something like
backbone.jsorbatman.js, these will allow you to build up templates and work in a more controlled manner. they’re client-side MVC js frameworks that will provide a lot of power..my two cents.. otherwise, i would prepend all the id’s with the post id
eg.
post_id:20