Welcome Guest, Not a member yet? Register   Sign In
Logical Problem With category and sub-category
#21

[eluser]brandingdavid[/eluser]
To answer your other questions:

I want to provide all the data in one fell swoop. Like a category listing you see on the sides of blogs running WP.

I want to be able to move objects in a certain order. If I want Featured to show up, I'll set it to be 0 or 1 for menu_index, and I'll write code to refactor everything else, then doing the db queries, I was going to sort asc.
#22

[eluser]brandingdavid[/eluser]
Here is the print_r of the $results variable at the bottom of the get_all function:

Array ( [0] => Array ( [id] => 1 [title] => Information Technology [parent] => 0 [menu_index] => 4 [depth] => 0 [children] => Array ( [0] => Array ( [id] => 4 [title] => Web Specialist [parent] => 1 [menu_index] => 2 [depth] => 1 ) [1] => Array ( [id] => 5 [title] => Server Room [parent] => 1 [menu_index] => 1 [depth] => 1 ) ) ) [1] => Array ( [id] => 2 [title] => Human Resources [parent] => 0 [menu_index] => 1 [depth] => 0 [children] => Array ( ) ) [2] => Array ( [id] => 3 [title] => Planning [parent] => 0 [menu_index] => 3 [depth] => 0 [children] => Array ( ) ) )

Hopefully, that helps.
#23

[eluser]brandingdavid[/eluser]
I added a return $results; to the category.php model page. Now I can print_r the array on the frontpage_view.php view file, but I am still getting the foreach loop error.

A PHP Error was encountered
Severity: Warning
Message: Invalid argument supplied for foreach()
Filename: views/frontpage_view.php
Line Number: 16

This one seems to relate to the child loop foreach statement. Also, for echoing the parent title, I get the number 0. Seems like the title and the id fetching got mixed up somehow... Not sure.
#24

[eluser]brandingdavid[/eluser]
Okay, getting closer... This is what my frontpage_view.php now looks like

Code:
<?php
    //print_r ($categories);
    
    foreach ($categories as $category) {
        //print_r ($category);
        echo "<br /><br />";
            echo $category['title'] .'<br />';
            
            foreach($category['children'] as $child) {
                echo '- '. $child['title']. '<br />';
            }
       }
    ?&gt;

It now prints it out and works fine, except for the fact that Travel, a child of the child Web Specialist doesn't show up, nor get called anywhere in this whole thing, so if we can just figure out how to make this go N levels deep, it'd be perfect.
#25

[eluser]brandingdavid[/eluser]
Could I not re-write in CI form this type of thing? http://psoug.org/snippet/Recursive_funct...ee_338.htm

If so, would I have to use one function for grabbing the top level elements and another for building out the n-level of possible children?
#26

[eluser]jedd[/eluser]
Oh, you do want n levels returned, huh? I was trying to interpret meaning from the code snippet you first published, which looked like an intent to just pull one generation deep.

Yeah, I think here, if you want to generate a potentially infinite-dimension array, you'd have to use two functions. I've done recursive-ish stuff with these kinds of thing before - but working upwards (so only one parent for a given level) within a single function.

The code at psoup .. wow .. that's quite something, isn't it. I wonder what style guide out there encouraged that guy to use uppercase for php operators.

Do you have an idea how you want the data to present (in the array) - and if so, please share (wrapped in code tags, and nice and narrow - not all spread out on one or two lines). The psoup snippet you posted is as good as any for demonstrating the logic - have you tried to write something like that in your model yet?
#27

[eluser]brandingdavid[/eluser]
I haven't tried it in a model, I tried it in a view, but it got mad because of the $this error I was getting when I first tried various things to get this to work, also I came across issues calling the function from within the function.

As for data presentation. It will be a giant unordered list.

<ul><li>List Item
<ul><li>Sub-Item</li></ul></li>
</ul>




Theme © iAndrew 2016 - Forum software by © MyBB