I am trying to add Post view counts using cookies in PHP Codeigniter. I have created a custom library(snippetfunctions) in which I have created a function(add_count) as shown below
public function add_count($table,$column,$value)
{
$CI =& get_instance();//CI super object
$check_visitor = $CI->input->cookie(urldecode($value), FALSE);
$ip = $CI->input->ip_address();
// if the visitor visit this article for first time then //
//set new cookie and update article_views column ..
//you might be notice we used slug for cookie name and ip
//address for value to distinguish between articles views
if ($check_visitor == false) {
$cookie = array(
"name" => urldecode($value),
"value" => "$ip",
"expire" => time() + 0,
"secure" => false
);
$CI->input->set_cookie($cookie);
$CI->Constant_model->update_counter($table,$column,urldecode($value));
}
}
and I used this function in my Controller file to add the count
$this->snippetfunctions->add_count('snippets','counter_views',$slug);
Where snippets
is the table name, counter_views
is the column name and $slug
is the URL of the post which is used to identify the post.
The error I am getting is shown in image
Model function
function update_counter($table, $column, $value)
{
// return current article views
$this->db->where($column, urldecode($value));
$this->db->select('counter_views');
$count = $this->db->get($table)->row();
// then increase by one
$this->db->where($column, urldecode($value));
$this->db->set('counter_views', ($count['counter_views'] + 1));
$this->db->update($table);
}