Datacollections are a great way of organizing similar data like for example faq entries, customer reviews, etc..
If you have a FAQ datacollection for example, you can make an infinite amount of entries within this datacollection, representing your FAQ entries. All of these entries will have the same amount and types of elements that you have specified. Remember that a datacollection consists of entries. The datacollection holds the global settings and the entries within the datacollection hold the specific FAQ entries in this example.
Adding a datacollection only requires a name. After adding the datacollection, you can click 'configure' and then 'add element'.
Adding an element to a datacollection works in the same way as when you would add an element to a section or form for example. You simply choose the type of element, specify a name and optionally a tooltip and validation.
These elements will be presented to the content manager when they add a datacollection entry.
Then click save and the datacollection is ready for use.
To add a datacollection entry, go to the specific datacollection and click 'add entry'.
Now all of the elements you added to this datacollection are presented.
When the user has completed all required fields, and saves the entry, it will be stored.
Remember that new entries are unpublished by default, meaning they are not visible on the website. If you want to publish an entry, edit the entry and toggle the published status.
Datacollections are only usefull if you can include them in you sections. This is how you can include the datacollection data in your section(s).
<?php foreach($webigniter->getDatacollectionEntries(name: 'FAQ', randomOrder: true, limit: 10) as $faq_item):?> <h1><?=$faq_item['data']['question'];?></h1> <p><?=$faq_item['data']['answer'];?></p> <?php endforeach; ?>
Let's brake it down into piecies.
The PHP foreach statement tells the section to loop through all entries and store each entry in the variable
Within the foreach a Webigniter method is called:
$webigniter->getDatacollectionEntries(name: 'FAQ', randomOrder: true, limit: 10).
This will get the FAQ entries from the datacollection with the name 'FAQ',
randomOrder: true means that they will be presented in a random order, can be set to false as well to get them in the order in which they are present in the CMS.
Limit: 10 means that it will retreive no more that 10 entries of this datacollection. You can also add: ,
unpublished: true to also retreive unpublished entries.
Now that we have our faq items stored in
$faq_item, we can start displaying our entries.
All data provided by the user in the CMS is stored in
$faq_item['data'], this means you can display the entry data, by using
<?=$faq_item['data']['question'];?> to display the element in the entry with the name 'question'. In this case the element 'question' is a simple text element, so I can access by just using 'question', other element types may need a different approach, check the manual to find out how each elementtype should be retreived.
There are also 2 extra elements added by default, 'created_at' and 'updated_at'. These two can be displayed using