FatFree pass array to a view using Jig data mapper

427 Views Asked by At

I have data/faqs.json like this.

{
    "section-1" : { 
        "sub-section-1" : {
            "557aef62e0629": {
                "question": "how are you?",
                "answer": "fine.. you?"
            },

            "557af3d40b041": {
                "question": "Question",
                "answer": "Answer to question"
            }
        },
        "sub-section-2": {
            "557af3d80b041": {
                "question": "another section question?",
                "answer": "another section answer?"
            }
        }
    },
    "section-2" : {
            "557af32d201f6": {
                "question": "Question",
                "answer": "Answer to question"
            },
            "557af33c7c60e": {
                "question": "Question",
                "answer": "Answer to question"
            }
    }
}

and in my controller method:

    $faqs = [];

    $mapper = new \DB\Jig\Mapper($this->db, 'faqs.json');
    $mapper->load();

    while (!$mapper->dry()) {
        $faqs[] = $mapper->cast();
        $mapper->next();
    }
    $this->f3->set('faqdata', $faqs);

to send faqdata to the view.

In my view I tried:

<repeat group="{{ @faqdata[1] }}" key="{{ @key }}" value="{{ @faqs }}">
    <div>
        <p><span><b>{{ @key }}</b></span></p>
        <repeat group="{{ @faqs }}" key="{{ @k }}" value="{{ @v }}">
            <dt>{{ @k }}</dt>
            <dd>{{ @v }}</dd>
        </repeat>
    </div>
</repeat>

to read only section-2 of faqs, but I get the error:

Invalid argument supplied for foreach()

Why is @faqs considered as invalid argument to foreach()?

EDIT:

This is what var_dump shows:

array(2) {
  [0]=>
  array(3) {
    ["sub-section-1"]=>
    array(5) {
      ["557aef62e0629"]=>
      array(2) {
        ["question"]=>
        string(12) "how are you?"
        ["answer"]=>
        string(11) "fine.. you?"
      }
      ["557af0d114839"]=>
      array(2) {
        ["question"]=>
        string(35) "hi there, this is quesiton number 2"
        ["answer"]=>
        string(19) "this is answer no 2"
      }
      ["557af32d201f6"]=>
      array(2) {
        ["question"]=>
        string(8) "Question"
        ["answer"]=>
        string(18) "Answer to question"
      }
      ["557af33c7c60e"]=>
      array(2) {
        ["question"]=>
        string(8) "Question"
        ["answer"]=>
        string(18) "Answer to question"
      }
      ["557af3d40b041"]=>
      array(2) {
        ["question"]=>
        string(8) "Question"
        ["answer"]=>
        string(18) "Answer to question"
      }
    }
    ["sub-section-2"]=>
    array(1) {
      ["557af3d80b041"]=>
      array(2) {
        ["question"]=>
        string(25) "another section question?"
        ["answer"]=>
        string(23) "another section answer?"
      }
    }
    ["_id"]=>
    string(9) "section-1"
  }
  [1]=>
  array(3) {
    ["557af32d201f6"]=>
    array(2) {
      ["question"]=>
      string(8) "Question"
      ["answer"]=>
      string(18) "Answer to question"
    }
    ["557af33c7c60e"]=>
    array(2) {
      ["question"]=>
      string(8) "Question"
      ["answer"]=>
      string(18) "Answer to question"
    }
    ["_id"]=>
    string(9) "section-2"
  }
}

So isn't section-2 array of arrays?

1

There are 1 best solutions below

4
On BEST ANSWER

That's because @faqdata[1] contains the index of the array: _id => 'section-2'. So you can't just loop through its properties. You should call the expected properties instead (question and answer).

Anyway since your intent is to get the whole data array, a direct call to the Jig::read would be simpler. You don't need the mapper for that. See:

$faqs = $this->db->read('faqs.json');

Now $faqs['section-2'] contains the 2nd section.

UPDATE:

In order to display that kind of data, you need a recursive view. This can be achieved, using the with attribute of the <include> tag. Cf. the docs.

Here's how we could do it in your case, assuming your view is named faqs.html:

<ul>
<repeat group="@faqs" key="@section" value="@data">
    <li>
        <span>{{ @section }}</span>
        <check if="($faq=reset(@data)) && isset(@faq.question)">
            <true>
                <ul>
                    <repeat group="@data" value="@faq">
                        <li>
                            {{ @faq.question }}
                            =>
                            {{ @faq.answer }}
                        </li>
                    </repeat>
                </ul>
            </true> 
            <false>
                <include href="faqs.html" with="faqs={{ @data }}"/>
            </false>
        </check>
    </li>
</repeat>
</ul>