Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7666173
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T14:46:02+00:00 2026-05-31T14:46:02+00:00

I have below recursive function in my model with CI, working. In my controller,

  • 0

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.
  }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T14:46:03+00:00Added an answer on May 31, 2026 at 2:46 pm

    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;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a recursive jQuery function that has something like this: parentItem.find('> div:first-child >
Recursive function for copy of multilevel folder is not working. I have a code
I am using PHP and I need to script something like below: I have
In the code snippet included below, I have a recursive function call, used to
I have below query I am trying to show message 'No SubSource for this
I have below a working query that needs to be simplified. The reason is
I have the following recursive function: ALTER FUNCTION [dbo].[ListAncestors] ( @Id int ) RETURNS
I have been trying to write a recursive version of function itoa , the
I was trying to make a tail-recursive version of this very simple SML function:
I am having problems running the below recursive function on our development web server.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.