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!
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.
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.
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
"
);
精彩评论