Giuseppe Giubaldo Giuseppe Giubaldo - 5 months ago 13
HTML Question

Recreate html page from array (2.0)

Good morning everyone,
Yesterday I asked a question to resolve this problem but array was flawed.
Now the array seems fixed but I'm having troubles to create the page.
So, this is the fixed array

'tag' => array(
'tagName' => 'section',
'tagAttributes' => array(
'class' => 'main full',
),
'tagContents' => array(
'tag' => array(
'tagName' => 'img',
'tagAttributes' => array(
'class' => 'logo',
'src' => 'assets/images/logo-480x176.png'
)
),
'tag_1' => array(
'tagName' => 'div',
'tagAttributes' => array(
'class' => 'cover sfondo'
),
'tagContents' => array(
'tag' => array(
'tagName' => 'img',
'tagAttributes' => array(
'class' => 'cover-image',
'src' => 'assets/images/cover-img-header.png'
)
)
)
)
),
'tag_2' => array(
'tagName' => 'div',
'tagAttributes' => array(
'class' => 'cover-mob sfondo-mob'
),
'tagContents' => array(
'tag' => array(
'tagName' => 'img',
'tagAttributes' => array(
'class' => 'cover-image-mob',
'src' => 'assets/images/cover-img-mobile.jpg'
)
)
)
),
'tag_3' => array(
'tagName' => 'a',
'tagAttributes' => array(
'class' => 'button order light'
),
'tagContents' => array(
'tag' => array(
'tagName' => 'div',
'tagAttributes' => array(
'class' => 'data-min',
),
'tagContents' => array(
'tag' => array(
'tagName' => 'div',
'tagAttributes' => array(
'class' => 'label-holder',
),
'tagContents' => array(
'tag' => array(
'tagName' => 'img',
'tagAttrbiutes' => array(
'src' => 'assets/images/sfondo-cta.jpg'
)
),
'tag_1' => array(
'tagName' => 'p',
'tagAttributes' => array(
'class' => 'label',
),
'tagContents' => array(
'strong' => 'text'
)
)
)
),
'tag_1' => array(
'tagName' => 'div',
'tagAttributes' => array(
'class' => 'price-holder'
)
)
)
)
),
'tag_1' => array(
'tagName' => 'div',
'tagAttributes' => array(
'class' => 'cta'
),
'tagContents' => array(
'tag' => array(
'tagName' => 'p',
'tagAttributes' => array(
'class' => 'main-title'
),
'tagContents' => array(
'tag' => array(
'tagName' => 'a',
'tagAttributes' => array(
'strong' => 'text'
)
)
)
)
)
),
'tag_2' => array(
'tagName' => 'img',
'tagAttributes' => array(
'class' => 'main-title',
'src' => 'assets/images/extra_1.png'
)
)
)
),
'tag_1' => array(
'tagName' => 'section',
'tagAttributes' => array(
'class' => 'gallery',
'id' => 'video'
),
'tagContents' => array(
'tag' => array(
'tagName' => 'p',
'tagAttributes' => array(
'class' => 'title video-title',
),
'tagContents' => array(
'tag' => array(
'tagName' => 'span',
'tagAttributes' => array(
'strong' => 'text'
)
)
)
),
'tag_1' => array(
'tagName' => 'div',
'tagAttributes' => array(
'class' => 'container',
),
'tagContents' => array(
'tag' => array(
'tagName' => 'div',
'tagAttributes' => array(
'class' => 'arrow-container prev',
),
'tagContents' => array(
'tag' => array(
'tagName' => 'img',
'tagAttributes' => array(
'class' => 'arrow',
'src' => 'assets/images/freccia-sx_1.png'
)
)
)
)
)
),
'tag_2' => array(
'tagName' => 'div',
'tagAttributes' => array(
'class' => 'arrow-container next',
),
'tagContents' => array(
'tag' => array(
'tagName' => 'img',
'tagAttributes' => array(
'class' => 'arrow',
'src' => 'assets/images/freccia-dx_1.png'
)
)
)
),
'tag_3' => array(
'tagName' => 'div',
'tagAttributes' => array(
'class' => 'gallery-holder'
),
'tagContents' => array(
'tag' => array(
'tagName' => 'div',
'tagAttributes' => array(
'class' => 'internal single'
),
'tagContents' => array(
'tag' => array(
'tagName' => 'div',
'tagAttributes' => array(
'class' => 'gallery-item video',
),
'tagContents' => array(
'tag' => array(
'tagName' => 'div',
'tagAttributes' => array(
'class' => 'wrapper video-wrapper'
),
'tagContents' => array(
'tag' => array(
'tagName' => 'img',
'tagAttributes' => array(
'class' => 'play-vid pointer thumb',
'src' => 'assets/images/img-spot-tv.png'
)
),
'tag_1' => array(
'tagName' => 'img',
'tagAttributes' => array(
'class' => 'play-icon hover',
'src' => 'assets/images/play.png'
)
)
)
)
)
)
)
)
)
)
)
),


