#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    2
    Rep Power
    0

    Issues with recursion & HTML


    So, I have a problem with a WordPress plugin I'm making - I don't really like WordPress, but that's unrelated, so moving on. Anyways, I'm going to try and give some background information but I have a feeling it's going to be fairly confusing, so if I don't explain something clearly please ask.

    I am creating a "file manager" that basically takes uploaded files and allows you to put them under a category & give them specific permissions. Everything works pretty good (it isn't done by any means, but this is the only real bug right now). You can add categories, and you can give a category sub-categories. You can do this infinitely (well, your browser limits it, since it would crash if it tried loading too much, but anyways).

    Now, there are 2 places the categories are "rendered", in the front-end on the website (the part the normal user would see), and then in the back-end where you can add more categories, or delete/updating existing ones (you can also sort them, etc).

    I am going to be referring to the back-end, I have no idea if the front-end works. I can deal with that later, I need to get this fixed first. On the back-end as you add categories the HTML needs to automatically "update". I.e. it needs to actually generate the HTML for the categories, so lets say the HTML looks like this:
    Code:
    <div id="1">
    Category 1
    <div id="1_1">
    Sub-category to category 1
    </div>
    </div>
    <div id="2">
    Category 2
    </div>
    Now, as you can see as you'd add categories the HTML would obviously need to update. This isn't the exact HTML, but it's the same concept. The problem is, sometimes it doesn't add enough </div> tags. There is no problem "opening" categories, just closing then. Now it seems like it would be really simple, and you could just do it with a basic for-loop but for whatever reason I just cannot get it to work. Right now, it isn't adding enough closing tags.

    So, if you look above, IDs are organized by underscores. For example, ID 1 would be a top-level category, 1_1 would be a sub-category of 1, 1_1_1 would be a sub-category of 1_1.

    Okay, now that I tried to explain my problem, the code can be found at https://github.com/Ryuske/Wordpress-File-Manager. The primary files you should be concerned with are application/view/options/categories.php and controller/generate_views.php. Everything in the code should be fairly well documented. I can set up a live example of it, but I'd rather not. You can just download it and use it on your own version of WordPress. Like I side, I can make a live version, I'd just rather not.

    Anyways, if anyone has any thoughts at all I would vastly appreciate them. I've been stuck on this for a few months. I can get it to work for a certain number of levels, but once you go past that it breaks. I just can't seem to get it to be 100% dynamic and I don't understand why because it should just be simple math. I can get it to work just fine on paper...

    Thanks for reading!

    P.S. Sorry the URL isn't inside bb tags, I guess technically it's against the rules to post a URL but it's relevant and will be way easier for everyone involved if you just look there...
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    2
    Rep Power
    0
    Sorry for bumping but I really need help with this...
  4. #3
  5. Come play with me!
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    13,742
    Rep Power
    9397
    At first glance I don't see any recursion. Did you linearize it like with a stack/queue?

    The recursive function would look something like
    PHP Code:
    /**
     * Render categories as nested DIVs
     *
     * array(
     *   "Category 1" => array(
     *     "Subcategory"
     *   ),
     *   "Category 2"
     * )
     *
     * div id=1
     *   Category 1
     *   div id=1_1
     *     Subcategory
     *   /div
     * /div
     * div id=2
     *   Category 2
     * /div
     *
     * @param array $categories
     * @param string $parentId
     */
    $x = function($categories$parentId) use (&$x) {
        
    $i 1;
        foreach (
    $categories as $key => $value) {
            
    $id "{$parentId}_{$i++}";
            echo 
    "<div id='$id'>";
            if (
    is_int($key)) { // straight string value
                
    echo $value;
            } else if (
    is_array($value)) { // has subcategories
                
    $x($value$id);
            }
            echo 
    "</div>";
        }

    Does that help at all?

    Originally Posted by Ryuske
    P.S. Sorry the URL isn't inside bb tags, I guess technically it's against the rules to post a URL but it's relevant and will be way easier for everyone involved if you just look there...
    That restriction is for spammers but tends to do more harm than good. You're fine.

IMN logo majestic logo threadwatch logo seochat tools logo