anf.etienne Posted March 13, 2009 Share Posted March 13, 2009 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(); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/149314-looping-issue-for-phpxml/ Share on other sites More sharing options...
Psycho Posted March 13, 2009 Share Posted March 13, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/149314-looping-issue-for-phpxml/#findComment-784135 Share on other sites More sharing options...
anf.etienne Posted March 13, 2009 Author Share Posted March 13, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/149314-looping-issue-for-phpxml/#findComment-784139 Share on other sites More sharing options...
Psycho Posted March 13, 2009 Share Posted March 13, 2009 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"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/149314-looping-issue-for-phpxml/#findComment-784162 Share on other sites More sharing options...
anf.etienne Posted March 13, 2009 Author Share Posted March 13, 2009 it returned an error Parse error: syntax error, unexpected '{' in /home/vecre0/public_html/test/8/xmltest.php on line 26 Quote Link to comment https://forums.phpfreaks.com/topic/149314-looping-issue-for-phpxml/#findComment-784171 Share on other sites More sharing options...
Psycho Posted March 14, 2009 Share Posted March 14, 2009 As I said "Not tested". Add a right paren at the end of the is_array test. Quote Link to comment https://forums.phpfreaks.com/topic/149314-looping-issue-for-phpxml/#findComment-784357 Share on other sites More sharing options...
anf.etienne Posted March 14, 2009 Author Share Posted March 14, 2009 ive tested it over the past couple of hours and it just runs out a blank page....doesn't generate anything Quote Link to comment https://forums.phpfreaks.com/topic/149314-looping-issue-for-phpxml/#findComment-784504 Share on other sites More sharing options...
anf.etienne Posted March 14, 2009 Author Share Posted March 14, 2009 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); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/149314-looping-issue-for-phpxml/#findComment-784538 Share on other sites More sharing options...
anf.etienne Posted March 14, 2009 Author Share Posted March 14, 2009 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> Quote Link to comment https://forums.phpfreaks.com/topic/149314-looping-issue-for-phpxml/#findComment-784603 Share on other sites More sharing options...
Psycho Posted March 15, 2009 Share Posted March 15, 2009 Show 1) Your latest code, 2) What the current output looks like and 3) what you want the output to look like Quote Link to comment https://forums.phpfreaks.com/topic/149314-looping-issue-for-phpxml/#findComment-784922 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.