How to make the code properly close 2 divs when I need it?

I am reworking a PHP code so it will automatically create tabs on my pages. It checks if there are h2 (for the title of the tab) and h3 (for the tab text contents) tags added to the page text and if so it creates tabs automatically.
But, I am stuck for days already cause I need to tell the php code to close 2 divs at a specific place but no matter what I try it never closes those divs in the right place.

Now please understand I do not know much about php. I am simply using php code made by others and I am adjusting it to my needs. So, if you see stupid errors or I can easily sove this I am sorry :slight_smile:

Let me show the code: [php]<?php
defined(‘COT_CODE’) or die(‘Wrong URL’);

require_once cot_langfile(‘autotab’);

$elems = explode(’,’, $cfg[‘plugin’][‘autotab’][‘elements’]);
$text = $t->vars[‘PAGE_TEXT’];
$chapters = array();
$chapters_elem = array();

foreach($elems as $level => $elem)
{
$elem = trim($elem);
$headings = array();
preg_match_all(“<$elem>(.*?)</$elem>is”, $text, $headings, PREG_OFFSET_CAPTURE);
$headings = $headings[1];
if (!$headings) continue;
foreach($headings as $heading)
{
$title = $heading[0];
$chapters[$heading[1]] = array($title, $level);
$chapters_elem[$title] = $elem;
}
}
ksort($chapters);

$toc = array();
$parents = array();
foreach($chapters as $chapter)
{
list($title, $level) = $chapter;
switch($level)
{
case 0:
$toc[$title] = array();
$parents[$level] = $title;
break;
case 1:
$toc[$parents[0]][$title] = array();
$parents[$level] = $title;
break;
default:
$toc[$parents[0]][$parents[1]][$title] = array();
break;
}
}

function buildTOC(&$text, $chapters, $parents = ‘’)
{
global $chapters_elem;
$i=0;
$toc = ‘

    ’;
    foreach($chapters as $chapter_raw => $subchapters)
    {
    $chapter = strip_tags(trim($chapter_raw));
    $i++;
    $elem = $chapters_elem[$chapter_raw];
    $level = $parents.$i;
    $url = $_SERVER[“REQUEST_URI”] . “#tab$level”;
    $toc .= “
  • <a href=”$url" title="$chapter">$chapter";
    // $text = str_replace("<$elem>$chapter_raw</$elem>", “<div class=“tab-content” id=“tab$level”><div class=“gamelist”><$elem>$level. $chapter</$elem>”, “”);
    //if (count($subchapters) > 0)
    //{
    // $toc .= buildTOC($text, $subchapters, $level.’.’);
    $text = str_replace("<$elem>$chapter_raw</$elem>", “<div class=“tab-content” id=“tab$level”><div class=“gamelist”><$elem>$level. $chapter</$elem>”, $text);
    //$text2 = str_replace("", “
”, $text);
// $text .= ‘’; this does not work cause now it creates xx number, equal to the number of tabs cause it loop, of ending divs after the last tab text.
//}
$toc .= ‘’;
// $text .= ‘’; this does not work cause now it creates xx number, equal to the number of tabs cause it loop, of ending divs after the last tab text.
}
$end .= ‘’;
$toc .= ‘
’;
$text .= ‘
’; // this div closes the tabs-1 and the tabs-container div!
return $toc;
}

$t->assign(‘PAGE_TOC’, buildTOC($text, $toc));
$t->assign(‘PAGE_TEXT’, $text);

?>[/php]

This outputs the following: [code]

1. tab1

text tab 1

2. tab2

text tab 2

3. tab3

text tab 3

[/code]

Now the problem is I need 2 closing divs after each and after the last there should be 4 closing divs.
No matter what I try I can not get closing divs to be added properly. Everything I do adds closing divs in the wrong position as you can see from some leftover comments in the above php.
I tried many more things, like a new variable called “$end” which adds the closing div but it simply skipped that. I tired an additional str_replace like this [php]$text = str_replace("", “

”, “”);[/php] but that overruled the main str_replace and all text was gone.

I hope people here can help me cause I am about to give up on this, thanks in advance! :slight_smile:

I’ve only glanced through your code so you will have to figure out where to put the following solution. (call it a free lesson in php :wink: )
If you know exactly when you want to close the div, i would use a counter…

[php]$counter = 0; // place outside the loop.

foreach(…blah blah) {
…blah blah…
…blah blah…

if($counter == 1) { // remember the counter started at zero, so 1 will be the second iteration ;)
    // close the div here!
}

...blah blah...
$counter++; // increment the counter.

}[/php]

Hope that helps,
Red :wink:

Sponsor our Newsletter | Privacy Policy | Terms of Service