fetch the records through ajax and jquery in json format using kohana

HI All,

After spending lot’s  of time, i got result in josn format in kohana. I want to fetch all records of last 3 minutes of comment table and it will display on my site after check is record already show on browser or not.

As we know kohana is MVC framework so we need to handle controller as well as model.

//Model

In model first i write query to fetch records from database which is under a function get_last_three_minutes_comments.

public function get_last_three_minutes_comments($id){

$query = $this->db->query(“SELECT u.user_name, gs.* from comments as gs INNER JOIN users as u on gs.`user_id`=u.`user_id` WHERE DATE_SUB( now( ) , INTERVAL 3 MINUTE ) < gs.speak_date and gs.group_id=$id ORDER BY id DESC”);
return $query->result_array();
}

//Controller

I call this model file from controller by write below code.

public function get_last_three_minutes_comments($page_id){
$data =    $this->comment->get_last_three_minutes_comments($page_id); // i have already made a object in constructor for comment table.

// As we know model return data in mixture of array and object and for json we need data only in array format. so here i manipulate data and convert into array format.

$arr = array();
$id=0;
foreach($data as $id => $rec){
$arr[$id] = array(‘id’=>$rec->id,’user_name’=>$rec->user_name,’speak’=>$rec->speak,’speak_time’=>$rec->speak_date);
$id++;
}

//Now we change value in json format using json_encode function and return to JavaScript file by echo command.

echo json_encode(array(‘comment_data’=>$arr));
}

Javascript file

//In jquery we use $.ajax function to get data from server by using ajax. $.ajax gets some parameter such as dataType which may be get, post, json as our requirement. I use json because i need to handle data in json format. Second parameter is url. url tell to server which page will be called. third one is success. When all server processing is completed then it return data in success which i get in mycomment variable.

$.ajax({
dataType: ‘json’,
url:url_main+”widgets/get_runtime_comment/”+tabid[1],
success:function(mycomment){

// In controller i make a array which name is comment_data. Now i access data of json by name of javascript variable in which holds all data then after write array name of php then after call by array index. such as call to user_name field we are below line

mycomment.comment_data[0].user_name;

mycomment is javascript variable name, comment_data is php array and user_name is index name of that array.

len = mycomment.comment_data.length; // here i get total number of records for run a loop.
i=0;
while(i<len){
cmid = mycomment.comment_data[i].id; // here i get id of current record.
comt = $(‘#tabs-’+tabid[1]).children().find(‘.commentbox’).find(‘#’+cmid).html(); // Check whether this record is exist or not in current div.

//if i get blank then it will added into that particular div else ignore it.
if(comt==null){
dt = ‘<div id=”‘+mycomment.comment_data[i].id+’>’+mycomment.comment_data[i].speak+’<br>By: – ‘+mycomment.comment_data[i].user_name+’ on ‘+mycomment.comment_data[i].speak_time+’</div>’;
$(‘#tabs-’+tabid[1]).children().find(‘.commentbox’).find(‘._group_comments’).prepend(dt);
}
i++;
}

}
});

I hope you will learn lot’s of thing from this article.

About the Author

admin