PHP查数据递归 php的递归
PHP 递归处理会员数据
循环递归 function user_($fid){ str="select * from user_table where uid='$fid' "//SQL语句 $db=pg_query($str);//查询 while($row=pg_fetch_array($db))//循环 { $str.=user_($row['id']).',';//递归 }}没测试 大体的思路就这样 试试看
成都创新互联公司-成都网站建设公司,专注成都网站设计、做网站、成都外贸网站建设公司、网站营销推广,域名注册,虚拟主机,网站托管运营有关企业网站制作方案、改版、费用等问题,请联系成都创新互联公司。
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是你的数据库连接结果
}
php 递归问题,想要将数据按照无限极分类的样式显示出来
首先 将json 解码成数组, 用json_decode 函数 注意 一定要加上第二个参数 否则他会返回一个对象。接下来就是地递归了。这是一个最简单的递归只要逐个遍历即可。
下面是完整的代码:
$data= json_decode($str,true);
$options = getChildren($data);
function getChildren($parent,$deep=0) {
foreach($parent as $row) {
$data[] = array("id"=$row['id'], "name"=$row['name'],"pid"=$row['parentid'],'deep'=$deep);
if ($row['childs']) {
$data = array_merge($data, getChildren($row['childs'], $deep+1));
}
}
return $data;
}
?
select name="" id=""
?php foreach ($options as $row) { ?
option value="?php echo $row['id'] ?"?php echo str_pad("",$row['deep']*3, "-",STR_PAD_RIGHT); ??php echo $row['name']; ?/option
?php } ?
/select
以上代码已测试通过 效果图如下
php递归无限极分类怎么弄
给个函数你,调用get_category()就是一个数组格式的结果
function get_category($parent_id=0){
$arr=array();
$sql = "select * from category where parent_id=$parent_id";//查询子级数据
$result = array(a_object,b_object,,,)=sql_query($sql);//查询结果一个数组或列表格式,自己完善。
if($result){
foreach($result as $re){//循环数组
if(get_category($re.id))//如果子级不为空
$re['child'] = get_category($re.id);
$arr[] = $re;
}
return $arr;
}
本文标题:PHP查数据递归 php的递归
文章链接:http://ybzwz.com/article/hjpdco.html