I try to fill the select field with choices from an xml file. Is there a way to do this? I have tried without luck… I get the choices, but cant make it to save to database..
MY XML:`<?php
if( ! $xml = simplexml_load_file('http://www.dx.no/ebillett/dx_forestillinger.php?p_id=198&type=XML') )
{
echo 'unable to load XML file';
}
else
{
foreach( $xml as $Show )
{
$field['choices'].= $k= $Show-> TicketURL. ' : ' . $v = $Show->MovieTitle.”\n<br />”;
}
echo $field['choices'];
}
?> `
SELECT FIELD CODE:
// loop through values and add them as options
if( is_array($field[‘choices’]) )
{
foreach( $field[‘choices’] as $key => $value )
{
if( $optgroup )
{
// this select is grouped with optgroup
if($key != ”) echo ‘<optgroup label=”‘.$key.'”>’;
if( is_array($value) )
{
foreach($value as $id => $label)
{
$selected = in_array($id, $field[‘value’]) ? ‘selected=”selected”‘ : ”;
echo ‘<option value=”‘.$id.'” ‘.$selected.’>’.$label.'</option>’;
}
}
if($key != ”) echo ‘</optgroup>’;
}
else
{
$selected = in_array($key, $field[‘value’]) ? ‘selected=”selected”‘ : ”;
echo ‘<option value=”‘.$key.'” ‘.$selected.’>’.$value.'</option>’;
}
}
Hi @eiriksk
Thanks for the code, bu i don’t quite understand what the issue is.
Can you please clarify the question?
What does and doesn’t work?
Where is your code found?
Is your code within an action?
Why is it broken into 2 parts which would cause PHP errors?
Why have you copied / pasted source code from the select field type?
Thanks
E
Just forget the code. I need a way to create choices dynamically to the select field. Choices are coming from an XML file. (See my code for XML). Is there a easy way to do this?
Hi @eiriksk
Checkout this documentation article for code examples of how to populate a select field’s list:
http://www.advancedcustomfields.com/resources/tutorials/dynamically-populate-a-select-fields-choices/
You should be able to adapt the code to your xml reader.
Be sure to debug your code as you go.
Thanks
E