Log in

View Full Version : List Of Contents In PHP



hackerspk
08-15-2011, 09:20 AM
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 = "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";

/**
* 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 My sub Heading My second My deep Heading string now again Top Heading My test string My sub Heading My second My deep Heading

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

any idea or help?