0

i created recursive function in magento which give string out at last .

i had created custom customer attributes, every time a new customer is registered he is registered using attributes from its parent customer some thing link MLM.

i created custom attribute refcode which is id of customer (refcode of customer at 0 level is admin). When i register new customer ref code is needed to already registered customer like 1 ,2 ,4

below is senario

customer | ID | Ref code
A           1      admin
B           2        1
C           3        1
D           4        3

now give customer D i want to get all parent customer id's

for customer D i want output 4->3->1->admin

i wrote below code Case 1

function getallrefcode($id,$string){
   if($id=='admin'){
     return $string;
   }else{

     $customerObj  = Mage::getModel('customer/customer')->load($id);
     $attr = $customerObj->getData('refcode');
     $newstring=$string.'->'.$attr ;
     getallrefcode($attr ,$newstring);
   }
} 

echo getallrefcode('4','');

i get blank value why so ,

but for below code Case 2

function getallrefcode($id,$string){
  if($id=='admin'){
    return $string;
}else{

  $customerObj  = Mage::getModel('customer/customer')->load($id);
  $attr = $customerObj->getData('refcode');
  echo $newstring=$string.'->'.$attr ;
  getallrefcode($attr ,$newstring);
}
} 

getallrefcode('4','');

i get output as "4->3->1->admin"

what i am missing with case I why return is not giving output

2

You should put return statement before calling getallrefcode($attr ,$newstring); inside getallrefcode

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.