1

OK, I have tried the info from another suggestion on stackexchange, but I seem to be missing something.

I have a subform in an admin view for a component I am working on (they were previously repeatable fields). Admin works perfect, but I am having trouble getting the json to display correctly on the frontend site view.

The fields that are being pulled into the view are in a layout, so it's a little different then in the site view ($displayData instead of $items).

In the db I have a field called appliances, and 4 items in the field are {"appliances1":{"appliance_type":"Dishwasher"},"appliances2":{"appliance_type":"Range"},"appliances3":{"appliance_type":"Microwave"},"appliances4":{"appliance_type":"Washer/Dryer"}}

OK. That looks good and properly formatted.

On the frontend, in the layout for a view, I have:

<?php
print_r($displayData->appliances);
foreach ($displayData->appliances as $item) : ?>
<div>
<p>
<?php echo $displayData->appliance_type; ?>
</p>
</div>
<?php endforeach; ?>

the print_r shows:

Appliances Array ( [appliances1] => Array ( [appliance_type] => Dishwasher ) [appliances2] => Array ( [appliance_type] => Range ) [appliances3] => Array ( [appliance_type] => Microwave ) [appliances4] => Array ( [appliance_type] => Washer/Dryer ) )

OK. again, that looks good. I'll remove that as soon at its working properly.

The problem is that

<p>
<?php echo $displayData->appliance_type; ?>
</p>

doesn't display anything.

I can see in the code, that it's doing the foreach correctly, but it's not grabbing anything from the array:

            Array
(
    [appliances1] => Array
        (
            [appliance_type] => Dishwasher
        )

    [appliances2] => Array
        (
            [appliance_type] => Range
        )

    [appliances3] => Array
        (
            [appliance_type] => Microwave
        )

    [appliances4] => Array
        (
            [appliance_type] => Washer/Dryer
        )

)
<div>
<p>
</p>
</div>
<div>
<p>
</p>
</div>
<div>
<p>
</p>
</div>
<div>
<p>
</p>
</div>

Any thoughts on what I may be missing?

1
  • Please could you format the code in your question so it's a little easier to read?
    – Lodder
    Commented Sep 26, 2017 at 11:34

1 Answer 1

2

There are two basic errors. 1. You assign each item in the array to the variable $item and then you don't use it. 2. You use arrow syntax to refer to an array. Having errors displaying would have helped you work through this.

foreach ($displayData->appliances as $item) : ?>
<div>
<p>
<?php echo $item['appliance_type']; ?>
</p>
</div>
<?php endforeach; ?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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