3

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>';
        }
    }
  • Could you use var_dump($attribs->addresses); and add the results to your question? – Lodder Jun 15 '15 at 16:02
  • Hi @Lodder, I have updated my question, thanks :) – DesignSubway Jun 15 '15 at 19:47
  • 1
    Maybe try using true as an argument in the json_decode to make it an array, then do foreach($attribs['addresses'] as .... Did you have a look at my answer I wrote a while back? joomla.stackexchange.com/questions/9721/… – Lodder Jun 15 '15 at 19:50
  • Hi @Lodder, I think I am getting somewhere, please have a look at my edit :) – DesignSubway Jun 15 '15 at 20:09
  • 1
    You solved it then? :) Don't forget to write your solution as an answer below ;) – Lodder Jun 15 '15 at 20:30
2

A big thank you to @Lodder (as always) for helping me with this. I don't know if my answer is the best way of acheiving the desired result but it works and that's what matters.

Here's what I used:

$attribs = json_decode($this->item->attribs);
$addresses = $attribs->addresses;
$addresses = json_decode($addresses);
foreach ($addresses as $index=>$addresses) 
    {   
        foreach($addresses as $address){
            echo $address;
        }
    }

Hopefully this will help someone else.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.