无限级服务端数据组织方案的实现,提供解决方案,其中数据库查询可替换为List的方式查找等其它方式。

function queryAllSubCustomers($cstId) {
$sqlA="SELECT ID, Name, ParentID FROM T_CustomerInfo WHERE ID = $cstId ";
$custList=Sql_Query($sqlA);
$AllCustInfs=array();

$result=array();while(count($custList) > 0) { // 组建当前客户列表,获取所有id $countNum = count($custList); $subCustIds=" ("; //print_r($custList); for ($i=0; $i<$countNum; $i++) { $custInf = $custList[$i]; $node=array(); $node['id']=$custInf['ID']; $node['text']=$custInf['Name']; array_push($AllCustInfs,$node); $subCustIds = $subCustIds.$custInf['ID']."," ; } $subCustIds = substr($subCustIds,0,-1).") "; // 查询下级客户 $sql="SELECT ID, Name, ParentID FROM T_CustomerInfo WHERE ParentID <> 0 AND ParentID IN $subCustIds "; unset($subCustIds); unset($custList); $custList=Sql_Query($sql); //print_r($custList);}return $AllCustInfs;

}