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>
2 Answers 2
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.
-
@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– Borealid08/19/2010 00:14:21Commented Aug 19, 2010 at 0:14
-
well how about the first
ol
tag then?GENx– GENx08/19/2010 00:19:57Commented 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.Borealid– Borealid08/19/2010 00:37:55Commented Aug 19, 2010 at 0:37
-
@Borealid - Well you call the next
make_list
withmake_list([arguments], ++$num_links)
. And withinmake_list
you check for$num_links
equaling 1 or 4.Peter Ajtai– Peter Ajtai08/19/2010 00:39:50Commented 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.Borealid– Borealid08/19/2010 01:14:10Commented Aug 19, 2010 at 1:14
$link
and only that intomake_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
.