Re: Problem returning array from function
Ok Sanfly, let's walk through the cases of calling the makeBreadcrumb function: 1) with $id = 1 and it has no parent; 2) with $id = 2 and it has one parent (9); Case 1: 1) Parameters: $array(0) = 1, $id = 1; 2) Statements: $parent = $this->GallerysCat->find("GallerysCat.id = '$id'", "parent"); $parent = $parent['GallerysCat']['parent']; results in $parent = 0; 3) Enters: if ( $parent == 0 ) { 4) Reverses array $reverse = array_reverse($array); results in $reverse(0) = 1; 5) Returns reversed array return $reverse; results in return of $reverse(0) = 1; 6) END OF CASE 1 - no problems! Case 2: 1) Parameters: $array(0) = 2, $id = 2; 2) Statements: $parent = $this->GallerysCat->find("GallerysCat.id = '$id'", "parent"); $parent = $parent['GallerysCat']['parent']; results in $parent = 9; 3) Skips: if ( $parent == 0 ) { 4) Enters: else{ 5) Adds parent to $array: $array[] = $parent; results in $parent(0) = 2, $parent(1) = 9; 6) Calls recursively: $this->makeBreadcrumb($array, $parent); 6.1) Parameters: $parent(0) = 2, $parent(1) = 9, $id = 9; 6.2) Statements: $parent = $this->GallerysCat->find("GallerysCat.id = '$id'", "parent"); $parent = $parent['GallerysCat']['parent']; results in $parent = 0; 6.3) Enters: if ( $parent == 0 ) { 6.4) Reverses array $reverse = array_reverse($array); results in $reverse(0) = 9, $reverse(1) = 2; 6.5) Returns reversed array return $reverse; results in return of $reverse(0) = 9, $reverse(1) = 2; 6.6 END OF RECURSIVE CALL!!! 7) END OF CASE 2 Now do you see that after making the recursive call to the function, the returned result is not being used and thus you are not returning it! Suggested change in the else block: return ( $this->makeBreadcrumb($array, $parent) ); When you are using recursive calls, each call has it own scope of variables, so $array is not the same in both!!! Enjoy, John On Jul 1, 9:06 am, Sanfly wrote: > Oh, and Ive set my debug to 2 and dont get any errors > > The problem is not building the array, the problem is returning the > built array > --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "CakePHP" group. To post to this group, send email to cake-php@googlegroups.com To unsubscribe from this group, send email to cake-php+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php?hl=en -~--~~~~--~~--~--~---
Re: Problem returning array from function
Oh, and Ive set my debug to 2 and dont get any errors The problem is not building the array, the problem is returning the built array On Jun 29, 9:04 pm, John Andersen wrote: > Assuming you are passing anarraywith one ID, then if I understand > your problem correctly, a parent will be read from the database and as > it will not be 0, thefunctionwill enter the else statement. > > There is will add the parent ID to thearrayand then pass thisarray > plus the parent ID via a recursive call to thefunction. > > In the (recursive)functiona parent will be read and it will be 0, > thus entering into the main if statement, where thearrayis reversed, > and returned ... > > ... returning to thefunction... ups, the returned (reversedarray) > is lost in the null land, because no variable is defined to receive > id! And the $arraywill just contain the values as before the call > (recursive) to thefunction. > > And furthermore, I don't see any statement returning anything from > hereon! > > Please check your code !! Turn on Debug, add debug statements and try > again! > Enjoy, > John > > On Jun 28, 11:53 pm, Sanfly wrote: > > > No, I dont think so. I have no trouble building thearraybecause > > this line: > > > print_r($reverse); echo ""; > > > does print the entire correctarray. Its returning it from the > >functionwhen there is more than one value in thearraythat gives me > > a problem. --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "CakePHP" group. To post to this group, send email to cake-php@googlegroups.com To unsubscribe from this group, send email to cake-php+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php?hl=en -~--~~~~--~~--~--~---
Re: Problem returning array from function
Im sorry, I really dont get this > the returned (reversedarray) > is lost in the null land, because no variable is defined to receive > id! If $id = 0 I dont need it to be added to the array - the array is now complete so I want to take the array ive built to date and exit the function > And the $arraywill just contain the values as before the call > (recursive) to thefunction. Thats just what I want > And furthermore, I don't see any statement returning anything from > hereon! Shouldn't return $reverse;be enough to return my array? On Jun 29, 9:04 pm, John Andersen wrote: > Assuming you are passing anarraywith one ID, then if I understand > your problem correctly, a parent will be read from the database and as > it will not be 0, thefunctionwill enter the else statement. > > There is will add the parent ID to thearrayand then pass thisarray > plus the parent ID via a recursive call to thefunction. > > In the (recursive)functiona parent will be read and it will be 0, > thus entering into the main if statement, where thearrayis reversed, > and returned ... > > ... returning to thefunction... ups, the returned (reversedarray) > is lost in the null land, because no variable is defined to receive > id! And the $arraywill just contain the values as before the call > (recursive) to thefunction. > > And furthermore, I don't see any statement returning anything from > hereon! > > Please check your code !! Turn on Debug, add debug statements and try > again! > Enjoy, > John > > On Jun 28, 11:53 pm, Sanfly wrote: > > > No, I dont think so. I have no trouble building thearraybecause > > this line: > > > print_r($reverse); echo ""; > > > does print the entire correctarray. Its returning it from the > >functionwhen there is more than one value in thearraythat gives me > > a problem. --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "CakePHP" group. To post to this group, send email to cake-php@googlegroups.com To unsubscribe from this group, send email to cake-php+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php?hl=en -~--~~~~--~~--~--~---
Re: Problem returning array from function
Assuming you are passing an array with one ID, then if I understand your problem correctly, a parent will be read from the database and as it will not be 0, the function will enter the else statement. There is will add the parent ID to the array and then pass this array plus the parent ID via a recursive call to the function. In the (recursive) function a parent will be read and it will be 0, thus entering into the main if statement, where the array is reversed, and returned ... ... returning to the function ... ups, the returned (reversed array) is lost in the null land, because no variable is defined to receive id! And the $array will just contain the values as before the call (recursive) to the function. And furthermore, I don't see any statement returning anything from hereon! Please check your code !! Turn on Debug, add debug statements and try again! Enjoy, John On Jun 28, 11:53 pm, Sanfly wrote: > No, I dont think so. I have no trouble building the array because > this line: > > print_r($reverse); echo ""; > > does print the entire correct array. Its returning it from the > function when there is more than one value in the array that gives me > a problem. > --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "CakePHP" group. To post to this group, send email to cake-php@googlegroups.com To unsubscribe from this group, send email to cake-php+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php?hl=en -~--~~~~--~~--~--~---
Re: Problem returning array from function
No, I dont think so. I have no trouble building the array because this line: print_r($reverse); echo ""; does print the entire correct array. Its returning it from the function when there is more than one value in the array that gives me a problem. On Jun 26, 8:23 pm, John Andersen wrote: > In the else statement, are you sure you don't need to do something > with the returnedarrayfrom $this->makeBreadcrumb(...)? > > Enjoy, > John > > On Jun 26, 1:08 am, Sanfly wrote: > > > Hi All > > > I have afunctionin a component. Basically the aim of it is to > > create a breadcrumb menu eg: Gallery > 2009 Photos > Winter Games > > > My problem is that if there is more than one element/value in the > >arrayit doesnt seem to return anything. I know that thearrayis > > being generated though because I can print_r it out within the > >function. It seems to be getting lost somewhere between the component > > and controller > > > In My controller: > > > $breadCrumbs = $this->GalleryFunctions->makeBreadcrumb(array($id), > > $id); > > print_r($breadCrumbs); > > > In My Component: > > > functionmakeBreadcrumb($array, $id){ > > > // find the parent of the cat > > $parent = $this->GallerysCat->find("GallerysCat.id = '$id'", > > "parent"); > > $parent = $parent['GallerysCat']['parent']; > > if($parent == 0){ > > $reverse = array_reverse($array); > > print_r($reverse); echo ""; > > return $reverse; > > > } > > else{ > > $array[] = $parent; > > $this->makeBreadcrumb($array, $parent); > > } > > } > > > Any Ideas? --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "CakePHP" group. To post to this group, send email to cake-php@googlegroups.com To unsubscribe from this group, send email to cake-php+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php?hl=en -~--~~~~--~~--~--~---
Re: Problem returning array from function
In the else statement, are you sure you don't need to do something with the returned array from $this->makeBreadcrumb(...)? Enjoy, John On Jun 26, 1:08 am, Sanfly wrote: > Hi All > > I have a function in a component. Basically the aim of it is to > create a breadcrumb menu eg: Gallery > 2009 Photos > Winter Games > > My problem is that if there is more than one element/value in the > array it doesnt seem to return anything. I know that the array is > being generated though because I can print_r it out within the > function. It seems to be getting lost somewhere between the component > and controller > > In My controller: > > $breadCrumbs = $this->GalleryFunctions->makeBreadcrumb(array($id), > $id); > print_r($breadCrumbs); > > In My Component: > > function makeBreadcrumb($array, $id){ > > // find the parent of the cat > $parent = $this->GallerysCat->find("GallerysCat.id = '$id'", > "parent"); > $parent = $parent['GallerysCat']['parent']; > if($parent == 0){ > $reverse = array_reverse($array); > print_r($reverse); echo ""; > return $reverse; > > } > else{ > $array[] = $parent; > $this->makeBreadcrumb($array, $parent); > } > } > > Any Ideas? --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "CakePHP" group. To post to this group, send email to cake-php@googlegroups.com To unsubscribe from this group, send email to cake-php+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php?hl=en -~--~~~~--~~--~--~---
Problem returning array from function
Hi All I have a function in a component. Basically the aim of it is to create a breadcrumb menu eg: Gallery > 2009 Photos > Winter Games My problem is that if there is more than one element/value in the array it doesnt seem to return anything. I know that the array is being generated though because I can print_r it out within the function. It seems to be getting lost somewhere between the component and controller In My controller: $breadCrumbs = $this->GalleryFunctions->makeBreadcrumb(array($id), $id); print_r($breadCrumbs); In My Component: function makeBreadcrumb($array, $id){ // find the parent of the cat $parent = $this->GallerysCat->find("GallerysCat.id = '$id'", "parent"); $parent = $parent['GallerysCat']['parent']; if($parent == 0){ $reverse = array_reverse($array); print_r($reverse); echo ""; return $reverse; } else{ $array[] = $parent; $this->makeBreadcrumb($array, $parent); } } Any Ideas? --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "CakePHP" group. To post to this group, send email to cake-php@googlegroups.com To unsubscribe from this group, send email to cake-php+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php?hl=en -~--~~~~--~~--~--~---