在codeIgniter飞增量文章的观点一行内 [英] Increment article views on the fly within one row in CodeIgniter

查看:132
本文介绍了在codeIgniter飞增量文章的观点一行内的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为柜台在我的表称为列文章

I have a column called counter in my table called articles

现在的问题是我怎么能增加价值在此列约1,所以在控制器或模型,我将使用更新数据库的功能。

The question is how can I increment the value in this column about 1, so in the controller or model I will use update db function.

例如。我的想法是这样的:

E.g. I am thinking about something like this:

 $id = 5; //article id (I will get that from the url or db, no problem with that)
 $this->db->update("current counter column value plus one where id column equals $id "); 

和假设,我得到的文章#的看法:

And let's assume that I get the article # of views:

$this->db->select_sum("counter")->get("articles");

我知道,我需要验证用户的如此如IP我这不是为每一个页面加载,但只有在5分钟计算。但是,这是另一回事)。我仅仅指刚要结束这个问题。

I know that I need to validate ip of the user so e.g. I count it not for every pageload, but only after 5 min. But that is another story ;). I jsut need to finish this problem.

推荐答案

您可以使用常规查询(绑定的):

You can use a regular query (with bindings):

$sql = "UPDATE articles SET counter = counter + 1 WHERE id = ?";
$this->db->query($sql, array($id));

或者,使用AR:

Or, using the AR:

$query = $this->db->update('articles')
                   ->set('counter','counter+1', FALSE)
                   ->where('id', $id);

假如的第三个参数设置()告诉它不要逃避列名。
你并不需要得到的若干意见,如果你只需要增加。

The FALSE as the third argument in set() tells it not to escape the column names.
You don't need to get the number of views if you only need to increment.

这篇关于在codeIgniter飞增量文章的观点一行内的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