jumasol Posted October 4, 2013 Share Posted October 4, 2013 Hi. After some time looking for the way to check the presence of links in a sitemap to add specific css styling in the head of my site, I have this script that works perfectly: <?php $DomDocument = new DOMDocument(); $DomDocument->preserveWhiteSpace = false; $DomDocument->load('http://www. el sitio .com/index.php?option=com_xmap&view=xml&tmpl=component&id=1'); $DomNodeList = $DomDocument->getElementsByTagName('loc'); foreach($DomNodeList as $url) { $urls[] = $url->nodeValue; } if(in_array("http://www. the web site .com/link-a",$urls)){ $css= '<link href="/css/link-a.css" rel="stylesheet" type="text/css" />'; } echo $css; ?> It looks for tag names in the loc tags of a sitemap to provide an array, offering the possibility to add a css for a given link. The problem is that when I add more that two conditionals the script inserts just the first and the last one. In this case, with five elements, just link-a.css and link-e.css appear in the head tag. <?php $DomDocument = new DOMDocument(); $DomDocument->preserveWhiteSpace = false; $DomDocument->load('http://www. el sitio .com/index.php?option=com_xmap&view=xml&tmpl=component&id=1'); $DomNodeList = $DomDocument->getElementsByTagName('loc'); foreach($DomNodeList as $url) { $urls[] = $url->nodeValue; } if(in_array("http://www. the web site .com/link-a",$urls)){ $css= '<link href="/css/link-a.css" rel="stylesheet" type="text/css" />'; } if(in_array("http://www. the web site .com/link-b",$urls)){ $css= '<link href="/css/link-b.css" rel="stylesheet" type="text/css" />'; } if(in_array("http://www. the web site .com/link-c",$urls)){ $css= '<link href="/css/link-c.css" rel="stylesheet" type="text/css" />'; } if(in_array("http://www. the web site .com/link-d",$urls)){ $css= '<link href="/css/link-d.css" rel="stylesheet" type="text/css" />'; } if(in_array("http://www. the web site .com/link-e",$urls)){ $css= '<link href="/css/link-e.css" rel="stylesheet" type="text/css" />'; } echo $css; ?> Well, my level is basic, and I would appreciate some advice on the problem, and a suitable way to gather those elements in a single script, since there are some scores of them in my project. Thanks very much. Quote Link to comment Share on other sites More sharing options...
jcbones Posted October 5, 2013 Share Posted October 5, 2013 (edited) Try this: <?php $DomDocument = new DOMDocument(); $DomDocument->preserveWhiteSpace = false; $DomDocument->load('http://www. el sitio .com/index.php?option=com_xmap&view=xml&tmpl=component&id=1'); $DomNodeList = $DomDocument->getElementsByTagName('loc'); foreach($DomNodeList as $url) { $file = '/css' . strrchr($url->nodeValue,'/') . '.css'; //get the the string from the last occurrence of / to the end of the string, appended to /css. Edit: append .css to the end of it. if(file_exists($file)) { //then check if that file exists where it is suppose to. echo '<link href="' . $file . ' rel="stylesheet" type="text/css" />'; //if it does, echo the link. } } ?> Edited October 5, 2013 by jcbones Quote Link to comment Share on other sites More sharing options...
jumasol Posted October 5, 2013 Author Share Posted October 5, 2013 Thanks for replying. I would like to abuse of your help, but I must doing something wrong because now it doesn't work. My try: <?php $DomDocument = new DOMDocument(); $DomDocument->preserveWhiteSpace = false; $DomDocument->load('http://www. the website .com/index.php?option=com_xmap&view=xml&tmpl=component&id=1'); $DomNodeList = $DomDocument->getElementsByTagName('loc'); foreach($DomNodeList as $url) { $file = '/css' . strrchr($url->nodeValue,'/') . '.css'; $urls[] = $url->nodeValue; if(file_exists($file)) if(in_array("http://www. the website .com/link-a",$urls)){ echo '<link href="' . $file . ' rel="stylesheet" type="text/css" />'; } } ?> Quote Link to comment Share on other sites More sharing options...
jcbones Posted October 5, 2013 Share Posted October 5, 2013 Yeah, you have parse errors. You should be working with: error_reporting(-1); ini_set('display_errors',1); Secondly, try commenting your scripts, so that you can follow the logic. Quote Link to comment Share on other sites More sharing options...
jumasol Posted October 6, 2013 Author Share Posted October 6, 2013 I have seen no parse errors. Can you tell me if you see some error in the script: <?php $DomDocument = new DOMDocument(); $DomDocument->preserveWhiteSpace = false; $DomDocument->load('http://www. the website .com/index.php?option=com_xmap&view=xml&tmpl=component&id=1'); $DomNodeList = $DomDocument->getElementsByTagName('loc'); foreach($DomNodeList as $url) { $file = '/css' . strrchr($url->nodeValue,'/') . '.css'; $urls[] = $url->nodeValue; if(file_exists($file)) if(in_array("http://www. the website .com/link-a",$urls)){ echo '<link href="' . $file . ' rel="stylesheet" type="text/css" />'; } } ?> Quote Link to comment Share on other sites More sharing options...
jumasol Posted October 7, 2013 Author Share Posted October 7, 2013 I have tested it again looking for some error and not found anything. Quote Link to comment Share on other sites More sharing options...
jcbones Posted October 8, 2013 Share Posted October 8, 2013 Sorry, I scanned to quick, thought there was a missing bracket on the if clause. To be honest, you took part of a script that I wrote, and factored in some of your own logic that would in no way work out correctly. The script I showed would dynamically pick up any url in the foreach loop, and link a css file to it. IF and only IF a matching css file was found, in the css directory. The script was changed by you, to load a css file ONLY if a url to a specific website's page was found. Quote Link to comment Share on other sites More sharing options...
.josh Posted October 9, 2013 Share Posted October 9, 2013 Okay let's rewind a bit back to your original code. I'm scratching my head over how it managed to output more than 1 to begin with.. are you sure one of them isn't being output from somewhere else? Because by this logic: $urls[] = $url->nodeValue; } if(in_array("http://www. the web site .com/link-a",$urls)){ $css= '<link href="/css/link-a.css" rel="stylesheet" type="text/css" />'; } if(in_array("http://www. the web site .com/link-b",$urls)){ $css= '<link href="/css/link-b.css" rel="stylesheet" type="text/css" />'; } if(in_array("http://www. the web site .com/link-c",$urls)){ $css= '<link href="/css/link-c.css" rel="stylesheet" type="text/css" />'; } if(in_array("http://www. the web site .com/link-d",$urls)){ $css= '<link href="/css/link-d.css" rel="stylesheet" type="text/css" />'; } if(in_array("http://www. the web site .com/link-e",$urls)){ $css= '<link href="/css/link-e.css" rel="stylesheet" type="text/css" />'; } echo $css; Either NO link tags will output (none of the conditions are true), or else ONE link tag will output - even if every one of them are true. You assign the value to $css for every one of them with no concatenation or anything, so each time a condition is true, $css gets overwritten. So either you need to do $css.= .. (notice the dot before the equal sign) on each of them, or else just skip assigning them to $css and just put an echo in front of them instead. But as far as you getting TWO output.. yeah, it can't be because of this. You said it the first and last tags being output..the common denominator is the first, so you must have the first tag being output somewhere else as well. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.