0

I've used php to open and list the files in the folder, but I'd like to somehow turn it into an if else statement so that if its from the 'dark' background folder a white logo will be displayed and if its from a 'light' background folder a black logo will be displayed.

<?php
//path to directory to open
$directory = "backgrounds/dark";
$dir_handle = @opendir($directory) or die("Unable to open folder");

while(false !== ($file = readdir($dir_handle)))
{
  if($file != '.' && $file != '..') 
  {
    echo "{image : 'http://occa-art.com/backgrounds/dark/".$file."'}, ";                      
  }
}
closedir($dir_handle);
?>

The {image: 'http://' } format is used because the images are listed within the Supersized background script.

---EDIT---

Maybe something like this (untested) to determine which folder the background image is from:

<?php
$doc = new DOMDocument();
$doc->loadHTML($htmlAsString);
$xpath = new DOMXPath($doc);
$nodeList = $xpath->query('//img[@class="bg"]/@src');
for ($i = 0; $i < $nodeList->length; $i++) {
    # Xpath query for attributes gives a NodeList containing DOMAttr objects.
    # http://php.net/manual/en/class.domattr.php
    echo $nodeList->item($i)->value . "<br/>\n";
}

$bg_url = $nodeList->item($i)->value; // not sure about this?
$parent = dirname("$bg_url");
if ($parent == 'dark') { ?>
    <img src="<?php bloginfo('template_url'); ?>/images/OCCAIndexHeaderwhite.png" alt="OCCA logo" />
<?php } else { ?>
    <img src="<?php bloginfo('template_url'); ?>/images/OCCAIndexHeaderblack.png" alt="OCCA logo" />
<?php } ?>

used code found here and here

Community
  • 1
  • 1
lizzmo
  • 201
  • 1
  • 13
  • Are the logos the files in the _dark_ and _light_ folders? – 0eggxactly Mar 12 '12 at 11:10
  • but if you set the directory to be opened to "black" how can that become "light"? – Nicola Peluchetti Mar 12 '12 at 11:15
  • @SLACEDIAMOND the background images would be divided into the dark and light folders. The logos would be stored in the general images folder. But that can change if necessary. – lizzmo Mar 12 '12 at 13:32
  • @NicolaPeluchetti at the moment I'm using the same code repeated twice, replacing 'dark' with 'light' but other than that its exactly the same so I didn't post it. Sorry if thats confusing. – lizzmo Mar 12 '12 at 13:35

0 Answers0