0

I am trying to create a table of content for article content. The problem I am facing right now looks like this:

$section1 = " <h2>1. Section 1 headline<h2> <p>Lorem ipsum dolor sit amet, <strong>{$consectetur}</strong> adipiscing elit. Morbi id pharetra nunc. Praesent imperdiet, libero eget pharetra posuere, eros odio sagittis ante, vel commodo nulla nisl nec nibh. Sed nec finibus quam. Mauris vel erat tempor, egestas sapien at, interdum mauris. Cras at tortor sit amet lorem molestie mattis non vel nunc. Cras maximus enim eu dolor vestibulum venenatis et sed nibh. Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>" $section1a = " <h2>1.a Section 1a headline<h2> <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi id pharetra nunc. Praesent imperdiet, libero eget pharetra posuere, eros odio sagittis ante, vel commodo nulla nisl nec nibh. Sed nec finibus quam. Mauris vel erat tempor, egestas sapien at, interdum mauris. Cras at tortor sit amet lorem molestie mattis non vel nunc. Cras maximus enim eu dolor vestibulum venenatis et sed nibh. Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>" $section1b = " <h2>1.b Section 1b headline<h2> <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi id pharetra nunc. Praesent imperdiet, libero eget pharetra posuere, eros odio sagittis ante, vel commodo nulla nisl nec nibh. Sed nec finibus quam. Mauris vel erat tempor, egestas sapien at, interdum mauris. Cras at tortor sit amet lorem molestie mattis non vel nunc. Cras maximus enim eu dolor vestibulum venenatis et sed nibh. Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>" $section2 = " <h2>2. Section 2 headline<h2> <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi id pharetra nunc. Praesent imperdiet, libero eget pharetra posuere, eros odio sagittis ante, vel commodo nulla nisl nec nibh. Sed nec finibus quam. Mauris vel erat tempor, egestas sapien at, interdum mauris. Cras at tortor sit amet lorem molestie mattis non vel nunc. Cras maximus enim eu dolor vestibulum venenatis et sed nibh. Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>" 

I want to output:

Table of Contents:

  1. Section 1 headline

    1.a Section 1a headline

    1.b Section 1b headline

  2. Section 2 headline

  3. Section 3 headline

1. Section 1 headline

Lorem ipsum dolor sit amet, variablevalue adipiscing elit. Morbi id pharetra nunc. Praesent imperdiet, libero eget pharetra posuere, eros odio sagittis ante, vel commodo nulla nisl nec nibh. Sed nec finibus quam. Mauris vel erat tempor, egestas sapien at, interdum mauris. Cras at tortor sit amet lorem molestie mattis non vel nunc. Cras maximus enim eu dolor vestibulum venenatis et sed nibh. Lorem ipsum dolor sit amet, consectetur adipiscing elit.

1.a Section 1a headline

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi id pharetra nunc. Praesent imperdiet, libero eget pharetra posuere, eros odio sagittis ante, vel commodo nulla nisl nec nibh. Sed nec finibus quam. Mauris vel erat tempor, egestas sapien at, interdum mauris. Cras at tortor sit amet lorem molestie mattis non vel nunc. Cras maximus enim eu dolor vestibulum venenatis et sed nibh. Lorem ipsum dolor sit amet, consectetur adipiscing elit.

1.b Section 1b headline

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi id pharetra nunc. Praesent imperdiet, libero eget pharetra posuere, eros odio sagittis ante, vel commodo nulla nisl nec nibh. Sed nec finibus quam. Mauris vel erat tempor, egestas sapien at, interdum mauris. Cras at tortor sit amet lorem molestie mattis non vel nunc. Cras maximus enim eu dolor vestibulum venenatis et sed nibh. Lorem ipsum dolor sit amet, consectetur adipiscing elit.

2. Section 2 headline

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi id pharetra nunc. Praesent imperdiet, libero eget pharetra posuere, eros odio sagittis ante, vel commodo nulla nisl nec nibh. Sed nec finibus quam. Mauris vel erat tempor, egestas sapien at, interdum mauris. Cras at tortor sit amet lorem molestie mattis non vel nunc. Cras maximus enim eu dolor vestibulum venenatis et sed nibh. Lorem ipsum dolor sit amet, consectetur adipiscing elit.

What would be the best way to track the index of table of contents dynamically in PHP? The reason I need these dynamic is so that if someone decides to not print section 3, section 4 would automatically become section 3 in table of contents and in the body.

What have I tried so far?

So far, I tried wrapping my variable inside a function. For example:

function section1output = ($tocindex,$consectetur) { $section[1] = " <h2>{$tocindex}. Section 1 headline<h2> <p>Lorem ipsum dolor sit amet, <strong>{$consectetur}</strong> adipiscing elit. Morbi id pharetra nunc. Praesent imperdiet, libero eget pharetra posuere, eros odio sagittis ante, vel commodo nulla nisl nec nibh. Sed nec finibus quam. Mauris vel erat tempor, egestas sapien at, interdum mauris. Cras at tortor sit amet lorem molestie mattis non vel nunc. Cras maximus enim eu dolor vestibulum venenatis et sed nibh. Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>" return $section1; } 

And then I would use a for loop to output the sections and generate the table of content simultaneously.

However, this is making my code quite messy because there are about 30 variables being passed from the post data.

Is there a better way to generate the dynamic table of content I am after?

1
  • How are you determining the exclusion of a section? My initial thought is to restructure the HTML accordingly Like jsfiddle.net/16hw3sn0/1 to resemble the hierarchy of section and subsections. Commented Nov 16, 2019 at 4:30

2 Answers 2

1

firstly you arrange heading and sub heading in array format like below

$contents = []; $contents[] = [ 'heading' => 'Main heading 1', 'subHeading' => [ 'sub heading a', 'sub heading b', 'sub heading c', ] ]; $contents[] = [ 'heading' => 'Main heading 2', 'subHeading' => [ 'sub heading a', 'sub heading b', 'sub heading c', ] ]; 

than your table of content output from this code

echo "<ol>"; foreach ($contents as $content) { echo "<li>"; echo $content['heading']; if (isset($content['subHeading'])) { echo "<ol>"; foreach ($content['subHeading'] as $subHeading) { echo "<li>$subHeading</li>"; } echo "</ol>"; } echo "</li>"; } echo "</ol>"; 

Output form above code is

  1. Main heading 1
    1. sub heading a
    2. sub heading b
    3. sub heading c
  2. Main heading 2
    1. sub heading a
    2. sub heading b
    3. sub heading c
Sign up to request clarification or add additional context in comments.

Comments

1

You can try storing the contents in an object instead

$sections=array( array('headline'=>'Section 1 headline', 'content'=>'content... '.$variable.'..more content.' ), array('headline'=>'Section 2 headline', 'content'=>'more content....' ), ); 

Then you can loop through the object and skip what you want to omit.

$content=''; $omit=array(3,5); $count=1; foreach($sections as $section){ if(!in_array($count,$omit)){ $content.='<h2>'.$count.' '.$section['headline'].'</h2>'; $content.='<p>'.$section['content'].'</p>'; } $count++; } return $content; 

You can do the same treatment for just 'headline' for the table of contents.

2 Comments

Sheesh, I've been spending too much time with javascript. Coudln't figure out what's wrong with this code. Just need to use . operator instead of + in your answer. Other than that, it seems quite neat.
Omg. Yes. Sorry. I was just answering a question in javascript. Lol. Thanks! Will edit the answer.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.