php递归查询
答案:4 悬赏:60 手机版
解决时间 2021-03-28 10:50
- 提问者网友:書生途
- 2021-03-28 03:36
php递归查询
最佳答案
- 五星知识达人网友:夜风逐马
- 2021-03-28 04:25
function myfun($id)
{
$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; }
}
{
$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; }
}
全部回答
- 1楼网友:玩世
- 2021-03-28 07:29
like '%-1-%' or 其他条件
- 2楼网友:醉吻情书
- 2021-03-28 05:59
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追问这能运行?
$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追问这能运行?
- 3楼网友:老鼠爱大米
- 2021-03-28 05:52
1 2 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 ?>
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 ?>
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