简体   繁体   中英

PHP recursive function problem?

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>

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);
...

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM