PHP无限分类的例子(包括数据库)转
其他常见的无限分类方法:
1,简单的通过递归查询加目录path字段的无限分类
缺点:查询数据库次数太多,不方便其他操作,比如删除节点。添加节点,移动节点
2,左右值无限分类,预排序二叉树
缺点:操作繁琐,数据库冗余,且添加删除修改都要进行左右值更新
本分类方法的优势:
1,数据库结构简单,只有 cid parentid name 三个字段,无任何冗余字段
2,不使用递归查询,所有操作只需一条sql语句
3,所有数据在读取一次数据库后,在数组内进行分析处理,节省数据库服务器资源
创建数据库以及表:
CREATE DATABASE `sortclass`DEFAULT CHARSET utf8;
CREATE TABLE IF NOT EXISTS `class` (
`cid` mediumint(8) unsigned NOT NULL auto_increment,
`pid` mediumint(8) unsigned NOT NULL,
`cname` varchar(50) NOT NULL,
PRIMARY KEY (`cid`),
KEY `pid` (`pid`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
处理文件示例:
<?php
header("Content-type: text/html; charset=utf-8");
//连接数据库
$link = mysql_connect('localhost','root','eric') or die(mysql_error());
mysql_select_db('sortclass',$link);
//无限分类类库
class SortClass{
var $data = array();
var $child = array(-1=>array());
var $layer = array(-1=>-1);
var $parent = array();
var $link;
var $table;
function SortClass($link, $table){
$this->setNode(0, -1, '顶极节点');
$this->link = $link;
$this->table = $table;
$node = array();
$results = mysql_query('select * from '.$this->table.'',$this->link);
while($node = mysql_fetch_assoc($results)){
$this->setNode($node['cid'],$node['pid'],$node['cname']);
}
}
function setNode ($id, $parent, $value){
$parent = $parent?$parent:0;
$this->data[$id] = $value;
$this->child[$id] = array();
$this->child[$parent][] = $id;
$this->parent[$id] = $parent;
$this->layer[$id] = !isset($this->layer[$parent])? 0 : $this->layer[$parent] + 1;
}
function getList (&$tree, $root= 0){
foreach ($this->child[$root] as $key=>$id){
$tree[] = $id;
if ($this->child[$id]) $this->getList($tree, $id);
}
}
function getValue ($id){return $this->d
相关文档:
<?php
/*
* 名称 : MySQL数据库基本操作
* 作者 : pjx
* 版本 : v 2010/02/25 v 1.0
* 说明 : 该类用于对MySQL做一些简单的操作
* 示例 :
* 实例 => $db = new DB_MYSQL($database),打个$database数据库
* 查询数据库 => $db->query($sql_str),查询$sql_st ......
0、用单引号代替双引号来包含字符串,这样做会更快一些。因为PHP会在双引号包围的字符串中搜寻变量,单引号则不会,注意:只有echo能这么
做,它是一种可以把多个字符串当作参数的“函数”(译注:PHP手册中说echo是语言结构,不是真正的函数,故把函数加上了双引号)。
1、如果能将类的方法定义成static,就 ......
<?php
/*
Version: v1.0
CopyRight Davis.z 2010
Creation Date 2010-02-25
----------------------- Table Script ------------------------
CREATE TABLE [dbo].[T_Employee](
[Name] [nvarchar](50) COLLATE Korean_90_CS_AS NOT NULL,
[Age] [nvarchar](5) COLLATE Korean_90_CI_AS NULL,
[ ......
if(validatorImage("d:\b.jpg"))
echo '是个低俗图片<br />';
else
echo '不是低俗图片<br />';
function validatorImage($fileName){
$image = getImage($fileName);
$width = ImagesX($image);
$height = ImagesY($image);
$ycb = 0;
for($y=0;$y<$height;$y++){
for($x=0;$x<$widt ......
<?php
//GB2312的Encode
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
/*重点了解strtotime()函数
1、strftime比time()好用,可以直接把常用的’2010-02-03‘转成时间戳。
2、date( ......