Jump to content

Optimise Recursive Function


jontilt

Recommended Posts

Hi there,

 

I have written a recursive function that basically generates a tree structure for my site pages (kind of like a sitemap). It all worked fine until yesterday, I started getting error messages telling me that I have exhausted the amount of memory available.

 

There are only about 30 pages in the site (parent, child, sub-child etc), so I dont think that I should be using up that much memory. This was my first attempt at a recursive function so I am pretty sure that it may not have been optimised properly.

 

If anyone could take a look and offer any feed back that would be great. (written in codeigniter framework)

 

      function getAllPages() {

 

$this->tmp_arr = array();

 

$this->buildPageStructure(0, 0);

 

return $this->tmp_arr;

 

}

 

 

 

function buildPageStructure($parent_id, $counter) {

 

$CI = & get_instance();

 

$query = $CI->db->query("SELECT page_id, title, page_url, status FROM pages WHERE parent_page='$parent_id' ORDER BY sort_order ASC");

 

$counter++;

 

if($query->num_rows() > 0) {

 

foreach($query->result() as $row) {

 

$new_arr = new Page();

 

$new_arr->page_id = $row->page_id;

$new_arr->title = $row->title;

$new_arr->level = $counter;

$new_arr->page_url = $row->page_url;

$new_arr->status = $row->status;

 

$this->tmp_arr[] = $new_arr;

 

$this->buildPageStructure($row->page_id, $counter);

 

 

}

 

 

}

 

}

 

Cheers

 

Jon

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.