create ul and li using a multidimensional array in php
Asked Answered
D

2

7

I have the following array:

$tree_array

When I do a var_dump, I get:

array(6) {
    [0]=> string(23) "$100,000 Cash Flow 2013"
    [1]=> array(6) {
        [0]=> string(1) "2" ["Goal_ID"]=> string(1) "2"
        [1]=> string(13) "Sell Iron Oak" ["Opportunity"]=> string(13) "Sell Iron Oak"
        [2]=> string(2) "10" ["OID"]=> string(2) "10"
    }
    [2]=> array(2) {
        [0]=> string(32) "ask her if she would like to buy" ["Activity"]=> string(32) "ask her if she would like to buy"
    }
    [3]=> array(6) {
        [0]=> string(1) "2" ["Goal_ID"]=> string(1) "2"
        [1]=> string(8) "Sell Car" ["Opportunity"]=> string(8) "Sell Car"
        [2]=> string(2) "11" ["OID"]=> string(2) "11"
    }
    [4]=> array(2) {
            [0]=> string(52) "Call Roy back to see if he would like to purchase it" ["Activity"]=> string(52) "Call Roy back to see if he would like to purchase it"
    }
    [5]=> array(1) {
        ["tot_opp"]=> NULL
    }
} 

My end goal is to create unordered lists and lists (ul, li) with this data. There will be more data added to the array as the database gets updated, so it will keep growing. My goal is to loop through the array and have it create the following code and be able to keep creating lists as the data grows. I am new to php and not sure how to accomplish this.

<ul>
<li>$100,000 Cash Flow 2013</li>
<ul>
<li>Sell Iron Oak</li>
<ul>
<li>ask her if she would like to buy</li>
</ul>
<ul>
<li>Sell Car</li>
</ul>etc...

Any help will be greatly appreciated! Thank you in advance!

Demimondaine answered 15/12, 2012 at 15:9 Comment(0)
T
2

Seems like a simple enough recursion to me:

function arrayToList($in) {
  echo "<ul>";
  foreach($in as $v) {
    if( is_array($v)) arrayToList($v);
    else echo '<li>' . $v . '</li>';
  }
  echo "</ul>";
}

It looks like you have some duplicate values up there. Are you using mysql_fetch_array? You should be using mysql_fetch_assoc or mysql_fetch_row depending on whether you need an associative or indexed array.

Tatting answered 15/12, 2012 at 15:24 Comment(4)
These suggestions are both great. The problem I am having is the lists are going to grow deep. There is a goal table and and opportunity table the data is being pulled from in mysql. Therefore, directly under a goal could be a child/sub goal or an opportunity. For example: 100,000 income => 30,000 from job (this is a goal) 100,000 income => sell rental house to bob (this is an opportunity). Additionally, as the child goals drill down deeper they can have child goals attached to them and opportunities.Demimondaine
Also, I changed the arrays to assoc. Here is the output I am getting from the dump now: array(6) { [0]=> string(23) "$100,000 Cash Flow 2013" [1]=> array(3) { ["Goal_ID"]=> string(1) "2" ["Opportunity"]=> string(13) "Sell Iron Oak" ["OID"]=> string(2) "10" } [2]=> array(1) { ["Activity"]=> string(32) "ask her if she would like to buy" } [3]=> array(3) { ["Goal_ID"]=> string(1) "2" ["Opportunity"]=> string(8) "Sell Car" ["OID"]=> string(2) "11" } [4]=> array(1) { ["Activity"]=> string(52) "Call Roy back to see if he would like to purchase it" } [5]=> array(1) { ["tot_opp"]=> NULL } }Demimondaine
Thanks so much for the help by the way! You both have gotten me leaps beyond where I was at!Demimondaine
Hi again. The above code outputs every item in ul and li tags. I am trying to get it in a hierarchy type display. For example: ul li 100k income /li li Sell Iron Oak /li /ul /ul How can I get the recursive array to filter through and place the tags appropriately and not just open and close both tags after every item? Thanks so very much in advance!Demimondaine
F
2

You need a recursive function for that, not a loop. This way it will handle any depth of your source array.

function make_list($arr)
{
    $return = '<ul>';
    foreach ($arr as $item)
    {
        $return .= '<li>' . (is_array($item) ? make_list($item) : $item) . '</li>';
    }
    $return .= '</ul>';
    return $return;
}
echo make_list($source_array);
Felicity answered 15/12, 2012 at 15:26 Comment(2)
These suggestions are both great. The problem I am having is the lists are going to grow deep. There is a goal table and and opportunity table the data is being pulled from in mysql. Therefore, directly under a goal could be a child/sub goal or an opportunity. For example: 100,000 income => 30,000 from job (this is a goal) 100,000 income => sell rental house to bob (this is an opportunity). Additionally, as the child goals drill down deeper they can have child goals attached to them and opportunities.Demimondaine
That's what the function will do if you keep elements in order. That is, if an element with a goal will be followed by an element with childs/sub goals, it will look natural, won't it?Felicity

© 2022 - 2024 — McMap. All rights reserved.