FatFree pass array to a view using Jig data mapper - view

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?

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>

Related

how to sort nested array in laravel collection

I'm trying to sort nested collection but i can't get correct results.
category tags collection
[
{
"id": 1,
"name": "Age",
"tags": [
{
"id": 1,
"name": "18+"
},
{
"id": 2,
"name": "21+"
},
{
"id": 3,
"name": "25+"
},
{
"id": 4,
"name": "30+"
},
{
"id": 5,
"name": "17+"
},
{
"id": 6,
"name": "16+"
},
{
"id": 7,
"name": "26+"
},
{
"id": 8,
"name": "13+"
},
{
"id": 9,
"name": "24+"
},
{
"id": 10,
"name": "20+"
},
{
"id": 11,
"name": "19+"
}
],
}
]
i'm trying to sort tags:
<?php
$categoryTagsSorted = $categoryTags->transform(
function ($categoryTag) {
$sortedTags = $categoryTag->tags->sortBy('name');
// $sortedTags->values() return correct sorted array
$categoryTag->tags = $sortedTags->values();// < - this not work
return $categoryTag;
}
);
$categoryTagsSorted returns untouched results, but when i assign $sortedTags
to $categoryTag->newField = $sortedTags->values() i've got sorted tags in newField.
Question is how to return collection with sorted tags?
you can try this :
$categoryTagsSorted = $categoryTags->each(
function ($categoryTag) {
$categoryTag = $categoryTag->tags->sortBy('name');
}
);
In your code I believe you have misunderstood the use of the transform() method. It is used to iterate over each item in the collection and replaces the item with the value you give it in the callback. In your code you have also simply returned the original value without applying any 'transformation' to it. If you wish to alter the value of tags on the object you could try reassigning the object property:
$categoryTags->tags = $categoryTags->tags->sortBy('name');
Read the docs on the transform method here: https://laravel.com/docs/9.x/collections#method-transform#
Update
I think you can simply use map and re-assign the tags property to accomplish the solution:
$categoryTags->map(function($categoryTag) {
$categoryTag->tags = $categoryTag->tags->sortBy('name');
return $categoryTag;
});
The following test code worked OK for me...
$categoryTags = collect([
(object) [
'id' => 1,
'name' => 'Age',
'tags' => collect([
['id' => 1, 'name' => '18+'],
['id' => 2, 'name' => '21+'],
['id' => 3, 'name' => "25+"],
['id' => 4, 'name' => "30+"],
['id' => 5, 'name' => "17+"]
])
]
]);
$categoryTags->map(function($categoryTag) {
$categoryTag->tags = $categoryTag->tags->sortBy('name');
return $categoryTag;
});
var_dump($categoryTags);
Output:
object(Illuminate\Support\Collection)#1029 (1) {
["items":protected]=>
array(1) {
[0]=>
object(stdClass)#1030 (3) {
["id"]=>
int(1)
["name"]=>
string(3) "Age"
["tags"]=>
object(Illuminate\Support\Collection)#1026 (1) {
["items":protected]=>
array(5) {
[4]=>
array(2) {
["id"]=>
int(5)
["name"]=>
string(3) "17+"
}
[0]=>
array(2) {
["id"]=>
int(1)
["name"]=>
string(3) "18+"
}
[1]=>
array(2) {
["id"]=>
int(2)
["name"]=>
string(3) "21+"
}
[2]=>
array(2) {
["id"]=>
int(3)
["name"]=>
string(3) "25+"
}
[3]=>
array(2) {
["id"]=>
int(4)
["name"]=>
string(3) "30+"
}
}
}
}
}
}
=> null
Try it out here: https://web.tinkerwell.app/#/snippets/e2c93c20-5706-4ffd-bcc0-601aae861c8b (takes a min to load)

Not able to display from controller to view in codeigniter

