I'm using an analog of Shopify and I'm stuck with syntax of Liquid.
I need to output in the template the field with an id product[product_field_values_attributes][][value]
So I need to write a loop to get the i value of this array.
I'm confused with this empty element in the brackets.
I've looked through the examples of loop syntax in Liquid but all of those arrays are simple and they are not my case.
For example, the Title field has id product[title] and in Liquid template i call this variable product.title and it works fine.
But all my tries to write a loop for this array failed.
Please, help to write a loop to get the values of the array stated above.
Try outputting the array directly onto the page using {{ product }} or {{ product[product_field_values_attributes] }} somewhere in the HTML. That will do a JSON-like string representation of the array. From there you can figure out what the keys for the array are.
I'm not sure what you're saying about the i value. You don't reference i anywhere else in your question. If you can clarify that then we can see if something can be done about it.
Related
I'm making a scheduling app, and storing all the scheduled things in firebase with arrays. When I try to schedule something with the same string value, it fails and doesn't add it to the array. I don't know if this is something in swift I can edit, or if it's a firebase setting.
If it's something in swift, here's the code updating the array:
doc.updateData([
"Instructor": FieldValue.arrayUnion(["\(scheduleinstructor)"])
])
If it's something in firebase, could someone please explain a way around this or a simple fix I overlooked?
According to the documentation on adding items to an array:
arrayUnion() adds elements to an array but only elements not already present
So the fact that the duplicate entry is not added is by design. If you want to allow that, you'll have to:
Read the document with the array from the databae.
Extract the array from the document into your application code.
Add the item to the array.
Write the entire modified array back to the database.
I'm trying to convert an Object array into an array without use "let item of array" on html, I already google a lot but nothing that I find works.
Why I don't use loops? because I pretend to display data inside a page that comes from a liteSQL database, so all the data that I extract from that it's an Object and sure I can display the data without issues if I use a loop like "let item of array" but in this case I just want to show information on the HTML like item.name or item.avatar
Thanks in advance for any help, If you guys need more information please let me know.
The database have students so every array have: name, age, avatar, etc, so I try to show some like a profile after they tap the name on the list
EDIT:
What you are receiving from your server is an array of objects.
array of objects ->
[{
key1:vlaue1,
key2:value2
},
{
key1:vlaue1,
key2:value2
}]
You refer the values here by using array[0]['key1'] array[0]['key2'] and so on
In your case, you are receiving only one object in the array, so simply use array[0].name array[0].age and so on
Thanks for the help, I actually found the solution by my self, I just need to use:
item: array = <array>{};
in order to use my object array as a simple array items without any loop on html
I am currently having trouble filling up an array of customClass.
I try to fill it with a jsonFile. During my json parsing (using swiftyJSON) i loop and fill my array.
The problem is, at the end of my loop, it is still empty. I tested it in different ways, and here is my code:
That's the file where the problem is. In my loop I fill an Annotation, that I add with append to my array. The problem is what my print return. Here is a part of it:
It's just a small part of a huge jsonfile. And, my tmpAnnot.name is correctly printed every iteration. But when it comes to my Array, nothing.
So I'm completly lost and hope you could help me ^^
(And for the information, here is my custom class) :
And btw, I tried to print my array.count, and it's nil too
Im so sorry if the question has been posted. I couldn't find it in the entire website.
Change your JSONAnnotationList declaration to be an non-optional and assign it an empty array
var JSONAnnotationList: [UGOAnnotation] = []
You see, you have never created an array so there was nothing to be printed.
The whole point of optionals is to use them sparingly, not everywhere.
my view page consist {{$data->attachment}} which render
[{"filename":"hello.jpg", "location":"/home/my_folder"}].
Here i tried to display file name in view page using
#foreach($data->attachment as $attachment)
$attachment->filename
#endforeach
which gives me
Invalid argument supplied for foreach()
i trying
{{$data->attachment->filename}}
which gives me
Trying to get property of non-object
what i'm doing wrong? how can i display filename? thanks.
As you mentioned in the comments, the value of $data->attachment is actually a string. In order to iterate over it in a loop you will need to convert it back to an array.
So if you change your loop to:
#foreach(json_decode($data->attachment) as $attachment)
{{ $attachment->filename }}
#endforeach
you should get what you want.
I would add that the correct place for this conversion should really be in your controller, not your view.
Your question was not entirely clear so this is all assuming you knew what you were doing when you used a foreach loop, meaning you actually have multiple entries in the $data->attachment array. If it is just the one attachment and you are really just trying to get the filename, then you don't need a loop, all you need is to say:
{{ json_decode($data->attachment)->filename }}
and again I would add that really that all belongs in your controller so that in your view you would end up with something like {{ $filename }}
I have been trying to access the values of the the array .. in the foreach loop but no luck, the first two calls get the values, but the ones in the foreach loop nothing.. comes empty although if i print the array all the values are there.
You never actually access the $dish variable in the foreach loop.
Try
$dish['Dish']['dish_name'];
instead of
$dishes['Dish']['dish_name'];
etc.
Additionally, you will get an error because the first element in your array is not a Dish, its a Dish_Category. Thus, either remove this element from the array, or use a simple if statement before you access the $dish such as:
if($dish['dish_name'])
//DO STUFF HERE
Also, there is no reason for so many
<?php ?>
tags. Couldn't you put the whole block of code in one?