Jump to content

Recommended Posts

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?

Link to comment
https://forums.phpfreaks.com/topic/261820-can-you-do-php-in-an-xml-file/
Share on other sites

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());

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.

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.