php递归查询
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php递归查询相关的知识,希望对你有一定的参考价值。
他们的关系是 主导航 婚嫁->婚嫁下面1 ->商家,活动,妹纸.
如何通过递归给你id=1 找到下面的关联id
答案是1,2,3,4,7
大家给个思路或大概代码就好了,谢谢
$sql = "select `path` from `table` ";
$rs = mysql_query($sql);
while($rs =mysql_fetch_array($rs))
$arr[]=explode(\'-\',$rs);
$sql = "select $id from `table` ";
$rs = mysql_query($sql);
foreach($arr as $val)
if($val==mysql_fetch_array($rs))
echo $val;
// 以上不是递归做的 以下递归写的
function myfun($id)
$sql = "select `pid` from `table` ";
$rs = mysql_query($sql);
while($rs =mysql_fetch_array($rs))
$arr[]=$rs;
function myfun_2($id)
if($id<=count($arr))
foreach($arr as $val)
if($val!=$id)
echo $id_1.=myfun_1($id);
else
$id++;
return $id_1;
参考技术A function findnum($arr,$pid=0)
$result=array();
foreach($arr as $v)
if($v.['id']==$pid)
$v.['id']['child']=$v;
findnum($arr,$v.['id']);
$result[]=$v;
return $result;//将处理结果返回
其实你的path很有规律,用‘-’拆分后就是当前id的路径深度以及他的各个父类id
追问这能运行?
参考技术B 1 <?php2 require('includes/application_top.php');
3
4 function search_test($id,&$array)
5
6 $test_query = tep_db_query("select * from new_test where pid='".$id."'");
7 if(tep_db_num_rows($test_query) > 0)
8
9 while($test_array = tep_db_fetch_array($test_query))
10 $array[] = $test_array['id'];
11 search_test($test_array['id'],$array);
12
13
14
15
16 $array = array(1);
17 search_test(1,$array);
18 print_r($array);
19 ?> 参考技术C like '%-1-%' or 其他条件
php与mysql表中如何求递归求和汇总?
如图所示,数据表是递归数据,每个节点是所有下一级节点之和,如何用php&mysql操作数据表,使更新最后一级数据,前一级一级数据自动更新!?
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是你的数据库连接结果
参考技术A function get_str($id = 0)
global $str;
$sql = "select pname_ch,sum(pshuzi)as z from class where pid= $id group by pname_ch";
$result = mysql_query($sql);
if($result && mysql_affected_rows())
$str .= '<ul>';
while ($row = mysql_fetch_array($result))
$str .= "<li>" . $row['panem_ch'] . "--" . $row['z'] . "</li>";
get_str($row['id']);
$str .= '</ul>';
return $str;
给你个函数试试
如果显示,我早已解决这个问题,现在要倒推,有点麻烦!
每更新一个最后一个节点数据,所有与之有关的上一级节点数据要联动更新!当然需要数据库操作,这样后期数据比较好处理一点,如纯js操作,不好方便后期报表制作!
那就不止无五级,理论上是N>5级
参考技术C 先明确一下需求:1:如何显示数据?
2:是否需要数据库更新操作?追问
每更新一个最后一个节点数据,所有与之有关的上一级节点数据要联动更新!当然需要数据库操作,这样比较数据处理一点,如纯js操作,不好方便后期报表制作!
以上是关于php递归查询的主要内容,如果未能解决你的问题,请参考以下文章