tags:

views:

89

answers:

2

I want to add two different class attributes to my my first and fourth <ol> tags but I really don't know how to add it in my recursive function? can some one help me?

Here is my PHP script.

function make_list ($parent = 0, $parent_url = '') {
    global $link;
    echo '<ol>';

    foreach ($parent as $id => $cat) {
        if($cat['parent_id'] == '0'){
            $url = $parent_url . $cat['url'];
            echo '<li><a href="' . $url . '" title="' . $cat['category'] . ' Category Link" style="color: orange; font-weight: bold;">' . $cat['category'] . '</a>';            
        } else {
            $url = $parent_url . $cat['url'];
            // Display the item:
            echo '<li><a href="' . $url . '" title="' . $cat['category'] . ' Category Link">' . $cat['category'] . '</a>';
        }

        if (isset($link[$id])) {
            make_list($link[$id], $url);
        }               
        echo '</li>';
    }       
    echo '</ol>';
}

$mysqli = mysqli_connect("localhost", "root", "", "sitename");
$dbc = mysqli_query($mysqli,"SELECT * FROM categories ORDER BY parent_id, category ASC");
if (!$dbc) {
    print mysqli_error();
} 

$link = array();

while (list($id, $parent_id, $category, $url, $depth) = mysqli_fetch_array($dbc)) {
    $link[$parent_id][$id] =  array('parent_id' => $parent_id, 'category' => $category, 'url' => $url, 'depth' => $depth);
}

make_list($link[0]);

Output

<ol>
   <li>First Nested List</li>
   <li>First Nested List</li>
   <li>First Nested List
      <ol>
        <li>Second Nested List</li>
        <li>Second Nested List</li>
        <li>Second Nested List
          <ol>
            <li>Third Nested List</li>
            <li>Third Nested List</li>
            <li>Third Nested List
              <ol>
                <li>Fourth Nested List</li>
                <li>Fourth Nested List</li>
                <li>Fourth Nested List</li>
              </ol>
            </li>
            <li>Third Nested List</li>
            <li>Third Nested List</li>
          </ol>
        </li>
        <li>Second Nested List</li>
        <li>Second Nested List</li>
      </ol>
   </li>
   <li>First Nested List</li>
   <li>First Nested List</li>
</ol>
A: 

Add a global $num_links variable. Whenever you emit an <ol>, increase it. Add the attributes when it hits the values you want.

However, if you're doing this, there's probably a mismatch between the function you've chosen and the task you're solving... "The first or the fourth" is likely not the real condition you want to check.

Borealid
@Borealid you lost me?
GENx
@GENx: I doubt what you want is "whenever I call this function, have the first and the fourth things it creates be magical". That's not a normal thing to want.
Borealid
well how about the first `ol` tag then?
GENx
@Borealid - Instead of a global `$num_links` `make_list` should be passing `$num_lnks` to itself, but there must be a simpler way of doing it.
Peter Ajtai
@Peter Ajtai: Because he specifically said "the first and the fourth" tags, that means you need a global. If it were an argument, it wouldn't work.
Borealid
@Borealid - Well you call the next `make_list` with `make_list([arguments], ++$num_links)`. And within `make_list` you check for `$num_links` equaling 1 or 4.
Peter Ajtai
@Borealid where in my code should I place the code?
GENx
@Peter Ajtai: Try it. Doesn't work, because `make_list` can call itself more than once from a given iteration. @GENx: You're going to need a little more understanding of how to program here. Sorry.
Borealid
@Borealid sorry dosn't cut it a better explanation would of helped.
GENx
@Borealid - sorry, the call should be `make_list([arguments], $num_links+1)` so `$num_links` isn't changed within a function instance.
Peter Ajtai
@Peter Ajtai: Still doesn't work. One `make_links` with `$num_links=1` could spawn **ten** `make_links` each with `$num_links=2`.
Borealid
+2  A: 

Just add depth as a parameter. Then check if its 0 or 4 or whatever you need.

function make_list ($parent = 0, $parent_url = '', $depth=0) {
...
make_list($link[$id], $url, $depth+1);
...
acidzombie24
I'd call it `$depth`.
Mark
@Mark: Good idea.
acidzombie24