-1

I have below recursive function in my model with CI, working. In my controller, i need to check if function worked correctly like:

if($this->my_model->level_corrector($id_page,$level)) echo 'Levels are corrected';

But as the function always return false (to end the recursion), I couldn't figure out how to achieve my goal.

  function level_corrector($id_page_of_parent,$level_of_parent)
  {
    $sql = "
    SELECT id_page, id_parent, level
    FROM page 
    WHERE id_parent = $id_page_of_parent";
    $query = $this->db->query($sql);

    if($query->num_rows() > 0)
    {
      $result = $query->result_array();          
      foreach ($result as $r)
      {
        $data = array('level'=>$level_of_parent+1);
        $this->db->where('id_page', $r['id_page']);
        if($this->db->update('page', $data))
        {
           $this->level_corrector($r['id_page'],$level_of_parent+1);
        }
        else
        {
           // let me handle it what to do
           return false;
        }
      }
    }
    else
    { //  again let me handle it to log a message or sth
       return false; // (2)
    }
    return true; // (3) means it all gone right, so I can move on.
  }
  • 3
    Careful with this line: `if($this->db->update('page', $data));`. Either remove the conditional, or remove the semicolon :) – Cam Mar 21 '12 at 18:37
  • 1
    @Cam, I am calling this function inside `$this->db->trans_start()`. So I think I just need to get a feedback from the function for 3 possible condition. if error, return false; if, query empty in the last loop, return false so stop the recursion; if not all of them means all gone right, so return true. Let me know if I am mistaken in any aspect. –  Mar 21 '12 at 18:44

1 Answers1

2

You need to do two things:

  • If there is an error, return false. Otherwise, at the end of the function, return true by default

  • If, when you call the function recursively, an error occurred, return false.

Edit: Based on your answers to my questions in the comments, what you want is this:

function level_corrector($id_page_of_parent,$level_of_parent)
{
  $sql = "
  SELECT id_page, id_parent, level
  FROM page 
  WHERE id_parent = $id_page_of_parent";
  $query = $this->db->query($sql);

  if($query->num_rows() > 0)
  {
    $result = $query->result_array();          
    foreach ($result as $r)
    {
      $data = array('level'=>$level_of_parent+1);
      $this->db->where('id_page', $r['id_page']);
      if($this->db->update('page', $data))
      {
         // no error. return error code from recursive call
         return $this->level_corrector($r['id_page'],$level_of_parent+1);
      }
      else
      {
         // error occured
         return false;
      }
    }
  }

  // $query->num_rows <= 0. This is not an error, so return true:
  return true;
}
Cam
  • 14,930
  • 16
  • 77
  • 128
  • Added a comment above i guess saying the same thing. Can you demonstrate it as I started to see $ signs everywhere. –  Mar 21 '12 at 18:45
  • @yahyaE: Can you specify exactly the condition inside your function that should cause 'return false' to be returned all the way up to the original call? – Cam Mar 21 '12 at 18:47
  • I updated the question. I guess they are all the possible scenes to handle the function. Is it the answer of yr question? Yet, still seems third return true is in the wrong place because of recursion. –  Mar 21 '12 at 18:57
  • 2
    @yahyaE: Actually I have a question: Why are you returning false when query->num_rows() > 0 ? Is it really an error always when query->num_rows() > 0? – Cam Mar 21 '12 at 19:02
  • still screening, it is the weakest link of my vision. The answer is no :) –  Mar 21 '12 at 19:03
  • 2
    Works. Thank you for your time and care. –  Mar 21 '12 at 19:15