0

I'm using the jQuery nestedSortable plugin in a project. When a list item is moved, I serialize the array and save it to my db. I'm having trouble recreating the HTML list from the PHP array. Probably because it's late. :)

I have the following PHP array:

Array
(
    [list_4] => root
    [list_3] => 4
    [list_1303966373] => 3
    [list_1] => 1303966373
    [list_5] => 1
    [list_2] => 1
    [list_6] => root
)

It should make a list something like...

<ol>
<li id='list_4'>value 4
  <ol>
    <li id='list_3'>value 3
      <ol>
        <li id='list_1303966373'>value 1303966373
          <ol>
            <li id='list_1'>value 1
              <ol>
                <li id='list_5'>value 5</li>
                <li id='list_2'>value 2</li>
              </ol>
            </li>
          </ol>
        </li>
      </ol>
    </li>
  </ol>
</li>
<li id='list_6'>value 6</li>          
</ol>

(Ignore the values, they're just there for show.)

The HTML list could have any depth of nested lists.

My brain is dead and I cannot get it to work. Anyone have a suggestion? I'll owe you cookies for eternity. Also, a castle.

Thanks. :)

10
  • This isn't the concern of the question, but you may want to investigate other ways to save such a list to your database. Commented Apr 28, 2011 at 5:43
  • Please don't start id's with numbers, it makes babies cry w3.org/TR/html4/types.html (see it's even in the docs) Commented Apr 28, 2011 at 5:45
  • hey guys, that's really just for show. the actual list has ids prepended with "list_" I'll edit the ex to make the babies feel better. :) Commented Apr 28, 2011 at 5:48
  • @zneak That's true. After this problem is solved, I intend to save the parent id with each row of the "values" table I use to keep info on each individual <li> item. I'll still run into the issue though, so I figured I'd cross this bridge first. Commented Apr 28, 2011 at 5:51
  • @Dylan - Why don't you try jquery treeview for this...? Commented Apr 28, 2011 at 6:03

1 Answer 1

5

Here you have it (tested OK with your sample):

<?php
  $tree = array(
    "list_4" => "root",
    "list_3" => "list_4",
    "list_1303966373" => "list_3",
    "list_1" => "list_1303966373",
    "list_5" => "list_1",
    "list_2" => "list_1",
    "list_6" => "root",
 );
 function getChildrenLists($tree){
    $tree2 = array();
    foreach($tree as $child => $parent){
       if(!array_key_exists($parent, $tree2)) $tree2[$parent] = array();
       $tree2[$parent][] = $child;
    }
    return $tree2;
 }

 function renderTree($tree2, $parent = "root"){
    if($parent != "root") echo "<li id='$parent'> value $parent\n";
    $children = $tree2[$parent];
    if(count($children) > 0){ //If node has children
       echo "<ol>\n";
       foreach($children as $child)
          renderTree($tree2, $child);
       echo "</ol>\n";
    }
    if($parent != "root") echo "</li>\n";
 }
 $tree2 = getChildrenLists($tree); 
 renderTree($tree2);

?>

I want my cookie! Hehe. Hope it helps.

Sign up to request clarification or add additional context in comments.

1 Comment

I think you might just be the bomb.com. Thank you thank you thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.