⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 category.inc.php

📁 功能齐全
💻 PHP
字号:
<?PHP
if(!defined("__CLASS_CATEGORY__"))
{
	define("__CLASS_CATEGORY__",1);


	class Category
	{
		var $delIds     = array();   //当执行删除操作时,记录被删除的所有id
		var $pidData       = array();
		var $idData    = array();
		var $db;
		var $table;
		var $showline = true;

		function Category($table = 'category',$db='')
		{

			$this->db = $db;
			$this->table = $table;
			/*
			$sql = "CREATE TABLE `category` (
			`id` int(10) unsigned NOT NULL auto_increment,
			`pid` int(10) unsigned NOT NULL default '0',
			`groupid` int(5) unsigned NOT NULL default '0',
			`name` varchar(50) NOT NULL default '',
			`image` varchar(50) NOT NULL default '',
			`note` text,
			`order` int(5) unsigned NOT NULL default '0',
			PRIMARY KEY  (`id`)
			) TYPE=MyISAM AUTO_INCREMENT=37
			";
			$this->db->query($sql);
			*/
		}

		function getDataById($id,$data='')
		{
			if (empty($data))$data = $this->pidData;
			if (not_null($data))
			{
				return $data[$this->idData[$id]['pid']][$this->idData[$id]['num']];
			}

			$sql = "select * from ".$this->table." where `id` = $id";
			$this->db->query($sql);
			$returnVal=$this->db->fetch_array('',MYSQL_ASSOC);
			return $returnVal;
		}

		function getDataByPid($pid=0)
		{
			if (not_null($this->pidData))
			{
				return $this->pidData[$pid];
			}

			$sql = "select * from ".$this->table." where `pid` = $pid order by `order`";
			$this->db->query($sql);
			$returnVal = $this->db->getData();
			return $returnVal;
		}

		function getAllData()
		{
			$sql = "select * from ".$this->table." order by `pid`,`order`";
			$this->db->query($sql);
			$this->pidData = array();
			$this->idData = array();

			while ($row = $this->db->fetch_array())
			{
				$num = count($this->pidData[$row['pid']]);
				$this->pidData[$row['pid']][$num] = $row;
				$this->idData[$row['id']]['pid'] = $row['pid'];
				$this->idData[$row['id']]['num'] = $num;
			}
			return $this->pidData;
		}

		function getDelIds($id)
		{
			if (!not_null($this->pidData)) $this->getAllData();
			$this->delIds[] = $id;
			if (isset($this->pidData[$id]))
			{
				foreach ($this->pidData[$id] as $v)
				{
					$this->getDelIds($v['id']);
				}
			}
			return  $this->delIds;
		}

		function getFormatList( $pid = 0, $stopid = '')
		{
			if ($stopid === 0) return;

			if ($this->pidData != NULL) $this->getAllData();

			$tmpData = $this->pidData;

			if (count($tmpData) == 0) return;

			if (intval($stopid) > 0)unset($tmpData[$this->idData[$stopid]['pid']][$this->idData[$stopid]['num']]);

			$showData = array();

			$this->getFormatList_c($tmpData, $showData, $pid);

			return $showData;
		}

		function getFormatList_c(&$tmpData, &$showData, $pid, $headstr = '')
		{
			$num = count($tmpData[$pid]);

			$i = 0;

			foreach ($tmpData[$pid] as $key => $val)
			{
				$id     = $val['id'];
				$tmplen = count($showData);

				$showData[$tmplen] = $val;

				if (!empty($headstr)) $showData[$tmplen]['headStr'] = $headstr;

				if ($i == $num-1)
				{
					$showData[$tmplen]['headStr'] .= ($this->showline) ? "└" : " ";
					$headstr_1 =  $headstr." ";
				}
				else {
					$showData[$tmplen]['headStr'] .= ($this->showline) ? "├" : " ";
					$headstr_1 = ($this->showline) ? $headstr."│" : $headstr." ";
				}

				$showData[$tmplen]['text']=$showData[$tmplen]['headStr'].$val['name'];

				$i++;

				if (count($tmpData[$id]) > 0) $this->getFormatList_c($tmpData, $showData, $id, $headstr_1);
			}
		}

		//返回从祖目录到此的导航数组,用于生成导航条
		function getnav($id)
		{
			if (!not_null($this->pidData)) $this->getAllData();

			$ret = array();

			$this->_getnav($id,$ret);

			krsort($ret);
			$ret = array_merge($ret,array()) ;

			return $ret;
		}

		function _getnav($id,&$ret)
		{
			$val = $this->getDataById($id);

			if ($val) $ret[] = $val;

			if ($val['pid'] != 0) $this->_getnav($val['pid'],$ret);

			return $ret;
		}

		function order($pid,$newval)
		{
			if (!not_null($newval))return false;
			$data = $this->getDataByPid($pid);
			foreach ($newval as $k => $v)
			{
				foreach ($data as $_data)
				{
					if ($_data['id'] == $v)
					{
						$sql = "update ".$this->table." set `order` = '$k' where id = '".$_data['id']."'";
						$result=$this->db->query($sql);
					}
				}
			}
		}
	} //end class

}//end if defined
?>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -