function get_category($id){
創(chuàng)新互聯(lián)建站長期為上1000+客戶提供的網(wǎng)站建設(shè)服務(wù),團(tuán)隊(duì)從業(yè)經(jīng)驗(yàn)10年,關(guān)注不同地域、不同群體,并針對不同對象提供差異化的產(chǎn)品和服務(wù);打造開放共贏平臺,與合作伙伴共同營造健康的互聯(lián)網(wǎng)生態(tài)環(huán)境。為沙市企業(yè)提供專業(yè)的網(wǎng)站建設(shè)、網(wǎng)站制作,沙市網(wǎng)站改版等技術(shù)服務(wù)。擁有10年豐富建站經(jīng)驗(yàn)和眾多成功案例,為您定制開發(fā)。
$str=array();
//$sql = "select * from biao where id=$id";查詢節(jié)點(diǎn),自己寫吧
$result = array('id'=,'parent_id'=);//查詢結(jié)果一個數(shù)組格式
if($result){
$str = get_category($result['parent_id']);
$str[]=$result;
}
return $str;
}
}
調(diào)用get_category()就行了,$str第一個元素是節(jié)點(diǎn)本身,去掉就行了。
可以做樹狀結(jié)構(gòu),先查詢第一層,然后當(dāng)展開第二層
的時候,用ajax去查詢下一層的信息,如果感覺慢的
話可以給表做索引,也可以做分表處理,幾十萬不是
太大的數(shù)據(jù)
function?sumShuzi($tree,?$updateData?=?array())?{
$sum?=?0;
//?foreach($tree?as?$key?=?$item)?{??//這句話有毒
foreach($tree?as?$key?=?$item)?{
if(isset($item['children']))?{
$oldPshuzi?=?$tree[$key]['Pshuzi'];
$tree[$key]['Pshuzi']?=?sumShuzi($item['children'],?$updateData);
if($oldPshuzi?!=?$tree[$key]['Pshuzi'])?{
$updateData[$item['id']]?=?array($tree[$key]['Pshuzi'],?$tree[$key]['Pname_ch']);
}
}
$sum?+=?$tree[$key]['Pshuzi'];
}
return?$sum;
}
$tree?=?json_decode('[{"id":"1","Pid":"0","Pname_ch":"\u6e20\u9053\u90e8","Pshuzi":"1638000","children":[{"id":"4","Pid":"1","Pname_ch":"\u9500\u552e\u4e8c\u90e8","Pshuzi":"895000","children":[{"id":"13","Pid":"4","Pname_ch":"\u5468\u7ecf\u7406","Pshuzi":"28","children":[{"id":"28","Pid":"13","Pname_ch":"\u6e56\u5357","Pshuzi":"158000"},{"id":"35","Pid":"13","Pname_ch":"\u65b0\u7586","Pshuzi":"19000"}]},{"id":"40","Pid":"4","Pname_ch":"\u9648\u7ecf\u7406","Pshuzi":"5000"}]}]}]',?true);
//$tree是具有父子關(guān)系的數(shù)據(jù)樹
sumShuzi($tree,?$updateData);
foreach?($updateData?as?$id?=?$item)?{
$sql?=?"update?your_table?set?Pshuzi={$item[0]}?where?id={$id}";
mysqli_query($db,?$sql);????//$db是你的數(shù)據(jù)庫連接結(jié)果
}