例子,php递归算法实例。
复制代码 代码示例:
<?php
function Type($tid) {
$rs = $this->conn->Execute("SELECT * FROM protype where id=$tid");
if ($rs) {
$i = 0;
$type[] = array();
while (!$rs->EOF) {
$type[$i] = $rs->fields;
$type[$i]['child'] = $this->GetClass($type[$i]['id']);
$i++;
$rs->MoveNext();
}
}
return $type;
}
function GetClass($tid) {
$rs = $this->conn->Execute("SELECT * FROM proclass where tid=$tid");
if ($rs) {
$i = 0;
$class[] = array();
while (!$rs->EOF) {
$class[$i] = $rs->fields;
$class[$i]['child'] = $this->GetPro($class[$i]['id']);
$i++;
$rs->MoveNext();
}
}
return $class;
}
function GetGroup($cid) {
$rs = $this->conn->Execute("SELECT * FROM progroup where cid=$cid");
if ($rs) {
$i = 0;
$group[] = array();
while (!$rs->EOF) {
$group[$i] = $rs->fields;
$group[$i]['child'] = $this->GetPro($group[$i]['id']);
$i++;
$rs->MoveNext();
}
}
return $group;
}
function GetPro($cid) {
$rs = $this->conn->Execute("SELECT * FROM prolist where cid=$cid limit 5");
if ($rs) {
$i = 0;
while (!$rs->EOF) {
$plist[$i] = $rs->fields;
$i++;
$rs->MoveNext();
}
}
return $plist;
}
function PageHead() {
$this->GetAdvit();
$rs = $this->conn->Execute("SELECT * FROM protype");
if ($rs) {
$i = 0;
$type[] = array();
while (!$rs->EOF) {
$type[$i] = $rs->fields;
$type[$i]['child'] = $this->GetClass($type[$i]['id']);
$i++;
$rs->MoveNext();
}
}
$this->smarty->assign("alltype", $type);
$rs = $this->conn->Execute("SELECT * FROM province");
if ($rs) { //递归算法 www.jb200.com
$i = 0;
while (!$rs->EOF) {
$pvc[$i] = $rs->fields;
$i++;
$rs->MoveNext();
}
}
$this->smarty->assign("pvc", $pvc);
$this->smarty->display("head.html");
}