cmgmyr Posted March 19, 2008 Share Posted March 19, 2008 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!'; } Quote Link to comment https://forums.phpfreaks.com/topic/96923-function-not-returning-variable/ Share on other sites More sharing options...
sKunKbad Posted March 19, 2008 Share Posted March 19, 2008 echo $new_cats instead of $cats on your test page or after return. Quote Link to comment https://forums.phpfreaks.com/topic/96923-function-not-returning-variable/#findComment-495941 Share on other sites More sharing options...
cmgmyr Posted March 19, 2008 Author Share Posted March 19, 2008 But if I'm setting the returned value = $cats there shouldn't be any reason why it's not showing up. I even tried doing "return true;" instead of the variable and it still didn't return anything. Quote Link to comment https://forums.phpfreaks.com/topic/96923-function-not-returning-variable/#findComment-495950 Share on other sites More sharing options...
trq Posted March 19, 2008 Share Posted March 19, 2008 What does the echo $new_cats; produce? Quote Link to comment https://forums.phpfreaks.com/topic/96923-function-not-returning-variable/#findComment-495962 Share on other sites More sharing options...
cmgmyr Posted March 19, 2008 Author Share Posted March 19, 2008 the echo is just to see if the function is working but for 62 it echos "Anniversary & Wedding » Diamond » Center Stone". I tried commenting out the echo statement, but that didn't change anything. Quote Link to comment https://forums.phpfreaks.com/topic/96923-function-not-returning-variable/#findComment-495967 Share on other sites More sharing options...
Sulman Posted March 19, 2008 Share Posted March 19, 2008 If it doesn't return anything at all even when you return true then the problem lies here: <?php if($p == '0'){ ?> Are you sure that this if statement is evaluating correctly (is $p always '0')? Quote Link to comment https://forums.phpfreaks.com/topic/96923-function-not-returning-variable/#findComment-495971 Share on other sites More sharing options...
trq Posted March 19, 2008 Share Posted March 19, 2008 If thats what is being echo'd then theres no reason its not being returned. Sorry. Quote Link to comment https://forums.phpfreaks.com/topic/96923-function-not-returning-variable/#findComment-495974 Share on other sites More sharing options...
cmgmyr Posted March 19, 2008 Author Share Posted March 19, 2008 @ Sulman - if it wasn't correct the I wouldn't be seeing the echo statement either @ thorpe - that's what I thought too but for some reason it's not, I'll keep messing with it. Thanks guys Quote Link to comment https://forums.phpfreaks.com/topic/96923-function-not-returning-variable/#findComment-495980 Share on other sites More sharing options...
lemmin Posted March 19, 2008 Share Posted March 19, 2008 What does the test page produce? Does it say no cats or is it blank or something? Quote Link to comment https://forums.phpfreaks.com/topic/96923-function-not-returning-variable/#findComment-495988 Share on other sites More sharing options...
cmgmyr Posted March 19, 2008 Author Share Posted March 19, 2008 it echos "No cats!" Quote Link to comment https://forums.phpfreaks.com/topic/96923-function-not-returning-variable/#findComment-495989 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.