gusucode.com > 同城苏州黄页系统php源码程序 > lib/column.class.php

    <?
if( !class_exists('base') )
	include_once($root_path."lib/base.class.php");

class column extends base
{
	var $name		= '';
	var $columnName	= '*';
	var $type		= '';
	var $fields		= array();
	var $group		= '|';
	var $data		= '';

	var $parent		= null;

#### 公有函數 #####################################################

	function column()
	{
		$this->base();
	}

	# 添加一个栏位 作为字段的来源栏位
	function add_field(&$field)
	{
		if( is_object($field) )						// 已经的实例
			$this->fields[$field->name]=&$field;
		else										// 为创建实例,仅仅是栏位的名称
			$this->fields[$field] = $field;
	}
	
	/**
	 * 从用户提交的表单中获取数据,并整理成为写入数据库的值
	 *
	 * @return unknown
	 */
	function get_data()
	{
		if( ($fldCnt=count($this->fields)) <= 0 )
		{
			$this->bad("数据库字段:{$this->name}没有设置来源栏位。");
			return false;
		}

		foreach ($this->fields as $item)
		{
			# 遇到 无需写入数据库 的 来源栏位
			if( $item->value == '--null--' )
				return '--null--';
			else
				$arr[$item->name]=$item->value;
		}

		switch ($this->group)
		{
			case 'serialize':
				$this->data=serialize($arr);
				break;
			case '';
				$this->data=$arr[0];
				break;
			default:
				$this->data=implode($this->group,$arr);
				break;
		}

		return $this->data;

		
	}

	/**
	 * 将数据库各栏位的值分配到各相关栏位
	 *
	 */
	function set_data($value)
	{
		if( ($fldCnt=count($this->fields)) > 0 )
		{

			switch ($this->group)
			{				
				case 'serialize':
					$this->data = @ unserialize($value);
					break;
				case '';
					$this->data=array($value);
					break;
				default:
					$this->data=explode($this->group,$value);
					break;
			}
			
			$idx=-1;
			foreach ($this->fields as $key=>$item)
			{
				$idx++;
				# 放弃 数据不相对应的来源栏位 
				if( !isset($this->data[$idx]) )
					continue;

				# 放弃 尚未 实例化的 来源栏位
				if( !is_object($this->fields[$key]) )
					continue;

				$this->fields[$key]->set_data($this->data[$idx]);
			}

			return true;
		}
		else
		{
			$this->bad("column::set_data()函数遇到错误,对象 {$this->name}没有设置来源栏位。");
			return false;
		}
	}
	
}
?>