Discuss Help with recursive function in the PHP Development forum on Dev Shed. Help with recursive function PHP Development forum discussing coding practices, tips on PHP, and other PHP-related topics. PHP is an open source scripting language that has taken the web development industry by storm.
The ASP Free website provides in-depth information on the latest developer tools available from Microsoft. Our cadre of writers, highly experienced industry experts, reveals the best ways to use established technologies as well as new and emerging technologies. Our coverage of Microsoft's development and administration technologies is among the most respected in the IT industry today.
ASP Free and Iron Speed Designer are giving away $5,500+ in FREE licenses. Iron Speed's RAD CASE toolset can save up to 80% of your coding time. One free license per week, one perpetual license per month! Download and Activate to enter!
Intel® Graphics Performance Analyzers is a powerful tool suite for analyzing and optimizing your games, media, and graphics-intensive applications. Used by some of the best developers on the planet, Intel GPA lets you maximize your app’s performance.
Posts: 73
Time spent in forums: 1 Day 40 m 29 sec
Reputation Power: 11
Help with recursive function
Hi,
I have categorys and subcategories in one table with a parent/child relationship.
I want to be able to determine the path UP to the top level category from any sublevel category. And develop an array of subcategories along the way up to top level
I have the code below *sort of* working but not completely, and I wondered if anyone might have a moment to point me in the right direction
thank you
my code:
PHP Code:
//main recursive function
function getRootCat($subcatID) {
$sql = "SELECT subcat.name as subcatName, cat.ID as catID, cat.name as catName
FROM t_category cat
INNER JOIN t_category subcat ON subcat.parent_ID = cat.ID
WHERE subcat.ID = $subcatID
";
//checkRootCat queries the db to see if
//the current category is the main category
//it checks by seeing if the ID is equal to the parentID
function checkRootCat($catID) {
$sql = "SELECT cat.ID
FROM t_category cat
WHERE cat.ID = $catID
AND cat.parent_ID = $catID
";
Posts: 66
Time spent in forums: 20 h 15 m 58 sec
Reputation Power: 7
PHP Code:
/**
* Example recursion function for cPath
*
* This short example was created for wjones8050 from the DevShed community.
*
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
* @version 1.0
* @link http://www.oscommerce-freelancers.com/ osCommerce-Freelancers
* @copyright Copyright 2005, Bobby Easland
* @author Bobby Easland
*/
function categoryPathRecursive($catID, &$cPath){
// Sanity check to make sure we don't run the function
// on the top level
if ($catID == '0') return;
// Initialize the SQL
$sql = "SELECT ID, parent_ID, name
FROM t_category
WHERE ID = '" . (int)$catID . "'
LIMIT 1";
// Execute the query, catch any errors
$query = mysql_query($sql) or trigger_error(mysql_errno() . ': ' . mysql_error());
// Sanity check to make sure there were rows returned
if ( mysql_num_rows($query) > 0 ){
// Fetch the array associatively
$result = mysql_fetch_array($query, MYSQL_ASSOC);
// Store the result in the $cPath referenced array
$cPath[ $result['ID'] ] = $result['name'];
// If the parent_ID is greater than zero (not top level)
// send it through the recursion function
if ( $result['parent_ID'] > 0 ){
categoryPathRecursive($result['parent_ID'], $cPath);
else {
// The parent_ID === 0 so we should just break the recursion
return;
}
} else {
// There were no rows returned so break the recursion
return;
}
} # end function
Notice that the cPath parameter is passed by reference!
Posts: 73
Time spent in forums: 1 Day 40 m 29 sec
Reputation Power: 11
Wow! that is awesome! thanks so much for the help, really nice piece of scripting! I'm not advanced enough to know about passing by reference, but I need to check that out a bit to understand how you got that to work so well.
Posts: 66
Time spent in forums: 20 h 15 m 58 sec
Reputation Power: 7
It's not advanced nor is it my best work However, it demonstrates 1 method of implementing a recursion function. Another method would be to return the data instead of using referenced variables. I find that the referenced parameter recursion functions are easier to use but your mileage may vary.
Another very important aspect of creating recursion functions is to make damn sure you don't get into an infinite loop. Use recursion break constructs (return, break, continue, etc) as liberally as possible!
Enjoy!
Bobby
Last edited by Bobby_Easland : September 28th, 2005 at 09:38 AM.
Reason: Spelling correction