TapeGun007 Posted April 29, 2012 Share Posted April 29, 2012 The original file is like this. I only included two <items>, but basically, it's just Picture_1.jpg through Picture_10.jpg. It's a rotating banner that goes through 10 JPG files. However, I wanted to randomize the images so they don't always just show picture 1, then 2, then 3, but rather in a random order. The only reason why I'm using XML in this case, is because I have a 3rd party deal, where a flash banner is compiled on the fly. They make a call to this XML file, but I want to use php to randomize the XML images. <items> <item> <title></title> <path>Picture_1.jpg</path> <url></url> <target>_blank</target> <bar_color>0x683f3f</bar_color> <bar_transparency>70</bar_transparency> <slideShowTime>5</slideShowTime> </item> <item> <title></title> <path>Picture_2.jpg</path> <url></url> <target>_blank</target> <bar_color>0x683f3f</bar_color> <bar_transparency>70</bar_transparency> <slideShowTime>5</slideShowTime> </item> ... Picture_3.jpg ... Picture_4.jpg ect </items> Here is what I want to do in PHP: <items> <?php $arr = str_split('123456789'); // get all the charaters into an array shuffle($arr); // randomize the array foreach ($arr as $a){ ?> <item> <title></title> <path>Picture_<?php echo $a; ?>.jpg</path> <url></url> <target>_blank</target> <bar_color>0x683f3f</bar_color> <bar_transparency>70</bar_transparency> <slideShowTime>5</slideShowTime> </item> <?php } ?> </items> But this doesn't work, so perhaps it's not possible, or I have the wrong syntax. Help? Quote Link to comment https://forums.phpfreaks.com/topic/261820-can-you-do-php-in-an-xml-file/ Share on other sites More sharing options...
xyph Posted April 30, 2012 Share Posted April 30, 2012 Use the XML handlers PHP has bundled with it // Tell the client that this is XML data header('Content-Type: application/xml'); $array = range(1,9); shuffle($array); $xml = new SimpleXMLElement('<items></items>'); foreach($array as $number) { $item = $xml->addChild('item'); $item->addChild('title'); $item->addChild('path','Picture_'.$number.'.jpg'); $item->addChild('url'); // etc... } echo htmlspecialchars($xml->asXML()); Quote Link to comment https://forums.phpfreaks.com/topic/261820-can-you-do-php-in-an-xml-file/#findComment-1341607 Share on other sites More sharing options...
requinix Posted April 30, 2012 Share Posted April 30, 2012 Personally I prefer outputting the XML manually, but that's irrelevant. The most important part of xyph's example is the header(). Without it the browser will think the output is HTML. Which it isn't. Quote Link to comment https://forums.phpfreaks.com/topic/261820-can-you-do-php-in-an-xml-file/#findComment-1341614 Share on other sites More sharing options...
TapeGun007 Posted April 30, 2012 Author Share Posted April 30, 2012 Ok, I think (hopefully), I'm on the right track here, let me give you more. The Bold is the link that points to the xml file. Index.php has the following JavaScript: <script type="text/javascript"> var cacheBuster = "?t=" + Date.parse(new Date()); // stage dimensions var stageW = 803;//"100%"; var stageH = 391;//"100%"; // ATTRIBUTES var attributes = {}; attributes.id = 'FlabellComponent'; attributes.name = attributes.id; // PARAMS var params = {}; params.bgcolor = "#ffffff"; /* FLASH VARS */ var flashvars = {}; flashvars.componentWidth = stageW; flashvars.componentHeight = stageH; flashvars.pathToFiles = ""; flashvars.xmlPath = "http://www.freedomroadac.com/newsite/components/banners/banner.php"; /** EMBED THE SWF**/ swfobject.embedSWF("http://www.freedomroadac.com/newsite/components/banners/preview.swf"+cacheBuster, attributes.id, stageW, stageH, "9.0.124", "http://www.freedomroadac.com/newsite/components/banners/expressInstall.swf", flashvars, params); </script> The only thing of importance is that the "flashvars.xmlPath= ..... banner.php" (I changed this from .xml to a .php file) So, I read up a little bit on the hanlder, tell me if I'm doing this correct. Here is banner.php: <?php // Tell the client that this is XML data header('Content-Type: application/xml'); $array = range(1,9); shuffle($array); $xml = new SimpleXMLElement('http://www.freedomroadac.com/newsite/components/banners/bannerxml.xml'); foreach($array as $number) { $item = $xml->addChild('item'); $item->addChild('title'); $item->addChild('path','http://www.freedomroadac.com/newsite/components/banners/Picture_'.$number.'.jpg'); $item->addChild('url'); $item->addChild('target','_blank'); $item->addChild('bar_color','0x683f3f'); $item->addChild('bar_transparency','70'); $item->addChild('slideShowTime','5'); } ?> Assuming I did this right (which I must not have, because it doesn't seem to work). Here is the bannerxml.xml file: <?xml version="1.0" encoding="UTF-8"?> <!-- slideShowTime - if set to 0, there is no autoslide --> <banner width = "803" height = "391" backgroundColor = "0xffffff" backgroundTransparency = "100" timerDisable="false" buttonsSide="left" buttonsMarginX="20" startWith = "1" controllerHeight = "25" autoPlay="true" fadeTransition = "true" verticalTransition = "false" controllerTop = "false" transitionSpeed = "1" titleX = "0" titleY = "0"> <items></items> </banner> This is my way of trying to better implement what you were saying. But I'm certainly not knowledgeable on the handlers, and will have to read more. The flash seems to run, but the images do not. It's the same thing that happens when it tries to load an image that doesn't exist basically. So something isn't quite right, I have a feeling it's how I reference the xml file. But maybe you can point me in the right direction. Quote Link to comment https://forums.phpfreaks.com/topic/261820-can-you-do-php-in-an-xml-file/#findComment-1341617 Share on other sites More sharing options...
TapeGun007 Posted April 30, 2012 Author Share Posted April 30, 2012 I saw an error and made the following adjustment: $data = file_get_contents('http://www.freedomroadac.com/newsite/components/banners/bannerxml.xml'); $xml = new SimpleXMLElement($data); Quote Link to comment https://forums.phpfreaks.com/topic/261820-can-you-do-php-in-an-xml-file/#findComment-1341619 Share on other sites More sharing options...
requinix Posted April 30, 2012 Share Posted April 30, 2012 ...and the second-most important part is the echo at the bottom. But leave off the htmlspecialchars() part, that shouldn't be there. Quote Link to comment https://forums.phpfreaks.com/topic/261820-can-you-do-php-in-an-xml-file/#findComment-1341623 Share on other sites More sharing options...
xyph Posted April 30, 2012 Share Posted April 30, 2012 Yeah, I just had it there to test it on http://writecodeonline.com/php/ Forgot to remove At girlfriend's Quote Link to comment https://forums.phpfreaks.com/topic/261820-can-you-do-php-in-an-xml-file/#findComment-1341628 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.