0

I'm struggling to pass a value from a recursive function into an array in a different method. I have written my recursive function below.

function get_parent_basket_id($parent_id) {            

    if($parent_id==NULL){
        return false;
    }

    $baskets = $this->Model_cds_clients->clients($parent_id)->result();

    if(count($baskets)==0){
        return false;
    }

    foreach ($baskets as $row) {    

        if($row->parent_id==0){
            return $row->id;
       } else {
           $this->get_parent_basket_id($row->parent_id);
       }    
    } 
}

My Model_cds_clients code is below

function clients ($parent_id) {
    $this->db->select('endpoint, parent_id');
    $this->db->from('cds_clients');
    $this->db->where('parent_id', $parent_id);
    $this->db->where('active', 1);                
    $result = $this->db->get();

    return $result;
}

My table structure for my model looks like below

id - int(11)
name - varchar(255)
endpoint - varchar(64)
active - tinyint(1)
parent_id - int(11)

Below is my function where I need to pass the variable into the client_id under the endpoints array.

public function __construct() {

    parent::__construct();
    $this->load->model('Model_story'); 

    $this->get_parent_basket_id($parent_id);

    $data = array(
        'basket_id' => $this->basketId,
        'story_id' => $this->storyId,
        'payload' => $this->xmlString,                 
        'endpoints' => array(
                           array(
                               'client_id' => XXXX,
                               'endpoint_url' => 'http://www.example.com/consume.php'
                           ), )
    );

    $json_payload = json_encode($data);

Please help.

jaahvicky
  • 438
  • 2
  • 8
  • 20

2 Answers2

1

I'm not sure if this is what you are looking for, but it's what sticks out to me

Change:

foreach ($baskets as $row) {    

    if($row->parent_id==0){
        return $row->id;
   } else {
       $this->get_parent_basket_id($row->parent_id);
   }    
} 

to:

foreach ($baskets as $row) {    

    if($row->parent_id==0){
        return $row->id;
   } else {
      return $this->get_parent_basket_id($row->parent_id);
   }    
} 

in your recursive function. This way the id will actually get passed back.

Then, in your controller, assign the result to a variable i.e.

$parent_basket_id = $this->get_parent_basket_id($parent_id);

However, it doesn't look like you're declaring/passing $parent_id to __construct() so I'm not sure how this is meant to work.

Hope this help!

Rwd
  • 34,180
  • 6
  • 64
  • 78
1

Your tail recursion's base case doesn't look quite right. Your direct call needs to return the results. I have re-factored your method as follows:

function get_parent_basket_id($parent_id) {            

    if(!$parent_id){

        return false;

    }

    // Your method chaining is missing brackets on $this->Model_cds_clients()
    $baskets = $this->Model_cds_clients()->clients($parent_id)->result();

    if(!count($baskets)){

        return false;

    }

    foreach ($baskets as $row) {    
    
        if(!$row->parent_id){

            return $row->id;

        } else {

        // Add a return here
        return $this->get_parent_basket_id($row->parent_id);

       }    
    } 
}
General Grievance
  • 4,555
  • 31
  • 31
  • 45
user141048
  • 21
  • 2