十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
function get_category($id){
创新互联建站长期为上1000+客户提供的网站建设服务,团队从业经验10年,关注不同地域、不同群体,并针对不同对象提供差异化的产品和服务;打造开放共赢平台,与合作伙伴共同营造健康的互联网生态环境。为沙市企业提供专业的网站建设、网站制作,沙市网站改版等技术服务。拥有10年丰富建站经验和众多成功案例,为您定制开发。
$str=array();
//$sql = "select * from biao where id=$id";查询节点,自己写吧
$result = array('id'=,'parent_id'=);//查询结果一个数组格式
if($result){
$str = get_category($result['parent_id']);
$str[]=$result;
}
return $str;
}
}
调用get_category()就行了,$str第一个元素是节点本身,去掉就行了。
可以做树状结构,先查询第一层,然后当展开第二层
的时候,用ajax去查询下一层的信息,如果感觉慢的
话可以给表做索引,也可以做分表处理,几十万不是
太大的数据
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是具有父子关系的数据树
sumShuzi($tree, $updateData);
foreach ($updateData as $id = $item) {
$sql = "update your_table set Pshuzi={$item[0]} where id={$id}";
mysqli_query($db, $sql); //$db是你的数据库连接结果
}