简体   繁体   中英

How to generate breadcrumbs using recursion on database row data?

I have to make a breadcrumb menu which comes database.

So made this function

function file_list($path) {
    $result = array();
    $q = "SELECT staticTitle,staticId,parentId FROM tbl_static_pages WHERE staticId = $path";
    $run = mysql_query($q);
    while($row = mysql_fetch_array($run)) {
        if($row['parentId'] > 1) {
            echo $row['staticTitle'];
            $result = file_list($row['parentId']);
            return $result; // INSERTED
        }else { 
            return $result; 
        }  
    }

I have a database structure like this:

id | parentId | title
 3  |  1      | keyword
 28 |  3      | xxx
 31 | 28      | business

I want to output like this business -> xxx -> keyword

I want to run this function until $row['parentId'] = 1 .
When I echo the title, I got correct result.
When I try it to store it in array, I always get single value.

How can I return an array in recursive array?

Try this:

function file_list($path)
{
    $result = array();
    $q = "SELECT staticTitle,staticId,parentId FROM tbl_static_pages WHERE staticId = $path";
    $run = mysql_query($q);

    $results = array();
    while ($row = mysql_fetch_array($run))
    {
        if ($row['parentId'] > 1)
        {
            echo $row['staticTitle'];
            $results = array_merge($results, file_list($row['parentId']));
        }
        else
        { 
            $results[] = $result; 
        }
    }  
    return $results;
}

Few things that are not clear from your question:

  • Breadcrumbs are usually not recursive, so I suppose you do NOT want to actually return a recursive array (array where every element is again array). If you want, follow comment by @midhunjose
  • Do you expect the query to ever return multiple records?
  • Swap the order of array_merge arguments, if you want the parent before child in resulting array.

I REALLY don't like the idea of recursive calls on the database. If this is a "breadcrumbs" database table, how big could it possibly be? ...I mean size is probably not a concern.

I would like to suggest that you pull the full table out (3 columns, all rows) and assign the resultset to a php variable. This means you only ever make one trip to the database -- and that's a good thing.

Code: ( Demo )

function breadcrumber($array,$id){
    static $result=[];  // declare the storage variable without losing elements during recursion
    if(isset($array[$id])){  // if target exists
        $result[]=$array[$id]['title'];  // store title text
        $parent=$array[$id]['parentId'];  // assign new target
        unset($array[$id]);  // remove possibility of an infinite loop
        breadcrumber($array,$parent);  // recurse
    }
    return $result;
}

$resultset=[
    ['id'=>3,'parentId'=>1,'title'=>'keyword'],
    ['id'=>28,'parentId'=>3,'title'=>'xxx'],
    ['id'=>31,'parentId'=>28,'title'=>'business']
];

echo implode(' -> ',breadcrumber(array_column($resultset,NULL,'id'),31));
//                               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^-assign associative keys for easy lookup

Output:

business -> xxx -> keyword

...and here is another demo using your second set of data

Try this:

$result[] = file_list($row['parentId']);

instand of

$result = file_list($row['parentId']);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM