菜单只显示第一个孩子的PHP

时间:2017-07-19 05:09:01

标签: php arrays recursion

我有这段代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string input = "{BEGIN:781}{hopi_docgen4}{sub_chronic_conditions_hpi}{END:}{OPh_cc_docgen}{END:621}{BEGIN:768}{cc_reviewed} {cc_rev_prov}{END:768}";
            string output = RemovePattern(input, 781);
        }

        static string RemovePattern(string input, int id)
        {
            string output = "";
            string beginPattern = string.Format("{0}BEGIN:{1}{2}", "{", id.ToString(), "}");
            string endPattern = string.Format("{0}END:{1}{2}", "{", id.ToString(), "}");


            int beginIndex = input.IndexOf(beginPattern);
            int endIndex = input.IndexOf(endPattern);
            if (endIndex == -1)
            {
                endPattern = "{END:}";
                endIndex = input.IndexOf(endPattern, beginIndex);
            }
            int lengthEnd = endPattern.Length;
            if ((beginIndex >= 0) && (endIndex >= 0))
            {
                int stringLength = (endIndex + lengthEnd) - beginIndex;

                output = input.Substring(0, beginIndex) + input.Substring(endIndex + lengthEnd);

            }

            return output;
        }
    }
}

并显示菜单我使用了这个递归函数:

$Maxcount = 10;$count = 1;$query =    "SELECT pages.paid,
     pages.pag_desc,
     pages.isActive,
     pages.pag_pag_id,
    pages.pag_type,
    parent.pag_desc,";while ($count <= $Maxcount) {$query .= "parent".$count.".pag_desc AS parent".$count;if ($count != $Maxcount) {$query .= ", ";$count++;} $query .= " FROM pages ";$query .= "LEFT JOIN pages parent ON pages.pag_pag_id = parent.paid ";$count = 1;while ($count <= $Maxcount) {   if ($count === 1) {
   $query .= "LEFT JOIN pages parent".$count." ON parent.pag_pag_id = parent".$count.".paid "; }else {
   $query .= "LEFT JOIN pages parent".$count." ON parent".($count-1).".pag_pag_id = parent".$count.".paid "  }$count++;}$query .="LEFT JOIN tblgroups_rights_gri on pages.paid = tblgroups_rights_gri.gri_pag_id 
     INNER JOIN users_usergroups on tblgroups_rights_gri.gri_usrg_id = users_usergroups.gid

     WHERE uid = '$uid' AND $where AND pages.isActive=1";

$result = $db->query($query);

if (mysqli_num_rows($result)) {
    $menu = array();
    while ($row = $result->fetch_assoc()) {
        $menu[] = $row;
    }

}

结果是它只显示父母和第一个孩子,但在数据库中孩子有另一个孩子等......

例如:

foreach($menu as $row){ $x[$row['parent3']][$row['parent2']][$row['parent1']][$row['pag_desc']]=$row['pag_desc']; }function recursive($x, $out) { if (is_array($x)){ //$out .= "<ul>"; foreach($x as $i => $data) { if ($i === '') { $out = recursive($data, $out); continue; } $out .= "<li>"; if (is_array($data)){ $out .= ' <a href="#" class="dropdown-toggle" data-toggle="dropdown">'; } else { $out .= '<a href="#">'; } $out .= $i; if (is_array($data)){ $out .= '<b class="caret"></b></a>'; } else { $out .= "</a>"; } if (is_array($data)){ $out .= "<ul class='dropdown-menu'>"; $out = recursive($data, $out); $out .= "</ul></li>"; } else { $out .= "</li>"; } } } return $out; } 有一个孩子user,管理员有另一个名为admin的孩子

但它仅显示editor作为父级,user作为子级

有什么帮助吗?谢谢你提前

0 个答案:

没有答案