How to read/import datas from a CSV file and use them on a template with placeholders?


I have a new use case.
I have a csv file cronly generated by another database server. The file is available with a URL

I need to build a simple html table with these CSV datas.
I think the best way is to use a template chunk with placeholder, with something like that :


But I don’t know how to make a snippet for doing this kind of stuff.
After research, I have found an old MODX addon but it can read CSV from an external file, just get some CSV fields as argument.

Do you know how to adapt this addon or if there exist another one to doing this ?

Thanks a lot :slight_smile:

There are different ways to load a file from another server in PHP. The simplest one is probably to use the file_get_contents function.

To parse the CSV file, it may be enough to call explode once for the whole file and then for each line. Like:

$lines = explode("\n", $file);
foreach ($lines as $line) {
	$values = explode(';', $line);

You can use the modX.getChunk function to parse a chunk and replace the placeholders in the chunk with the supplied values.

Also, you may want to use the get() and set() methods of the MODX cache manager to cache the result for some time, so that you don’t have to make the additional request to the second server for every request on your server.

Some useful information here.

Re: Step By Step - How to import Excel data and how to show them on front end page? | MODX Community Forums

1 Like

Thank you all for your answers !
@bruno17 : the script from your link works out of the box \o/

This topic was automatically closed 2 days after discussion ended and a solution was marked. New replies are no longer allowed. You can open a new topic by clicking the link icon below the original post or solution and selecting “+ New Topic”.