Jump to content

List Of Contents In PHP


hackerspk

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/244818-list-of-contents-in-php/
Share on other sites

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 ?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.