Sorry for the noob questions. I do write in PHP and Javascript, but am struggling to use the examples to write a simple page to (for instance) list out a series of items in a collection :-/
All the examples (here and across the web) just show the data with console.log. I’ve mucked around trying to get data onto the page, but am finding it really long-winded and complicated.
I’m hoping someone can show be a few lines of PHP or Javascript that pulls a list of items from a collection, and/or lists the contents of all the fields in an item.
Hi, personally I think its best to use some kind of front end framework like React or Vue to display the data since its a lot easier to manage using a shared component/ layout template.
The basic idea is to store your initial fetch data to some kind of state or variable in your application. You can then map through the data and display it however you need. If you’re familiar with the basics of React, here’s a super simple example:
For the demo, I created a new collection called ‘people’ with 3 fields called ‘name’, ‘age’, and ‘isDriver’.
Which will return a javascript object for the collection. You will need to access the ‘entries’ object to use the data. Should look something like this for each individual entry:
Essentially you are looping through each single entry and displaying your field properties. Obviously you don’t have to use React or you can write everything in PHP, it’s whatever you prefer, but as long as you understand how to loop through arrays and objects thats the basic idea.
If you run Cockpit on the same server as your frontend code you can also directly include cockpit into your project and access all its functionality and data right away.
Directory Structure
`-- cockpit/ // <-- your cockpit installation
|- test1.php
|- ...
`- // the other files of your project