Hi I'm Trying to display results from controller to view in Codeigniter. I fetching the results from model to controller and then displaying the details to map and inside div.
I'm using biostall for displaying on map.It is working perfectly.Also i want to display inside div also which is not working. Here is my Code..
My Model Code..
function getRegaddress($citycheck,$lat,$lng,$itemid,$hotelid) {
$return =array();
// Here goes Query....
//$query = mysql_query($results);
if ($query->num_rows() >0) {
foreach ($query->result() as $row) {
array_push($return, $row);
}
// return $query->result();
}
return $return;
}
}
Here The Controller code
$data = $this->maps_model->getRegaddress($citycheck,$lat,$lng,$itemid,$hotelid);
$data['hotelname'] = $hotelname;
$config['center'] = $lat.",".$lng;
$this->load->library('googlemaps');
$this->googlemaps->initialize($config);
foreach ($data as $coordinate) {
// $data = array();
// $data['values']=$coordinate->hotelname;
$marker = array();
$marker['title'] = $coordinate->hotelname;
$marker['position'] = $coordinate->lat.','.$coordinate->lng;
$this->googlemaps->add_marker($marker);
}
$data = array();
$data['map'] = $this->googlemaps->create_map();
$this->load->view('templates/_header');
$this->load->view('content/temp-view', $data);
$this->load->view('templates/footer');
View Code
<?php echo $map['js']; ?>
<div class="col-lg-8">
<div class="media">
<h3 class="media-heading text-danger-theme"><?php echo $hotelname; ?></h3>
<?php echo $map['html']; ?>
</div>
</div>
</div>
</div>
</div>
In the result ... The map marker is showing the details from database. But When I display the delalername on the div it is showing an error like ...
Severity: Notice
Message: Trying to get property of non-object
Filename: controllers/mapcontrol.php
Line Number: 84
A PHP Error was encountered
Severity: Notice
Message: Trying to get property of non-object
Filename: controllers/mapcontrol.php
Line Number: 84
Why i'm getting this error? Anyone Knows Please Help ...
Here is the structure of output
array(3) { [0]=> object(stdClass)#23 (15) { ["hotelid"]=> string(1) "2" ["hoteltype"]=> string(1) "3" ["hotelname"]=> string(18) "Sreekrishna food" ["dealerloc"]=> string(27) "Bengaluru, Karnataka, India" ["lat"]=> string(18) "12.971598700000000" ["lng"]=> string(18) "77.594562699999980" ["dealercity"]=> string(9) "Bengaluru" ["dealeraddress"]=> string(27) "Bengaluru, Karnataka, India" ["type"]=> string(2) "ar" ["stype"]=> string(3) "ggs" ["email"]=> string(15) "local#gmail.com" ["mobile"]=> string(10) "9947485214" ["phone"]=> string(10) "9947485214" ["website"]=> string(0) "" ["distance"]=> string(22) "0.00005899369716644287" } [1]=> object(stdClass)#24 (15) { ["hotelid"]=> string(1) "3" ["hoteltype"]=> string(1) "3" ["hotelname"]=> string(15) "Aey food" ["dealerloc"]=> string(42) "Shivaji Nagar, Bengaluru, Karnataka, India" ["lat"]=> string(18) "12.985650300000000" ["lng"]=> string(18) "77.605692699999960" ["dealercity"]=> string(13) "Shivaji Nagar" ["dealeraddress"]=> string(42) "Shivaji Nagar, Bengaluru, Karnataka, India" ["type"]=> string(2) "ar" ["stype"]=> string(3) "ggs" ["email"]=> string(9) "std#ff.il" ["mobile"]=> string(10) "9947485214" ["phone"]=> string(10) "9947485214" ["website"]=> string(0) "" ["distance"]=> string(18) "1.2265085392649464" } [2]=> object(stdClass)#25 (15) { ["hotelid"]=> string(1) "1" ["hoteltype"]=> string(1) "3" ["hotelname"]=> string(17) "Super - foods" ["dealerloc"]=> string(47) "FM Cariappa Colony, Bengaluru, Karnataka, India" ["lat"]=> string(18) "12.977923000000000" ["lng"]=> string(18) "77.612083499999930" ["dealercity"]=> string(18) "FM Cariappa Colony" ["dealeraddress"]=> string(47) "FM Cariappa Colony, Bengaluru, Karnataka, India" ["type"]=> string(2) "ar" ["stype"]=> string(3) "ggs" ["email"]=> string(11) "ddd#gfgf.in" ["mobile"]=> string(10) "9947485214" ["phone"]=> string(10) "9947485214" ["website"]=> string(12) "www.music.in" ["distance"]=> string(18) "1.2580702750661623" } }

integrate symfony and ajax

I need to send data using ajax in Symfony2 action.
For this I use the following js code:
$.ajax({
type: "POST",
url: "{{ path('slch_create_slot',{ 'uuid': meeting.uuid })}}",
data: request,
cache: false,
success: function(){
alert(data);
location.href = "{{ path('slch_new_meeting_step2',{ 'uuid': meeting.uuid })}}";
}
});
Symfony2 side I use the following code:
if($request->isXmlHttpRequest()){
$json = $request->getContent();
$tableau = json_decode($json, true);
var_dump($tableau);
....
$response = new Response(json_encode(array('response' => 'ok')));
$response->headers->set('Content-Type', 'application/json');
return $response;
}
When retrieving the Symfony2 response I get the following results:
array(1) {
["slots"]=>
array(4) {
[0]=>
array(5) {
["hour"]=>
string(2) "14"
["minute"]=>
string(1) "0"
["day"]=>
string(2) "11"
["month"]=>
string(1) "1"
["year"]=>
string(4) "2015"
}
[1]=>
array(5) {
["hour"]=>
string(2) "14"
["minute"]=>
string(1) "0"
["day"]=>
string(2) "12"
["month"]=>
string(1) "1"
["year"]=>
string(4) "2015"
}
[2]=>
array(5) {
["hour"]=>
string(2) "14"
["minute"]=>
string(1) "0"
["day"]=>
string(2) "13"
["month"]=>
string(1) "1"
["year"]=>
string(4) "2015"
}
[3]=>
array(5) {
["hour"]=>
string(2) "14"
["minute"]=>
string(1) "0"
["day"]=>
string(2) "14"
["month"]=>
string(1) "1"
["year"]=>
string(4) "2015"
}
}
}
{"response":"ok"}
can you tell me why I have the request in the Symfony2 response ?
you're doing var_dump($tableau) which dumps the request. That's why you're receiving it. Get rid of that line and you won't receive it anymore. – acontell 1 hour ago

How read .json file in angular js

Here i want to read a .json file. I have to read it in controller. But i am getting while reading the .json file.
quiz.json:
[
{
"data":{
"questions":{
"level":[
{
"question":[
{
"title":"What does SAP® stand for?",
"answer":[
"Services for Application Programming",
{
"_correct":"1",
"__text":"Systems, Applications, and Programs"
},
"Sino-American Peace",
"Statistical Analysis Program"
]
},
{
"title":"What does Tcode mean?",
"answer":[
"Television Code",
"Translation Code",
"Transition Code",
{
"_correct":"1",
"__text":"Transaction Code"
}
]
},
}
}
]
I tried to read i got Unexpected token /. Can anyone suggest how to read it?
The JSON you posted was incorrect.
This is the format that needs to be their:
JSON:
$scope.questions = [
{
"data":{
"questions":{
"level":[
{
"question":[
{
"title":"What does SAP® stand for?",
"answer":[
"Services for Application Programming",
{
"_correct":"1",
"__text":"Systems, Applications, and Programs"
},
"Sino-American Peace",
"Statistical Analysis Program"
]
},
{
"title":"What does Tcode mean?",
"answer":[
"Television Code",
"Translation Code",
"Transition Code",
{
"_correct":"1",
"__text":"Transaction Code"
}
]
}
]
}
]
}
}
}
];
and the html that I used to traverse the JSON in angular:
<div ng-app>
<div ng-controller = "test">
<div ng-repeat="data1 in questions">
<div ng-repeat="question in data1.data.questions.level">
<div ng-repeat="levelQuest in question.question">
{{levelQuest.title}}
</div>
</div>
</div>
</div>
</div>
Working Demo
You could paste your JSON structure here - http://jsonformatter.curiousconcept.com/
After pasting you will see that JSON has some errors in its structure.
Correct JSON will be:
[
{
"data":{
"questions":{
"level":[
{
"question":[
{
"title":"What does SAP® stand for?",
"answer":[
"Services for Application Programming",
{
"_correct":"1",
"__text":"Systems, Applications, and Programs"
},
"Sino-American Peace",
"Statistical Analysis Program"
]
},
{
"title":"What does Tcode mean?",
"answer":[
"Television Code",
"Translation Code",
"Transition Code",
{
"_correct":"1",
"__text":"Transaction Code"
}
]
}
]
}
]
}
}
}
]

How to translate an array of values from DB

I have an array of values that get from DB:
$repairs = DB::table('repair_parts')->orderBy('repair', 'asc')
->where('status_id', '1')->distinct()->lists('repair', 'id');
I want to ttranslate those values in another lang. To display in the view:
{{ Form::select('repair', $repairs, isset($v->repair_id) ? $v->repair_id : '', array(
'class' => 'form-control',
'id'=>'repair_field',
'placeholder' => Lang::get('messages.repair')))
}}
the problem is that I can't translate them with Lang::get('repair'.$repairs). Is there any other way to do it?
EDIT
array(13) { [22]=> string(2) "dd" [23]=> string(3) "fff" [21]=> string(4) "Test" [5]=> string(4) "test" [13]=> string(4) "test" [7]=> string(5) "test3" [14]=> string(5) "test4" [15]=> string(5) "test6" [16]=> string(5) "test6" [18]=> string(5) "test6" [19]=> string(5) "test6" [17]=> string(5) "test6" [20]=> string(5) "Tires" }
Solved. What I did was append to the values of the array
array_walk($repairs, function(&$value, $key) { $value = Lang::get('repair.'.$value); });
and I got translated values.

Resources