根据条件建立分类缓存,以减少类别的使用,不错的代码,有需要的朋友拿去吧。
<?php
/**
* 功能: 根据条件建立分类缓存减少类别使用
*/
class treeCache
{
var $tableName = "index_category"; //表名
var $where = "1"; //where条件
var $pidStr ="i_c_pid"; //pid 的字段名
var $tempCode = array(); //生成文件的数组
var $pid = '0'; //pid的初始值
var $db ; //数据库句柄
var $idStr="i_c_id"; //取得的数据id
var $title = "i_c_cn"; //名字字段
var $createArrayName = "treeCache"; //建立的数组名字
var $createFileName =""; //建立文件的名字
var $appendArr = array(); //附加的属性,需要字段名与数据里的名对应
var $is_utf8 = false;
function treeCache()
{
}
function set($db)
{
$this->db = $db;
$this->tempCode[] = "<?php";
}
//取得所有的一级
function getRootID()
{
$sql = "SELECT {$this->idStr} FROM {$this->tableName} WHERE
{$this->pidStr}='{$this->pid}' AND {$this->where} ";
//exit($sql);
$result = $this->db->select($sql);
$temp = array();
foreach ($result as $r)
{
$temp[]=$r["{$this->idStr}"];
}
$this->tempCode[] = "\${$this->createArrayName}['root']='".implode(',',$temp)."';";
//print_r($temp);
return $temp;
}
//取得子id
function getChildren($pid)
{
$sql = "SELECT {$this->idStr} FROM {$this->tableName} WHERE
{$this->pidStr}='{$pid}' AND {$this->where} ";
$result = $this->db->select($sql);
$temp = array();
foreach ($result as $r)
{
$temp[]=$r["{$this->idStr}"];
}
return $temp;
}
//取得夫id
function getParent($cid)
{
$sql = "SELECT {$this->pidStr} FROM {$this->tableName} WHERE
{$this->idStr}='{$cid}' AND {$this->where} ";
$result = $this->db->select($sql);
//print_r($result);exit();
return $result[0]["{$this->pidStr}"];
}
//取得上级的id
function getPidStr($cid,$pidStr="")
{
$pid=$this->getParent($cid);
$temp = array();
while ($pid!=$this->pid && !emptyempty($pid)) {
$temp[] = $pid;
$pid=$this->getParent($pid);
}
//print_r($temp);
return implode(',',$temp);
}
//取得深度
function getDepth($cid,$depth=0)
{
$pid=$this->getParent($cid);
$depth++;
if( $pid != $this->pid && !emptyempty($pid))
$depth = $this->getDepth($pid,$depth);
return $depth;
}
//建立文件
function make()
{
if(emptyempty($this->createFileName))
$this->createFileName = "{$this->createArrayName}.data.php";
$rootArr = $this->getRootID();
$selectF = "{$this->idStr},{$this->title},{$this->pidStr}";
foreach ($this->appendArr as $app)
{
if(emptyempty($app)) continue;
$selectF .=",{$app}";
}
$sql = "SELECT {$selectF} FROM {$this->tableName} WHERE
{$this->where}";
$result = $this->db->select($sql);
for ($i=0;$i<count($result);$i++)
{
//id值
$this->tempCode[] =
"\${$this->createArrayName}['{$result[$i][$this->idStr]}']['id']='{$result[$i]["{$this->idStr}"]}';";
//标题
$this->tempCode[] =
"\${$this->createArrayName}['{$result[$i][$this->idStr]}']['title']='{$result[$i]["{$this->title}"]}';";
//父id
$this->tempCode[] =
"\${$this->createArrayName}['{$result[$i][$this->idStr]}']['pid']='{$result[$i]["{$this->pidStr}"]}';";
//子id
$this->tempCode[] =
"\${$this->createArrayName}['{$result[$i][$this->idStr]}']['cid']='".implode(',',$this->getChildren($result[$i]["$this->idStr"]))."';";
//目录深度
$this->tempCode[] =
"\${$this->createArrayName}['{$result[$i][$this->idStr]}']['depth']='".$this->getDepth($result[$i]["$this->idStr"])."';";
//父id的id串
$this->tempCode[] =
"\${$this->createArrayName}['{$result[$i][$this->idStr]}']['pstr']='".$this->getPidStr($result[$i]["$this->idStr"])."';";
//添加的附加属性
foreach ($this->appendArr as $app)
{
if(emptyempty($app)) continue;
$this->tempCode[] =
"\${$this->createArrayName}['{$result[$i][$this->idStr]}']['{$app}']='{$result[$i]["{$app}"]}';";
}
}
$this->tempCode[] = "return \${$this->createArrayName};";
$this->tempCode[] = "?>";
//$content = implode("\n",$this->tempCode);
//print_r($this->tempCode);
$content = implode("\n",$this->tempCode);
//建立文件
$fio=Factory::getBaseClass('FileIO');
if($this->is_utf8) $content = "\xEF\xBB\xBF".$content;
$fio->writeFile($this->createFileName,$content);
return $content ;
}
}
//加载的文件就是为了,数据库连接
//需要数据库有 select 方法
/*
include_once(dirname(dirname(__FILE__))."/config/config.inc.php");
include_once(CLASSES_PATH."factryObject.class.php");
$db = factryObject::getDB('indexPush');
$c = new treeCache($db);
$c->make();
//exit();
//做分析
include_once("treeCache.data.php");
$treeCache=isset($treeCache) ? $treeCache : array();
$rootStr = isset($treeCache['root']) ? $treeCache['root'] : "";
echo parseTree($treeCache,$rootStr);
function parseTree($treeCache,$rootStr)
{
$tempStr = "";
$temp = explode(',',$rootStr);
foreach ($temp AS $cid)
{
$info = $treeCache[$cid];
$cidStr = $info['cid'];
$tempStr .= str_repeat('-',($info['depth']-1)*3);
$tempStr.=$info['title'];
if(empty($info['pid']))
{
//附加操作
}
$tempStr .= "<br/>";
if(!empty($info['cid']))
$tempStr .=parseTree($treeCache,$info['cid']);
}
return $tempStr;
}
*/
?>
<?php
/**
* 功能: 根据条件建立分类缓存减少类别使用
* 创建日期:Thu May 31 15:55:11 CST 2007
* 最后更新:
* 作者: sanshi <sanshi0815@tom.com>
*/
class treeCache
{
var $tableName = "index_category"; //表名
var $where = "1"; //where条件
var $pidStr ="i_c_pid"; //pid 的字段名
var $tempCode = array(); //生成文件的数组
var $pid = '0'; //pid的初始值
var $db ; //数据库句柄
var $idStr="i_c_id"; //取得的数据id
var $title = "i_c_cn"; //名字字段
var $createArrayName = "treeCache"; //建立的数组名字
var $createFileName =""; //建立文件的名字
var $appendArr = array(); //附加的属性,需要字段名与数据里的名对应
var $is_utf8 = false;
function treeCache()
{
}
function set($db)
{
$this->db = $db;
$this->tempCode[] = "<?php";
}
//取得所有的一级
function getRootID()
{
$sql = "SELECT {$this->idStr} FROM {$this->tableName} WHERE
{$this->pidStr}='{$this->pid}' AND {$this->where} ";
//exit($sql);
$result = $this->db->select($sql);
$temp = array();
foreach ($result as $r)
{
$temp[]=$r["{$this->idStr}"];
}
$this->tempCode[] = "\${$this->createArrayName}['root']='".implode(',',$temp)."';";
//print_r($temp);
return $temp;
}
//取得子id
function getChildren($pid)
{
$sql = "SELECT {$this->idStr} FROM {$this->tableName} WHERE
{$this->pidStr}='{$pid}' AND {$this->where} ";
$result = $this->db->select($sql);
$temp = array();
foreach ($result as $r)
{
$temp[]=$r["{$this->idStr}"];
}
return $temp;
}
//取得夫id
function getParent($cid)
{
$sql = "SELECT {$this->pidStr} FROM {$this->tableName} WHERE
{$this->idStr}='{$cid}' AND {$this->where} ";
$result = $this->db->select($sql);
//print_r($result);exit();
return $result[0]["{$this->pidStr}"];
}
//取得上级的id
function getPidStr($cid,$pidStr="")
{
$pid=$this->getParent($cid);
$temp = array();
while ($pid!=$this->pid && !empty($pid)) {
$temp[] = $pid;
$pid=$this->getParent($pid);
}
//print_r($temp);
return implode(',',$temp);
}
//取得深度
function getDepth($cid,$depth=0)
{
$pid=$this->getParent($cid);
$depth++;
if( $pid != $this->pid && !empty($pid))
$depth = $this->getDepth($pid,$depth);
return $depth;
}
//建立文件
function make()
{
if(empty($this->createFileName))
$this->createFileName = "{$this->createArrayName}.data.php";
$rootArr = $this->getRootID();
$selectF = "{$this->idStr},{$this->title},{$this->pidStr}";
foreach ($this->appendArr as $app)
{
if(empty($app)) continue;
$selectF .=",{$app}";
}
$sql = "SELECT {$selectF} FROM {$this->tableName} WHERE
{$this->where}";
$result = $this->db->select($sql);
for ($i=0;$i<count($result);$i++)
{
//id值
$this->tempCode[] =
"\${$this->createArrayName}['{$result[$i][$this->idStr]}']['id']='{$result[$i]["{$this->idStr}"]}';";
//标题
$this->tempCode[] =
"\${$this->createArrayName}['{$result[$i][$this->idStr]}']['title']='{$result[$i]["{$this->title}"]}';";
//父id
$this->tempCode[] =
"\${$this->createArrayName}['{$result[$i][$this->idStr]}']['pid']='{$result[$i]["{$this->pidStr}"]}';";
//子id
$this->tempCode[] =
"\${$this->createArrayName}['{$result[$i][$this->idStr]}']['cid']='".implode(',',$this->getChildren($result[$i]["$this->idStr"]))."';";
//目录深度
$this->tempCode[] =
"\${$this->createArrayName}['{$result[$i][$this->idStr]}']['depth']='".$this->getDepth($result[$i]["$this->idStr"])."';";
//父id的id串
$this->tempCode[] =
"\${$this->createArrayName}['{$result[$i][$this->idStr]}']['pstr']='".$this->getPidStr($result[$i]["$this->idStr"])."';";
//添加的附加属性
foreach ($this->appendArr as $app)
{
if(empty($app)) continue;
$this->tempCode[] =
"\${$this->createArrayName}['{$result[$i][$this->idStr]}']['{$app}']='{$result[$i]["{$app}"]}';";
}
}
$this->tempCode[] = "return \${$this->createArrayName};";
$this->tempCode[] = "?>";
//$content = implode("\n",$this->tempCode);
//print_r($this->tempCode);
$content = implode("\n",$this->tempCode);
//建立文件
$fio=Factory::getBaseClass('FileIO');
if($this->is_utf8) $content = "\xEF\xBB\xBF".$content;
$fio->writeFile($this->createFileName,$content);
return $content ;
}
}
//加载的文件就是为了,数据库连接
//需要数据库有 select 方法
/*
include_once(dirname(dirname(__FILE__))."/config/config.inc.php");
include_once(CLASSES_PATH."factryObject.class.php");
$db = factryObject::getDB('indexPush');
$c = new treeCache($db);
$c->make();
//exit();
//做分析
include_once("treeCache.data.php");
$treeCache=isset($treeCache) ? $treeCache : array();
$rootStr = isset($treeCache['root']) ? $treeCache['root'] : "";
echo parseTree($treeCache,$rootStr);
function parseTree($treeCache,$rootStr)
{
$tempStr = "";
$temp = explode(',',$rootStr);
foreach ($temp AS $cid)
{
$info = $treeCache[$cid];
$cidStr = $info['cid'];
$tempStr .= str_repeat('-',($info['depth']-1)*3);
$tempStr.=$info['title'];
if(empty($info['pid']))
{
//附加操作
}
$tempStr .= "<br/>";
if(!empty($info['cid']))
$tempStr .=parseTree($treeCache,$info['cid']);
}
return $tempStr;
}
*/
?>
这个类是改善过的,当初做这个的适合,只能是数字的作为数组下标,这次支持字母了
另外就是关于解析
view plaincopy to clipboardprint?
<?php
class parseTree
{
var $ads_type_file = "";
var $isX = false;
var $rowSize=2;
function parseTree()
{
$this->ads_type_file = CACHE_PATH."ads_type_arr.data.php";
$this->ads_city_file = CACHE_PATH."ads_city_arr.data.php";
}
function make_ads_type()
{
$db = Factory::getDB("ads_type");
$tree =Factory::getItemClass('treeCache');
$tree->set($db);
$tree->tableName=$db->tableName;
$tree->pidStr ="ads_type_pid";
$tree->idStr = "ads_type_id";
$tree->title = "ads_type_name";
$tree->createArrayName ="ads_type_arr";
$tree->where = " ads_type_state=1 ORDER BY ads_type_id DESC ";
$tree->appendArr = array("ads_type_info");
$tree->createFileName = $this->ads_type_file;
$tree->is_utf8 = true;
return $tree->make();
}
function get_ads_type_str()
{
$temp_arr = $this->get_ads_type_arr();
$treeArr = emptyempty($temp_arr) ? array() : $temp_arr;
$rootStr = isset($temp_arr['root']) ? $temp_arr['root'] : "";
$show_content = $this->__parseTree($treeArr,$rootStr,'pares_type_link');
return $show_content;
}
function get_ads_type_arr()
{
return is_file($this->ads_type_file) ? require($this->ads_type_file) : array();
}
function pares_type_link($info)
{
$class_name = "ads_type";
$tempStr = "[<a href="/blog_article/c/{$class_name}/amp;m/add/amp;ads_type_pid/{$info[.html" href="/blog_article/c/{$class_name}/amp;m/add/amp;ads_type_pid/{$info[.html"id']}'>子-添加</a>]";
$tempStr .="[<a href="/blog_article/c/{$class_name}/amp;m/edit/amp;ads_type_id/{$info[.html" href="/blog_article/c/{$class_name}/amp;m/edit/amp;ads_type_id/{$info[.html"id']}'>编辑</a>]";
$tempStr .="[<a href="/blog_article/c/{$class_name}/amp;m/del/amp;ads_type_id/{$info[.html" href="/blog_article/c/{$class_name}/amp;m/del/amp;ads_type_id/{$info[.html"id']}'>删除</a>]";
return $tempStr;
}
//根据提供的类别号取得类别数组
function get_type_arr($type_no=0)
{
$temp_arr = $this->get_ads_type_arr();
$rootStr = $type_no==0 ? (isset($temp_arr['root'])?$temp_arr['root'] : "") : (isset($temp_arr[$type_no]['cid'])?$temp_arr[$type_no]['cid']:"");
$temp = explode(',',$rootStr);
$return_temp = array();
foreach($temp as $cid)
{
if(isset($temp_arr[$cid])) $return_temp[$temp_arr[$cid]['id']]=$temp_arr[$cid]['title'];
}
return $return_temp;
}
function make_ads_city()
{
$db = Factory::getDB("ads_city");
$tree =Factory::getItemClass('treeCache');
$tree->set($db);
$tree->tableName=$db->tableName;
$tree->pidStr ="ads_city_pid";
$tree->idStr = "ads_city_no";
$tree->title = "ads_city_name";
$tree->createArrayName ="ads_city_arr";
$tree->where = " ads_city_state=1 ORDER BY ads_city_no DESC ";
$tree->appendArr = array("ads_city_info");
$tree->createFileName = $this->ads_city_file;
$tree->is_utf8 = true;
return $tree->make();
}
function get_ads_city_arr()
{
return is_file($this->ads_city_file) ? require($this->ads_city_file) : array();
}
function get_ads_city_str()
{
$temp_arr = $this->get_ads_city_arr();
$treeArr = emptyempty($temp_arr) ? array() : $temp_arr;
$rootStr = isset($temp_arr['root']) ? $temp_arr['root'] : "";
$show_content = $this->__parseTree($treeArr,$rootStr,'pares_city_link');
return $show_content;
}
function pares_city_link($info)
{
$class_name = "ads_city";
$tempStr = "[<a href="/blog_article/c/{$class_name}/amp;m/add/amp;ads_city_pid/{$info[.html" href="/blog_article/c/{$class_name}/amp;m/add/amp;ads_city_pid/{$info[.html"id']}'>子-添加</a>]";
$tempStr .="[<a href="/blog_article/c/{$class_name}/amp;m/edit/amp;ads_city_no/{$info[.html" href="/blog_article/c/{$class_name}/amp;m/edit/amp;ads_city_no/{$info[.html"id']}'>编辑</a>]";
$tempStr .="[<a href="/blog_article/c/{$class_name}/amp;m/del/amp;ads_city_no/{$info[.html" href="/blog_article/c/{$class_name}/amp;m/del/amp;ads_city_no/{$info[.html"id']}'>删除</a>]";
return $tempStr;
}
//根据提供的城市号取得城市数组
function get_city_arr($city_no=0)
{
$temp_arr = $this->get_ads_city_arr();
$rootStr = $city_no==0 ? (isset($temp_arr['root'])?$temp_arr['root'] : "") : (isset($temp_arr[$city_no])?$temp_arr[$city_no]:"");
$temp = explode(',',$rootStr);
$return_temp = array();
foreach($temp as $cid)
{
if(isset($temp_arr[$cid])) $return_temp[$temp_arr[$cid]['id']]=$temp_arr[$cid]['title'];
}
return $return_temp;
}
function __parseTree($treeCache,$rootStr,$fuc_str)
{
$tempStr = "";
$temp = explode(',',$rootStr);
if(emptyempty($temp)) return "";
$this->layer=0;
foreach ($temp AS $cid)
{
if(isset($treeCache[$cid]))
{
$info = $treeCache[$cid];
$cidStr = $info['cid'];
//如果下面有子id
if($info["cid"]!="")
{
$tempStr .="<div >";
$tempStr .= str_repeat(' ',($info['depth']-1)*3);
if($info["cid"]!="")
{
//$tempStr .="<img src="/images/arrow.jpg" src="/blog_article/images/arrow.jpg" onClick='javascript:ShowMenu(\"l_{$info['id']}\");' id='pic_l_{$info['id']}'>";
}
//$tempStr .=">";
$tempStr.=$info['title'];
$tempStr .=$this->{$fuc_str}($info);
$tempStr .="</div>";
$tempStr .="<div id='l_{$info['id']}' >";
$tempStr .= $this->__parseTree($treeCache,$info['cid'],$fuc_str);
$tempStr .="</div>";
}else{
$this->layer++;
$tempStr .= str_repeat(' ',($info['depth']-1)*3);
$tempStr.=$info['title'];
$tempStr .=$this->{$fuc_str}($info);
if($this->isX==true)
{
if($this->layer % $this->rowSize ==0)
$tempStr .= "<br/>";
else
$tempStr .= " ";
}else{
$tempStr .= "<br/>";
}
}
}
}
return $tempStr;
}
}
?>
您可能感兴趣的文章:
php将地区分类排序的算法
php写的一个递归实现无限分类生成下拉列表的函数
php与mysql实现的无限级分类
php把无限级分类生成数组的类
php用递归方法实现无限级分类的代码
php实现的无极分类(递归)的代码
php 冒泡排序代码。
$arr = array(345,4,17,6,52,16,58,69,32,8,234);
for($i=1;$i<count($arr);$i++){
for($j=count($arr)-1;$j>=$i;$j--){
if($arr[$j]<$arr[$j-1]){
$temp = $arr[$j-1];
$arr[$j-1] = $arr[$j];
$arr[$j] = $temp;
}
}
}
?>
基本概念
冒泡排序的基本概念是:依次比较相邻的两个数,将小数放在前面,大数放在后面。即首先比较第1 个和第2个数,将小数放前,大数放后。然后比较第2个数和第3个数,将小数放前,大数放后,如此继续,直至比较最后两个数,将小数放前,大数放后。重复以上过程,仍从第一对数开始比较(因为可能由于第2个数和第3个数的交换,使得第1个数不再大于第2个数),将小数放前,大数放后,一直比较到最小数前的一对相邻数,将小数放前,大数放后,第二趟结束,在倒数第二个数中得到一个新的最小数。如此下去,直至最终完成排序。
由于在排序过程中总是小数往前放,大数往后放,相当于气泡往上升,所以称作冒泡排序。
用二重循环实现,外循环变量设为i,内循环变量设为j。外循环重复9次,内循环依次重复 9,8,…,1次。每次进行比较的两个元素都是与内循环j有关的,它们可以分别用a[j]和a[j+1]标识,i的值依次为1,2,…,9,对于每一个i, j的值依次为1,2,…10-i。
产生
在许多程序设计中,我们需要将一个数列进行排序,以方便统计,常见的排序方法有冒泡排序,二叉树排序,选择排序等等。而冒泡排序一直由于其简洁的思想方法和比较高的效率而倍受青睐。
排序过程
设想被排序的数组R[1..N]垂直竖立,将每个数据元素看作有重量的气泡,根据轻气泡不能在重气泡之下的原则,从下往上扫描数组R,凡扫描到违反本原则的轻气泡,就使其向上”漂浮”,如此反复进行,直至最后任何两个气泡都是轻者在上,重者在下为止。
您可能感兴趣的文章:
php冒泡排序的小例子
php 实现冒泡排序的简单例子
php 冒泡排序的实现代码
php 数组排序方法分享(冒泡排序、选择排序)
php冒泡排序之交换排序法
php冒泡排序(bubble sort)的例子
php冒泡排序算法一例
php冒泡排序与快速排序的例子
php冒泡排序算法一例。
$arr = array(345,4,17,6,52,16,58,69,32,8,234);
$n = count($arr);
for($i=1;$i<$n;$i++){
//其中的为什么$n-1是因为数组是从0开始计算的
//接下来是第一次内循环
for($j=$n-1;$j>=$i;$j--)
{
//如果$arr[10]<$arr[9];
//temp = $arr[9];
if($arr[$j]<$arr[$j-1]){
//$temp 暂时先把小的值放起来
$temp = $arr[$j-1];
//这个时候开始要交换位置了
$arr[$j-1] = $arr[$j];
//$arr[9] = $arr[10]的值
$arr[$j] = $temp;
//$arry[10]的值等于$arr[9]的值
//这个时候就要开始交换位置了
}
}
}
?>
您可能感兴趣的文章:
php冒泡排序的小例子
php 实现冒泡排序的简单例子
php 冒泡排序的实现代码
php 数组排序方法分享(冒泡排序、选择排序)
php冒泡排序之交换排序法
php冒泡排序(bubble sort)的例子
php实现冒泡排序算法的代码
php冒泡排序与快速排序的例子