I am using a J3.x Plugin called Aixeena CCK http://extensions.joomla.org/extensions/extension/authoring-a-content/content-construction/aixeena-cck which adds extra fields to articles using standard JForm field types.
I have set up a repeatable form field type https://docs.joomla.org/Repeatable_form_field_type which simply has text boxes as follows:
<field name="addresses" type="Repeatable" icon="list" description="" label="Addresses" default="">
<fields name="params">
<fieldset hidden="true" name="addresses_modal" repeat="true">
<field name="address" label="Address" size="60" type="text" />
</fieldset>
</fields>
</field>
In my article override I have the following code:
<?php
$attribs = json_decode($this->item->attribs);
print_r($attribs->addresses);
?>
This returns:
{"address":["My Address 1","My Address 2"]}
"Great!" I thought, I'll just use a foreach loop to get the individual values. So I used the following code:
<?php
foreach($attribs->addresses as $address){
echo $address;
}
?>
However, that didn't work. I just got an error saying: "Warning: Invalid argument supplied for foreach()".
So my question is; how do I get each individual value stored in this string?
EDITED
Results of var_dump($attribs->addresses);
string(135) "{"address":["My Address 1","My Address 2"]}"
EDITED AGAIN
Thanks to @Lodder I am now using:
$attribs = json_decode($this->item->attribs);
$addresses = $attribs->addresses;
$addresses = json_decode($addresses, true);
foreach ($addresses as $index=>$address)
{
echo $address[0] . '<br>';
echo $address[1];
}
This returns: My Address 1 My Address 2
This is great but my question now is; how do I automatically perform the foreach loop? What I mean is; at the moment I have to add echo $address[1]
then echo $address[2]
etc. But the point of a repeatable field is that the user can add as many fields as they want. I hope I am making sense :)
SOLVED:
$attribs = json_decode($this->item->attribs);
$addresses = $attribs->addresses;
$addresses = json_decode($addresses);
foreach ($addresses as $index=>$addresses)
{
foreach($addresses as $address){
echo '<p>'.$address.'</p>';
}
}
var_dump($attribs->addresses);
and add the results to your question?true
as an argument in thejson_decode
to make it an array, then doforeach($attribs['addresses'] as ...
. Did you have a look at my answer I wrote a while back? joomla.stackexchange.com/questions/9721/…