简体   繁体   中英

Recursive navigation function in PHP (mssql -> sqlsrv problem)

Our site has a function that grabs all of the navigation items and the sub navigation items as follows:

function setNav($sectionID = 0) {
  $divs = $db->getData("SELECT * 
                          FROM table 
                         WHERE Section = ".$sectionID);
  foreach ($divs as $div) {
    $subArray = setNav($div['ID']);
    $thisArray[$div['ID']]['ID'] = $div['ID'];
    $thisArray[$div['ID']]['Title'] = $div['Title'];
    $thisArray[$div['ID']]['Children'] = $subArray;
  }

  return $thisArray;
}

It worked fine when we used the mssql_ functions, but since switching to a new version of PHP where that is depreciated, it's throwing up an error saying:

Fatal error: Maximum function nesting
level of '100' reached, aborting!

That message is coming from PHP, because you recursed into setNav too many times.

Unless you've got a navigation tree that ends up a hundred entries deep, I suspect something might be going wrong with your call to getData.

Can you make sure that it's returning valid entries?

Also, as a1ex07 says, you might be best using some kind of recursive query, rather than recursive calls to the non-recursive query, if possible.

This limit is set by xdebug. Check your php.ini and comment out the line that loads the xdebug library and then restart your web server.

Beyond that, you may wish to refactor your code to use nested sets in the future.

For SQL Server 2005 and higher you can write something like this:

$divs = $db->getData(" 
WITH tableCTE AS  
  (SELECT * 
    FROM table 
    WHERE Section = ".$sectionID. "
    UNION ALL 
    SELECT t2.* FROM tableCTE t1
    INNER JOIN table t2 ON (t2.id = t1.Section)
   )
 SELECT * FROM tableCTE
  "
);

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