Squeebee
asked on
Generate a table of contents automatically - Part 2
In the question at https://www.experts-exchange.com/questions/20798481/Create-a-table-of-contents-automatically.html I was given an excellent solution for automatically creating a table of contents for an article based on H3 tags. This is working very well for me, but I am hoping to expand it's functionality. Some of my articles are getting quite long, and the ability to add subsections based on H4 tags would be extremely valuable. I would like a format like this:
Item 1
Sub-Item 1
Sub-Item 2
Item 2
Sub-Item 1
Item 3
Item 4
Sub items would be any H4 tags that appear after an H3 tag and before the next h3 tag. I don't know if this is an easy modification or a major change, but 500 points because I like a fast response.
Item 1
Sub-Item 1
Sub-Item 2
Item 2
Sub-Item 1
Item 3
Item 4
Sub items would be any H4 tags that appear after an H3 tag and before the next h3 tag. I don't know if this is an easy modification or a major change, but 500 points because I like a fast response.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Ok..i don't like giving ready to use pieces of codes but here it is.. only for this time :-)
ASKER
Excellent, I just had to change this line:
echo '<li><a href="#part' . ($key + 1) . '">' . $value . '</a></li>';
It was producing a TOC that started with #part1 while the callback function started at #part0
Thanks for the quick reply!
echo '<li><a href="#part' . ($key + 1) . '">' . $value . '</a></li>';
It was producing a TOC that started with #part1 while the callback function started at #part0
Thanks for the quick reply!
yes... definitely, drop the +1, $key itself will do the job..
ASKER
Actually, there is one other issue:
foreach($toc AS $value) {
list($key,$type,$value) = $value;
if ($type == 1) print "<ol>\n";
echo '<li><a href="#part' . ($key + 1) . '">' . $value . '</a></li>';
if ($type == 1) print "</ol>\n";
}
If there is more than one subelement each element is element 1 and it looks odd to see 2 or three #1 elements under a main element, so I changed it to use ul tags for the sub-elements and it looks just fine. Just thought I would point it out for future readers.
You can see the new code in action at http://www.vbmysql.com/articles/vb_mysql_tutorials/vb_mysql_tutorial-part1.html
foreach($toc AS $value) {
list($key,$type,$value) = $value;
if ($type == 1) print "<ol>\n";
echo '<li><a href="#part' . ($key + 1) . '">' . $value . '</a></li>';
if ($type == 1) print "</ol>\n";
}
If there is more than one subelement each element is element 1 and it looks odd to see 2 or three #1 elements under a main element, so I changed it to use ul tags for the sub-elements and it looks just fine. Just thought I would point it out for future readers.
You can see the new code in action at http://www.vbmysql.com/articles/vb_mysql_tutorials/vb_mysql_tutorial-part1.html
you are right.. so you changed the <ol> to <ul>..
Well, there would be another solution, using a global variable that is $last_type, but i think it's fancier this way.
anyway for my need of perfection:
$last_type = 0;
foreach($toc AS $value) {
list($key,$type,$value) = $value;
if (($last_type == 1) && ($type == 0)) print "</ol>\n";
if (($last_type == 0) && ($type == 1)) print "<ol>\n";
$last_type = $type;
echo '<li><a href="#part' . ($key + 1) . '">' . $value . '</a></li>';
}
should work
Well, there would be another solution, using a global variable that is $last_type, but i think it's fancier this way.
anyway for my need of perfection:
$last_type = 0;
foreach($toc AS $value) {
list($key,$type,$value) = $value;
if (($last_type == 1) && ($type == 0)) print "</ol>\n";
if (($last_type == 0) && ($type == 1)) print "<ol>\n";
$last_type = $type;
echo '<li><a href="#part' . ($key + 1) . '">' . $value . '</a></li>';
}
should work
there is still an issue if the last item is a subelement, because it's not closing it.. but it's so trivial that you'll be able to sort out this condition :-)
bye
bye
ASKER
Yeah, thanks again for your excellent help.
ASKER