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>

Comments

I think the recursion is unnecessary. You're iterating over all the key, value pairs of an array. It looks like you're having some problems sorting out the variables you need to use. The variables use needs a lot of work.

Written by Peter Ajtai

@Peter Ajtai the recursion is necessary to display the categories properly unless you can come up with a better script that does the same thing?

Written by GENx

You should pass $link and only that into make_list, then it looks like you can use 1 foreach nested into another to iterate through and produce all the links you want. I don't exactly understand what you're trying to do, so I can't produce a detailed answer, but it's clear that you should be iterating over $link.

Written by Peter Ajtai

Providing sample data and sample desired output would be super helpful.

Written by Peter Ajtai

geeze. I feel sorry for some of the comments you had to read through and the answer. Most of the time when someone ask for example code or output it isnt necessary. Take it from someone who asked a lot of questions. Also it makes perfect sense why you may want to do this. "The first or the fourth" is definitely a condition you want to check. I do it all the time for html (css selectors is an alternative which i may or may not wish to use) and for formatting text outputs (definitely easier to read)

Written by acidzombie24

Accepted Answer

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);
...
Written by acidzombie24
This page was build to provide you fast access to the question and the direct accepted answer.
The content is written by members of the stackoverflow.com community.
It is licensed under cc-wiki