hackerspk Posted August 15, 2011 Share Posted August 15, 2011 Ok yesterday i asked for help in my FAQ script and some one help me very well and iam successfully make the script but now i want to make List of contents just like in wikipedia My code is <?php $str = "[H1]Top Heading[/H1] My test string [H2]My sub Heading[/H2] My second [H3]My deep Heading[/H3] string now again [H1]Top Heading[/H1] My test string [H2]My sub Heading[/H2] My second [H3]My deep Heading[/H3]"; /** * Helper class */ class FaqHelper { static $count = 1; static $listItems = array(); static $prefix = 'faq-'; static function GetList() { $items = ''; foreach (self::$listItems as $id => $label) { $items .= '<li><a href="#' . self::$prefix . $id .'">' . $label . '</a></li>'; } return '<ul>'. $items .'</ul>'; } static function ReplaceCallback($matches) { $id = self::$count; $label = $matches[1]; self::$listItems[$id] = $label; $res = '<font size=\"7"><span id=\"'. self::$prefix . $id .'">' . $label . '</span></font><hr />'; self::$count++; return $res; } } $text = preg_replace_callback( "#\[HD1\]([^\[]+)\[/HD1\]#", array('FaqHelper', "ReplaceCallback"), $str ); $list = FaqHelper::GetList(); echo $list; echo '<br /><br />'; echo $text; ?> and this gives 1. Top Heading 2. Top Heading Top Heading My test string [H2]My sub Heading[/H2] My second [H3]My deep Heading[/H3] string now again Top Heading My test string [H2]My sub Heading[/H2] My second [H3]My deep Heading[/H3] but i want this results 1. Top Heading 1.1 My sub Heading 1.1.1 My deep Heading 2. Top Heading 2.1 My sub Heading 2.1.1 My deep Heading Top Heading My test string My sub Heading My second My deep Heading string now again Top Heading My test string My sub Heading My second My deep Heading string now again So on ... Quote Link to comment https://forums.phpfreaks.com/topic/244818-list-of-contents-in-php/ Share on other sites More sharing options...
doddsey_65 Posted August 15, 2011 Share Posted August 15, 2011 You could use a recursive function to get all of the parent categories and its children to the nth level, but that would be slow if you have loads of them. I suggest reading about the modified preorder tree traversal algorithm Quote Link to comment https://forums.phpfreaks.com/topic/244818-list-of-contents-in-php/#findComment-1257580 Share on other sites More sharing options...
hackerspk Posted August 15, 2011 Author Share Posted August 15, 2011 need to track where you are currently with regards to the heading number you're at, and track levels. The basic theory is this. When you hit your first [H1], you increment the counter to 1. When you hit the h2, your second level counter is incremented to 1. When you hit the h3, your third level counter is incremented to 1. This will obviously output 1.1.1. Now when you hit the next h1, they sub levels (ie not the main level) are reset to 0, and then the first level is incremented to 2. You hit the h2, and it goes to 1 again... Then you have first level 2, sub 1, sub 1 = 2.1.1. i have this idea but i dont know how to implement it any one please ? Quote Link to comment https://forums.phpfreaks.com/topic/244818-list-of-contents-in-php/#findComment-1257646 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.