我想创建一个点击计数器,当用户通过他们的IP地址进入网站时,它将添加计数。这是我的代码:
观点:
<?php echo number_format(Visitor::model()->GetAll('portal')); ?>
这是我的模特:
public function attributeLabels() {
return array(
'id' => 'ID',
'web' => 'Web',
'dtvisit' => 'Dtvisit',
'total' => 'Total',
);
}
public function AddVisitor($web) {
date_default_timezone_set('Asia/Kuala_Lumpur');
$today = date('Y-m-d');
$model = $this->findByAttributes(array('dtvisit' => $today, 'web' => $web));
if (sizeof($model) > 0) {
//update
$model2 = $this->findByPk($model->id);
$model2->total = (1 + $model->total);
$model2->save();
} else {
//add new
$model2 = new Visitor;
$model2->web = $web;
$model2->dtvisit = $today;
$model2->total = 1;
$model2->save();
}
}
public function GetAll($web) {
date_default_timezone_set('Asia/Kuala_Lumpur');
$model = $this->findAll(array("condition" => "web='$web'"));
$total = 0;
if (sizeof($model) > 0) {
foreach ($model as $row) {
$total+= (int) $row->total;
}
}
return $total;
}
原则上,每次刷新或点击我网站上的任何链接时,总数就会增加。如何在新用户进入网站时添加总数。谢谢
答案 0 :(得分:0)
我认为你应该使用Params来传递价值
def json_template(id)
###################
# Method to create the json templates
###################
File.open("test_dir/temp_file.json", 'w'){|i|
templ = %Q<{
"id": "#{Random.rand(20)}#{Random.rand(20)}",
"version": 1.0.1.1,
"service": "ftp",
"os": "linux"
}>
i.write(templ)
}
end