Welcome Guest, Not a member yet? Register   Sign In
[UPDATED] Breadcrumbs from hierachical pages
#1

[eluser]Marcelo Kanzaki[/eluser]
It seems to be creating an infinite loop. I don't know why...

Given a page, Accepts its id as the first parameter and returns all the pages above it.

The 'page_parent_id' field default is ZERO. If the page is a subpage then, it is set with the id of the parent page.

Code:
function bread_crumb ($page_id, $crumbs = array())
    {
        // Find the page
        $page = $this->find_by_id($page_id);
        
        // Add a crumb
        $crumbs[] = anchor('pages/'.$page->page_id, $page->page_title);
        
        // Check to see the the current page is at top level
        if ($page->page_parent_id > 0)
        {
            // If not, call the function recursively
            $this->bread_crumb($page->page_parent_id, $crumbs);
        }
        else
        {
            return $crumbs;            
        }
        
    }


the find_by_id function

Code:
function find_by_id ($page_id)
    {
        $this->db->where('page_id', $page_id);
        $query = $this->db->get('pages_table');
        
        return $query->row();
    }
#2

[eluser]pistolPete[/eluser]
Then this line should be changed?

Code:
if ($page->page_ > 0)

// becomes

if ($page->parent_id > 0)
#3

[eluser]Marcelo Kanzaki[/eluser]
The post editor must have been striped out that part of the code. But still not working...
#4

[eluser]jedd[/eluser]
Just to clarify - did it also strip out the bit a few lines later where you've got :

Code:
$this->bread_crumb($page->page_, $crumbs);

And I'm assuming you've done a var_dump on page_id and crumbs at the start of this function too?
#5

[eluser]jedd[/eluser]
Hmm .. hang on .. recursive function calls make my nose bleed (lots of things do, it seems). I rarely use it, in any case. So I may be up the wrong tree here ... but ...

When you return $crumbs at the end, you're returning that to the previous instance of your function .. which will then be working, again, with a page that doesn't have a parent page id of 0, yes?
#6

[eluser]Marcelo Kanzaki[/eluser]
Sorry i think it was the spell check tool. But i've fixed the post. Any ideas now?
#7

[eluser]jedd[/eluser]
Do you get any output at all, or does it do that thing where it clears the screen and times out / blow the memory limit, without flushing the cache to screen at all?

If you can, I'd love to see what happens if you put a var_dump at the start of your function. Probably want to do a "&ltpre;>" thing first, of course.

Code:
function bread_crumb ($page_id, $crumbs = array())
    {
        // Find the page
        $page = $this->find_by_id($page_id);

        // debugging stuff
        var_dump ($page_id);
        var_dump ($page->page_parent_id);
        echo "<hr>";
...
#8

[eluser]jedd[/eluser]
Hmm .. I'm also thinking that I once had a problem the huge size of objects compared to an array - I mean the data you're getting out of the find_by_id function. Can you fiddle the code and take that to $query->row_array() instead, and update your key references in the crumbs function?

Not sure what you've tried so far, so just throwing ideas that I'd pursue.
#9

[eluser]Marcelo Kanzaki[/eluser]
[quote author="jedd" date="1237270428"]Do you get any output at all, or does it do that thing where it clears the screen and times out / blow the memory limit, without flushing the cache to screen at all?

If you can, I'd love to see what happens if you put a var_dump at the start of your function. Probably want to do a "&ltpre;>" thing first, of course.

Code:
function bread_crumb ($page_id, $crumbs = array())
    {
        // Find the page
        $page = $this->find_by_id($page_id);

        // debugging stuff
        var_dump ($page_id);
        var_dump ($page->page_parent_id);
        echo "<hr>";
...
[/quote]


I did the var_dump test you suggested. I had 3 the following result:

Code:
string(1) "5" // page_id
string(1) "3" // page_parent_id

string(1) "3" // page_id
string(1) "1" // page_parent_id

string(1) "1" // page_id (at root level)
string(1) "0" // page_parent_id (has no parent)

but it didn't return any value;
#10

[eluser]jedd[/eluser]
Ah, okay. It didn't return any values, but did it return now - or just locks up until PHP times out?

Just for poops and giggles, what if you put a return $crumbs as the absolute last line in your function. (Can you tell I'm grasping at straws at this point?)




Theme © iAndrew 2016 - Forum software by © MyBB