Retreive data from stored files

I’ve created a simple form that saves data to the website.

Data folder

These user submitted files contain some simple text information, I’ve used .yaml but another file format might be more relevant here:

User Submitted Data

The username shows the user that submitted the post; the text file is what they submitted in the form.

====

What I want to do is a bit of an adventure, but here goes…

I’d like to look through these folders & files to find all the posts made by the current user. Once found, I want to output the text field into a html webpage, I think using a for each loop of some sort. The result should be a webpage that acts as a dashboard for the users submissions. With the following structure:

Desired HTML output

I’d appreciate any advice, from code snippets, to better ways to go about achieving it. It’s pretty complicated, but if I can figure it out I’d like to later provide some video tutorials about how the process works to help other Grav users.

Currently the username can’t be added to the filename, which might help, but I’ve submitted it as a request to the form developers.

To cut processing time, it might also be better if the posts could first be grouped into user folders. That way rather than scanning 50 files for the username, it could just find all the files in one folder with the username. Loose ideas, but would appreciate any feedback and thought.

Still learning, but I’ve thought and looked more into this. Let me know if I’m on the right track. I think I’m going to try making a form template that constructs .json variables. Then parse it to the page with php’s json decoding, using the example plugin the grav docs provide, to place custom php on the page.

Video Example of Parseing JSON With PHP:

That relies on the filename though, which I don’t think I can do currently? And I’m a bit unsure how to access file contents in the data/myform folder.

Basic example:

  //decode json file
  $jsondata = file_get_contents("movies.json");
  $json = json_decode($jsondata,true);

//this uses a for each loop to output every movie as a list
  $output = "<ul>";
  foreach($json['movies'] as $movie){
    $output .= "<h4>".$movie['title']."</h4>";
    $output .= "<li>".$movie['year']."</li>";
    $output .= "<li>".$movie['genre']."</li>";
    $output .= "<li>".$movie['director']."</li>";
  }
  $output .="</ul>";  
  echo $output;

Theoretically though, if the username was eventually added to the form save: prefix:, I think I could get the form to make json files with username titles and call them with the file_get_conents using the current user variable.