Jump to content

looping issue for php/xml


anf.etienne

Recommended Posts

Hi,

 

I have created this code to get data from a html form into php and then generate an xml file. I am having a problem looping....I need it to loop so that if there is more than one caption that has been submitted then a new line of xml will start with the corresponding data. Each loop i have tried has given me an error saying the header has already been sent.

 

Here is my code......thanks in advance

 


<?php

if(isset($_POST['create_xml'])){

$picT = $_POST['picT'];

$photoT = $_POST['photoT'];

$captionT = $_POST['captionT'];

$xmltext = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<gallery></gallery>";
$xmlobj = simplexml_load_string($xmltext);

$nameCapsA = $xmlobj->addChild("$picT");
$nameCapsA->addAttribute("name", "$photoT");
$nameCapsA->addAttribute("caption", "$captionT");

// set output filename
$filename= "test1.xml";

print header("Content-type: text/plain") . $xmlobj->asXML();
}

?> 

Link to comment
https://forums.phpfreaks.com/topic/149314-looping-issue-for-phpxml/
Share on other sites

I don't think you can do what you are wanting. You can have a web page "open" a file (i.e. server the file to the user, without having a physical file) directly, but I've never seen a page open multiple files using that method, and I don't think you can. You could, however, have the script create physical file for the user and display a page for the user to download each file.

i am not trying to open multiple files......i have a form that people enter captions for pictures they have uploaded, when they click on submit it runs this script to create a xml file. I've tested the script and it works for just 1 xml line which is:-

 

$nameCapsA = $xmlobj->addChild("$picT");
$nameCapsA->addAttribute("name", "$photoT");
$nameCapsA->addAttribute("caption", "$captionT");

 

I know it can be looped because as i was testing it kept looping but brought back errors, I just don't know the best way to get it to loop.

Try this (not tested)

<?php

$xmlFiles = array();

function createXML($caption)
{
    global $xmlObjects;

    $picT = $_POST['picT'];
    $photoT = $_POST['photoT'];

    $xmltext = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<gallery></gallery>";
    $xmlobj = simplexml_load_string($xmltext);

    $nameCapsA = $xmlobj->addChild($picT);
    $nameCapsA->addAttribute("name", $photoT);
    $nameCapsA->addAttribute("caption", $caption);

    $xmlFiles[] = $xmlobj->asXML();
}

if(isset($_POST['create_xml']))
{

    if (is_array($_POST['captionT'])
    {
        foreach ($_POST['captionT'] as $caption)
        {
            createXML($caption);
        }
    }
    else
    {
        createXML($_POST['captionT']);
    }
}

echo header("Content-type: text/plain");

//Echo data from each file.
foreach ($xmlFiles as $xmlFile)
{
    echo $xmlFile . "<br /><br />\n";
}

?> 

this is my final code....can't work out why it's not generating the xml

 

<?php

$random_digit = $_POST['random_digit'];

error_reporting(E_ALL);

$xmlFiles = array();

function createXML($caption)
{
    global $xmlObjects;

    $picT = $_POST['picT'];
    $photoT = $_POST['photoT'];

    $xmltext = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<gallery></gallery>";
    $xmlobj = simplexml_load_string($xmltext);

    $nameCapsA = $xmlobj->addChild($picT);
    $nameCapsA->addAttribute("name", $photoT);
    $nameCapsA->addAttribute("caption", $caption);

    $xmlFiles[] = $xmlobj->asXML();
}

if(isset($_POST['create_xml']))
{

    if (is_array($_POST['captionT']))
    {
        foreach ($_POST['captionT'] as $caption)
        {
            createXML($caption);
        }
    }
    else
    {
        createXML($_POST['captionT']);
    }
}

echo header("Content-type: text/plain");

//Echo data from each file.
foreach ($xmlFiles as $xmlFile)
{
    echo $xmlFiles . "<br /><br />\n";

        $path_dir = "xmlf/";

        $path_dir .=   $random_digit .".xml";

$fp = fopen($path_dir,'w');

            $write = fwrite($fp,$xmlFile);

	}

?> 

 

 

i got a result from testing the code.....it generates the xml but splits everything up.....instead of being:

 

<gallery>

 

"X amount of lines for xml content"

 

</gallery>

 

it displays it lyk

 

<gallery>"xml content"</gallery>

<gallery>"xml content"</gallery>

<gallery>"xml content"</gallery>

<gallery>"xml content"</gallery>

<gallery>"xml content"</gallery>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.