Combining functions

Hi.

I have this script with a series of functions to insert different css files when a certain urls are in the site map of the site:


<?php
$DomDocument = new DOMDocument();
$DomDocument->preserveWhiteSpace = false;
$DomDocument->load('http://www. website .com/index.php?option=com_xmap&view=xml&tmpl=component&id=1');
$DomNodeList = $DomDocument->getElementsByTagName('loc');
foreach($DomNodeList as $url) {
$urls[] = $url->nodeValue;
}
// Sections;
[B]if(in_array("http://www.sitio .com/en/category/category-a",$urls)){
$css= '<style type="text/css">ul#areas li#ZAF, path#jvectormap1_ZAF {display: inherit;}</style>';
}
echo $css;

if(in_array("http://www.sitio .com/en/category/category-b",$urls)){
$css= '<style type="text/css">ul#areas li#ZAG, path#jvectormap1_ZAG {display: inherit;}</style>';
}
echo $css;

if(in_array("http://www.sitio .com/en/category/category-c",$urls)){
$css= '<style type="text/css">ul#areas li#ZAX, path#jvectormap1_ZAX {display: inherit;}</style>';
}
echo $css; [/B]

.......

The problem is that there are too many functions for Internet Explorer 8, which support up to a number of stylesheets.

Well, too many of them, not just the three in the example.

The question is if there is a way to combine, for instance, the three in bolds.

Yup


<?php
$DomDocument = new DOMDocument();
$DomDocument->preserveWhiteSpace = false;
$DomDocument->load('http://www. website .com/index.php?option=com_xmap&view=xml&tmpl=component&id=1');
$DomNodeList = $DomDocument->getElementsByTagName('loc');
foreach($DomNodeList as $url) {
    $urls[] = $url->nodeValue;
}
$css = '';
if (in_array("http://www.sitio .com/en/category/category-a",$urls)) {
    $css .= 'ul#areas li#ZAF, path#jvectormap1_ZAF {display: inherit;}';
}
echo $css;

if (in_array("http://www.sitio .com/en/category/category-b",$urls)) {
    $css .= 'ul#areas li#ZAG, path#jvectormap1_ZAG {display: inherit;}';
}
echo $css;

if (in_array("http://www.sitio .com/en/category/category-c",$urls)) {
    $css .= 'ul#areas li#ZAX, path#jvectormap1_ZAX {display: inherit;}';
}

if ($css !== '') {
    echo '<style type="text/css">', $css, '</style>';
}

:slight_smile:

.= means “add to variable”; $a .= 'something' is the same as $a = $a . 'something'. So basically it’s just a fast way to concatenate.

Yes, I found a similar way to do it.

And both of them work.

Thanks ScallioXTX.