Home >Backend Development >PHP Tutorial > Thinkphp中某个字段值从增或自减函数

Thinkphp中某个字段值从增或自减函数

WBOY
WBOYOriginal
2016-06-13 13:16:49842browse

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;
		}
	}
}
?
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn