Datacollections explained

Published: January 11, 2024

Data collections are a great way of organizing similar data, such as FAQ entries, customer reviews, etc.

What is a datacollection?

A data collection, as the name suggests, is a collection of data. This can be anything from FAQ entries, customer reviews, or perhaps employees for your 'Our team' pages. As long as it's data that you would like to gather on one page. For example, a blog does not use a data collection because blog articles are, in most cases, separate pages; the entries within a data collection won't produce actual pages. It's merely a way of organizing similar data in a convenient way. You can create as many data collections as you want and add as many entries within as you like. Each data collection has its own preset of data which is needed for the entries. For example, an FAQ data collection may only need a question and an answer field, where an employee's data collection may contain a name, job title, favorite quote, and an image, for example. Let's use this example to explore data collections a little deeper.

When configuring a datacollection

When you add and configure a data collection, you will need to inventory which information you need for each entry within the data collection. In our case, what do we want to know about our employees so we can create a cool overview of employees on our website? After we know what we want to show, we can configure the data collection and add elements to it. In our case, we will add four elements to the data collection:

  1. a TEXT element named 'name'
  2. a TEXT element named 'jobtitle'
  3. a TEXT element named 'favourite quote'
  4. a MEDIA element named 'image'

Now that we have set up our data collection, we can toggle the 'overview' toggler to specify which fields we want to see in the employees' overview in the CMS; this has no effect on the data collection behavior in your frontend files, which we will discuss in a bit.

Now that we have set up our elements, we can go and add some employees to the CMS. When adding an employee, these four fields will be requested to fill.

Getting datacollection data on your website

Now that we have set up and filled our employee's data collection, it's time to create our employee's page and request data from the CMS. A basic understanding of creating pages is required; read our manual on how to create your first page if you are not yet familiar with the Webigniter structure yet.

You can retrieve the employees in many ways, but before I explain what cool things you can do, let me start with a basic example of how to get (and loop through) your employees.

<?php foreach($webigniter->getDatacollectionEntries(name: 'Employees') as $employee):?>
   <h2><?=$employee['data']['name'];?></h2>
   <h3><?=$employee['data']['jobtitle'];?></h3>
   <p><?=$employee['data']['favourite-quote'];?></p>
   <img src='<?=$employee['data']['image']['link'];?>' alt='<?=$employee['data']['image']['link'];?>'>
<?php endforeach; ?>

This is the most basic way of getting the employees; this will use the getDatacollectionEntries() function to get all of the entries in the 'Employees' data collection. In some cases, this will just do, but there can be cases when you would like to have a little more control over how the data is retrieved. That's where the optional parameters come in handy...

These are the optional parameters for the getDatacollectionEntries() function:

  • unpublished: by default false, set to true to also retrieve unpublished entries.
  • orderBy: data collection field name to order the results by.
  • orderType: specify how you want to order the results (allowed values: ASC, DESC, and RAND).
  • filterBy: data collection field names you want to use for filtering (must be an array, ['name', 'jobtitle'] for example).
  • filterValue: specify the value the filtered fields should contain (also an array, ['john', 'mark'] for example.
  • limit: specify the amount of results you would like to show.

When you use, for example, this code, you will get a random list of 5 employees, whose name or favorite quote contains 'sky'.

<?php foreach($webigniter->getDatacollectionEntries(name: 'Employees', orderType: 'RAND', filterBy: ['name', 'favourite-quote'], filterValue: ['sky'], limit: 5) as $employee):?>
   <h2><?=$employee['data']['name'];?></h2>
   <h3><?=$employee['data']['jobtitle'];?></h3>
   <p><?=$employee['data']['favourite-quote'];?></p>
   <img src='<?=$employee['data']['image']['link'];?>' alt='<?=$employee['data']['image']['alt'];?>'>
<?php endforeach; ?>

As you can see, we can have endless possibilities in displaying our employees on the page.

Conclusion

This example was based on employees, but because you can configure the data collection with its elements any way you want, you can use it to store any kind of collection of data. When the CMS end user adds, deletes, or modifies this data, it will be directly presented on the website, without the need for a web developer to update the page.

The filtering options make this core function of Webigniter very useful for all sorts of collections of data.

Ready to get started?
Let us ignite your digital fire