Thinkphp中某个字段值自增或自减函数
Thinkphp中某个字段值自增或自减函数,可以用于文章的浏览量
/** +---------------------- * 某个字段值自增或自减 +---------------------- * @access +---------------------- * @param string $model 数据表名称 * @param string $fields 自增或自减字段名称 * @param integer $value 自增或自减量,默认为1 * @param boolean $type 布尔值,默认为true自增,为false自减 +---------------------- * @return void +---------------------- */ function autoed($id = '',$model = '', $fields = '', $value = '', $type = true) { if(!empty($fields)) { if(empty($model)) { $name = $this->getActionName(); } else { $name = $model; } $model = M($name); //默认为主键ID $pk = $model->getPk(); //默认自增自减量为1 $value = !empty($value) ? $value : 1 ; //默认为true时是自增,为false时自减 $type = $type ? "Inc" : "Dec" ; switch($type) { case "Inc": $model->setInc("$fields", "$pk=$id", $value); break; case "Dec": $model->setDec("$fields", "$pk=$id", $value); $list = $model->field("$fields")->where("$pk=$id")->find(); if($list[$fields] where("$pk=$id")->setField("$fields",0); } break; } } }