Custom code syntax for writing to ZOO

gilliancowe 13 Apr, 2012
Hi,

I have a custom code event that is writing a ZOO item into the database table.

I have all the fields adding correctly except one. I would appreciate some help with the syntax.

I have a field called 'elements' I need to place the following code into the elements field before writing to the database.

<?xml version="1.0" encoding="utf-8"?>
<elements>
<prevnext identifier="c258dff1-4954-4a6d-90b3-7b1f99d90811"/>
<text identifier="07f1c1bd-750e-4dd7-88a2-2e775eb529d4">
  <value/>
</text>
<image identifier="43923c04-6eb4-4b3c-9cf9-abfb1f69f902">
  <file><![CDATA[VALUE]]></file>
  <title/>
  <link/>
  <target/>
  <rel/>
  <lightbox_image/>
  <width><![CDATA[400]]></width>
  <height><![CDATA[300]]></height>
</image>
<select identifier="b8acde2a-2b49-4ce6-9cfd-1da2a3ad19fe">
  <option><![CDATA[VALUE]]></option>
</select>
<select identifier="b1399fc2-2170-4631-bc20-453216920c50">
  <option><![CDATA[VALUE]]></option>
</select>
<select identifier="f9f68eb2-b135-4de9-afc6-1d12b3668e0b">
  <option><![CDATA[VALUE]]></option>
</select>
<select identifier="7a292433-652e-4f02-95bc-199cf4e9641a">
  <option><![CDATA[VALUE]]></option>
</select>
<select identifier="b61d18a1-fefd-4f61-8ac9-661eddd2b078">
  <option><![CDATA[VALUE]]]></option>
</select>
<select identifier="d9c845d8-4fae-4b38-8f54-173acf0a432f">
  <option/>
</select>
<select identifier="e8120261-ff65-47c8-a601-f38fcf119c80">
  <option><![CDATA[VALUE]]></option>
</select>
<select identifier="12b073d2-b298-4bd7-999e-87a0ac9ec715">
  <option><![CDATA[VALUE]]></option>
</select>
<textarea identifier="a8c5cb15-7cef-4ca5-9dd4-8f6b8511cd59">
  <value><![CDATA[VALUE]]></value>
</textarea>
<select identifier="fa367fcf-7a9f-486d-a4a0-282edd465612">
  <option><![CDATA[VALUE]]></option>
</select>
<date identifier="19ab143a-8f10-4212-85ff-8e1234424b6b">
  <value><![CDATA[VALUE]]></value>
</date>
<date identifier="bb6f8291-7d2c-4b8c-889e-afbe3b1d3e51">
  <value/>
</date>
<text identifier="cf5f9bbf-0802-4d07-af47-82666ab80221">
  <value><![CDATA[VALUE]]></value>
</text>
<text identifier="61f0fbd2-ce37-4818-9a31-d1ff4afc4f84">
  <value><![CDATA[VALUE]]]></value>
</text>
<link identifier="babcb356-51d5-4f31-ae9d-3262392fb6e8">
  <value/>
  <text/>
  <target/>
  <custom_title/>
  <rel/>
</link>
<text identifier="35176631-21b0-4ca2-a759-4fddd2273fff">
  <value><![CDATA[VALUE]]></value>
</text>
</elements>


In the code above 'VALUE' is a piece of information from the form.

So I have set up a hidden field on the form called 'elements'.

In my custom code I have:-

$form->data['elements'] = '<?xml version="1.0" encoding="utf-8"?>
<elements>
<prevnext identifier="c258dff1-4954-4a6d-90b3-7b1f99d90811"/>
<text identifier="07f1c1bd-750e-4dd7-88a2-2e775eb529d4">
  <value/>
</text>
<image identifier="43923c04-6eb4-4b3c-9cf9-abfb1f69f902">
  <file><![CDATA[VALUE]]></file>
  <title/>
  <link/>
  <target/>
  <rel/>
  <lightbox_image/>
  <width><![CDATA[400]]></width>
  <height><![CDATA[300]]></height>
</image>
<select identifier="b8acde2a-2b49-4ce6-9cfd-1da2a3ad19fe">
  <option><![CDATA[VALUE]]></option>
</select>
<select identifier="b1399fc2-2170-4631-bc20-453216920c50">
  <option><![CDATA[VALUE]]></option>
</select>
<select identifier="f9f68eb2-b135-4de9-afc6-1d12b3668e0b">
  <option><![CDATA[VALUE]]></option>
</select>
<select identifier="7a292433-652e-4f02-95bc-199cf4e9641a">
  <option><![CDATA[VALUE]]></option>
</select>
<select identifier="b61d18a1-fefd-4f61-8ac9-661eddd2b078">
  <option><![CDATA[VALUE]]]></option>
</select>
<select identifier="d9c845d8-4fae-4b38-8f54-173acf0a432f">
  <option/>
