Jump to content

Recommended Posts

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.

 

 

 

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/282726-problem-with-script/
Share on other sites

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 by jcbones
Link to comment
https://forums.phpfreaks.com/topic/282726-problem-with-script/#findComment-1452658
Share on other sites

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" />';
 }
}

?>
Link to comment
https://forums.phpfreaks.com/topic/282726-problem-with-script/#findComment-1452674
Share on other sites

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" />';
 }
}

?>
Link to comment
https://forums.phpfreaks.com/topic/282726-problem-with-script/#findComment-1452855
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/282726-problem-with-script/#findComment-1453179
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/282726-problem-with-script/#findComment-1453185
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.