Jump to content

Creating XML output


chuy08

Recommended Posts

I am trying to create an XML file using a PHP script.  The XML File looks something like this:

<?xml version="1.0" encoding="ISO-8859-1"?>
<ConsumerDirectXML>
<UserParams id="1" BrokerID="1"/>
        <LoanParams>
        <FirstLeinAmt>500000</FirstLeinAmt>
        <ChooseProductTypeID>1</ChooseProductTypeID>
        </LoanParams>
</ConsumerDirectXML>

The part that I don't understand is the following.  I have a series of checkboxes with each box having a different value.  If a user were to select 3 checkboxes I need to iterate through the array and write to the ChooseProductTypeID field in the XML file as such,  <ChooseProductTypeID>1,5,7</ChooseProductTypeID>.  I have been able to print out the array variable $num using the following foreach loop:

foreach ($total as $num) {
print("$num,"); }

How would I get this string into the designated XML tag?

Currently I am able to write to this XML file using textboxes and the following code for different XML tags in same file.  How would I modify what I am currently using for textboxes to use for the checkboxes while utilitzing the foreach loop.  Below is the method by which I am writting to my XML file:

        if ($node_name == "FirstLeinAmt") {
          if ($value == $firstlein) {
            // Do nothing
          } else {
            $fl_textnode = $child_obj->firstChild;
            $new_firstlein = $doc->createTextNode($firstlein);
            $child_obj->replaceChild($new_firstlein, $fl_textnode);
          }
        }
        foreach ($total as $num) {
        if ($node_name == "ChoosedProductTypeID") {
          if ($value == $num) {
            // Do nothing
          } else {
            $pt_textnode = $child_obj->firstChild;
            $new_num = $doc->createTextNode("$num,");
            $child_obj->replaceChild("$new_num", "$pt_textnode");
          }}
...


Any help appreciated.
Link to comment
Share on other sites

Here's is a code I was given here for a for-each statement:

Someone may be able to help you along because the test says:

Warning: Invalid argument supplied for foreach()

[code]<?php
       
$xmldoc .= '<?xml version="1.0" encoding="UTF-8"?>'."\n";
   
$xmldoc .= " <ConsumerDirectXML>\n";

foreach ($total as $num) {

$xmldoc .= " <UserParams id=1 BrokerID=1/>\n";
$xmldoc .= " <LoanParams>\n";
$xmldoc .= " <FirstLeinAmt>500000</FirstLeinAmt>\n";
$xmldoc .= " <ChooseProductTypeID>".$_POST["num"]."</ChooseProductTypeID>\n";
$xmldoc .= " </LoanParams>\n";
       
}
$xmldoc .=  " </ConsumerDirectXML>\n";

$xmldoc;

?>[/code]

But at least theres a start.
Link to comment
Share on other sites

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.