</select>
<select identifier="e8120261-ff65-47c8-a601-f38fcf119c80">
  <option><![CDATA[VALUE]]></option>
</select>
<select identifier="12b073d2-b298-4bd7-999e-87a0ac9ec715">
  <option><![CDATA[VALUE]]></option>
</select>
<textarea identifier="a8c5cb15-7cef-4ca5-9dd4-8f6b8511cd59">
  <value><![CDATA[VALUE]]></value>
</textarea>
<select identifier="fa367fcf-7a9f-486d-a4a0-282edd465612">
  <option><![CDATA[VALUE]]></option>
</select>
<date identifier="19ab143a-8f10-4212-85ff-8e1234424b6b">
  <value><![CDATA[VALUE]]></value>
</date>
<date identifier="bb6f8291-7d2c-4b8c-889e-afbe3b1d3e51">
  <value/>
</date>
<text identifier="cf5f9bbf-0802-4d07-af47-82666ab80221">
  <value><![CDATA[VALUE]]></value>
</text>
<text identifier="61f0fbd2-ce37-4818-9a31-d1ff4afc4f84">
  <value><![CDATA[VALUE]]]></value>
</text>
<link identifier="babcb356-51d5-4f31-ae9d-3262392fb6e8">
  <value/>
  <text/>
  <target/>
  <custom_title/>
  <rel/>
</link>
<text identifier="35176631-21b0-4ca2-a759-4fddd2273fff">
  <value><![CDATA[VALUE]]></value>
</text>
</elements>';


For testing purposes, just to get things working I have set VALUE to text values and not tried to use the form data. But ideally I would like VALUE to be set to things like $name, $address1, $address2 and so on. If the form data can't be used it doesn't matter as I can use default values then edit in ZOO, each record will need edited anyway before publishing.

Can anyone help with the syntax to set the value of 'elements', as the above isn't working.

Thanks,

Gillian
GreyHead 14 Apr, 2012
Hi gilliancowe,

Congratulations on getting data to write to ZOO, this has come up a few times in the forums here but I think that you are the first person to achieve it :-)

I'll try to give you two answers. The first is the simple one which is to replace VALUE in your code with '.$form->data['input_name'].' where input_name is the corresponding input name from the form.

The second answer is that you can build the XML file using the PHP SimpleXML or DOM libraries. This might be easier if the coding is complex. Unfortunately SimpleXML doesn't support CDATA* easily and the DOM library gets tedious. Here's an example of the first chunk of your file:
$dom = new DOMDocument('1.0');
$e = $dom->createElement('element');

$prevnext = $dom->createElement('prevnext');
$prevnext->setAttribute('identifier', 'c258dff1-4954-4a6d-90b3-7b1f99d90811');
$prevnext->appendChild($i);
$e->appendChild($prevnext);

$text = $dom->createElement('text');
$text->setAttribute('identifier', '07f1c1bd-750e-4dd7-88a2-2e775eb529d4');
$e->appendChild($text);

$image = $dom->createElement('image');
$image->setAttribute('identifier', '43923c04-6eb4-4b3c-9cf9-abfb1f69f902');
$file = $dom->createElement('file');
$file_data = $dom->createCDATASection($form->data['file']);
$file->appendChild($file_data);
$image->appendChild($file);
$title = $dom->createElement('title');
$image->appendChild($title);
$e->appendChild($image);
. . .

Bob

* There is a way round this - let me know if it would be useful.
gilliancowe 15 Apr, 2012
Thanks Bob. Haven't had a minute to get back to this but I will let you know if (when!!) I get this working.

Writing to the ZOO table isn't difficult it's building the 'elements' field that presents the problem.

Gillian
gilliancowe 15 Apr, 2012
Sorry Bob can I just check something here? Just so that I know we understand each other!

The XML is the same for every ZOO item I am creating, only where I have written the word 'VALUE' does the data vary.

So I have a table field called 'elements' and it contains the whole of the XML code (see screenshot), I don't need to build it up, I already know what it will be, all except the places I have typed 'VALUE', those are the variables. I can just set these to defaults if I can't insert the form values, for this project that will work fine. In other words I could use a piece of default XML code for 'elements'.

What I haven't got correct yet is the statement that sets 'elements' to the XML code.

Eg.
Do I write

$form->data['elements'] = '<?xml .......... </elements>';

or should I enclose it in brackets?

$form->data['elements'] = '(<?xml .......... </elements>)';

Both of which I have tried but they didn't work. I just need to understand how to get 'elements' to contain the large of code.

Hope this is clear! In short I need to set 'elements' to a chunk of XML code.

Gillian
GreyHead 16 Apr, 2012
Hi Gillian,

I wasn't clear how you were using the XML. It sounds as though you want the first version:

replace VALUE in your code with '.$form->data['input_name'].' where input_name is the corresponding input name from the form.


Bob
This topic is locked and no more replies can be posted.