And this is how I tried to create html page

foreach ($MiniSiteStructureArray as $section => $sectionStructure) {

$html = '<' . $sectionStructure['tagName'] . '';
foreach ($sectionStructure['tagAttributes'] as $name => $value) {
$html .= " $name='" . $value . "'>";
}
foreach ($sectionStructure['tagContents'] as $contents) {
foreach ($contents['tagAttributes'] as $name => $value) {
$html .= '<' . $contents['tagName'] . ' ' . $name . '="' . $value . '">';
}
if (isset($contents['tagContents'])) {
$html .= findAllContents($contents['tagContents'], $html);
}
}
}

function findAllContents($tagContents, $tmpHtml) {
foreach ($tagContents as $tag => $tagValue) {
if (isset($tagValue['tagAttributes'])) {
foreach ($tagValue['tagAttributes'] as $tagKey => $value) {
$tmpHtml = '<' . $tagValue['tagName'] . ' ' . $tagKey . '="' . $value . '"></' . $tagValue['tagName'] . '>';
}
}
}
return $tmpHtml;
}

echo $html;


But there are some issues.

Cycling 'tagAttributes' for example.

1) The code finds 'class' => 'logo' but later it finds 'src' => 'assets/...' and overwrite previous values.

2) same thing with $html

Edit_1 :

foreach ($MiniSiteStructureArray as $section => $sectionStructure) {
$html = '<' . $sectionStructure['tagName'] . '';
foreach ($sectionStructure['tagAttributes'] as $name => $value) {
$html .= " $name='" . $value . "'>";
}
foreach ($sectionStructure['tagContents'] as $contents) {
$html .= '<' . $contents['tagName'];
foreach ($contents['tagAttributes'] as $name => $value) {
$html.=' '. $name . '="' . $value . '">';
}
if (isset($contents['tagContents'])) {
$html .= findAllContents($contents['tagContents'], $html);
}
}
$html .= '</'.$contents['tagName'].'>';
}

function findAllContents($tagContents, $tmpHtml) {
foreach ($tagContents as $tag => $tagValue) {
$tmpHtml = '<' . $tagValue['tagName'];

if (isset($tagValue['tagAttributes'])) {
foreach ($tagValue['tagAttributes'] as $tagKey => $value) {
$tmpHtml .= ' ' . $tagKey . '="' . $value . '"';
}
}

$tmpHtml .= '></' . $tagValue['tagName'] . '>';
}
return $tmpHtml;
}


Better, thanks.

Now remains the $html issue.
I'd been thinking to open a html tag like this
$html = <html><head></head><body>
at the beginning of the code and close html tag and body tag at the end but I tried and doesn't work.

Edit_2 :

Ok, it's done I think. I only add a this
$html = ''
at the beginning.

Answer

You have to open your tag before tagAttributes foreach loop and close after it. There will be also problem if you don't add tagAttributes key to your array, so the tag itself will be omitted.

Example of modification your foreach loop for findAllContents():

foreach ($tagContents as $tag => $tagValue) {
    $tmpHtml .= '<' . $tagValue['tagName']

    if (isset($tagValue['tagAttributes'])) {
        foreach ($tagValue['tagAttributes'] as $tagKey => $value) {
            $tmpHtml .= ' ' . $tagKey . '="' . $value . '"';
        }
    }

    $tmpHtml .= '></' . $tagValue['tagName'] . '>';
}
Comments