$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是你的数据库连接结果
}
function style($id,$i){static $i
$sql="select * from sort where id=".$id
$rs1=mysql_query($sql)
while($rsd=mysql_fetch_array($rs1)){
style($rsd['type_id'],++$i)
}
return $i
}
第一,没有选择数据库,在连接数据库后添加mysql_select_db("数据库名")第二、我感觉不代码不全吧,因为我拷下来看了一下,少一},当然上面那个没少,补充的少了。
第三、还有没看出这个$ii是干什么用的。
第四、没感觉$selec=0与不等于0输出有什么不同。
第五、以后最好是把html代码与程序分开写。
因为不知道需求,只能根据你的程序简单修改如下:
<?php
function TreeMenu($selec) {
$Conn=mysql_connect("localhost","kk","kk123")
mysql_select_db("数据库名")
$ii=1
$sql="select * from tvmenu where bid=".$selec
$result=mysql_query($sql,$Conn)
$str = ""
while($row =mysql_fetch_array($result)) {
if($selec=0) {
$str = '<option value="'.$row['id'].'">'.$row['name'].'></option>'
}else {
$str = '<option value="'.$row['id'].'">'.$row['name'].'></option>'
}
echo $str
$ii++
TreeMenu($row['id'])
$ii--
}
}
?>
<select name="bid" id="bid">
<?php
TreeMenu(0)
?>
</select>
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)