5

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>
asked Aug 19, 2010 at 0:04
5
  • 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. Commented Aug 19, 2010 at 0:08
  • @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? Commented Aug 19, 2010 at 0:10
  • 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. Commented Aug 19, 2010 at 0:19
  • Providing sample data and sample desired output would be super helpful. Commented Aug 19, 2010 at 0:19
  • 1
    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) Commented Aug 19, 2010 at 1:53

2 Answers 2

4

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);
...
answered Aug 19, 2010 at 1:36
0
0

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.

answered Aug 19, 2010 at 0:10
8
  • @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. Commented Aug 19, 2010 at 0:14
  • well how about the first ol tag then? Commented Aug 19, 2010 at 0:19
  • @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. Commented Aug 19, 2010 at 0:37
  • @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. Commented Aug 19, 2010 at 0:39
  • @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. Commented Aug 19, 2010 at 1:14

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.