Jump to content

Function not returning variable


cmgmyr

Recommended Posts

Hey Guys,

I have no idea why this function is not returning the variable $new_cats, it's echoing correctly, but nothing is getting returned. Am I missing something??? Thanks!

 

Category Function:

function getCategories($cat_id, $level=0, $cats='') {  
	global $db;

	if($level == 0){
		$cats = array();
	}

	$sql = "SELECT parentid, name FROM categories WHERE id = $cat_id";
	$result = $db->query($sql);
	list ($p, $n) = $db->fetchRow($result);

	$cats[] = $n;

	if ($p != '0') $this->getCategories($p, $level+1, $cats);

	if($p == '0'){
		krsort($cats);			
		$new_cats = '';
		foreach($cats as $cat){
			$new_cats .= "$cat » ";
		}

		$new_cats = substr($new_cats, 0, -9);

		echo $new_cats;

		return $new_cats;
	}
}

 

test page:

if($cats = $catalog->getCategories(62)){
	echo $cats;
}else{
	echo '<br /><br />No cats!';
}

Link to comment
https://forums.phpfreaks.com/topic/96923-function-not-returning-variable/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.