Buy Now
Sign in

How to build a file name with variables?

joachimliedtke , November 30 2017
J
joachimliedtke
I want to give a file a unique name, that is easy identifiable, e.g. filename-{aid}.csv, where {aid} is the id when content is saved to the database. The read_dataXX function is working properly and all the values go to the file, so "aid" is known, but what do I have to do to make it part of the file name?

filename-{var:read_data4.Data4.aid}.csv


This returns only filename-.csv, but

filename-{var:read_data4.[n].Data4.aid}.csv


returns nutzertermin-["21"].csv. 21 is the correct aid, but I don't want the brackets and the quotes in the name. Any ideas?

TIA
admin
Hi,

Please set the your "read data" "select type" to "First matching" in order to get a single record then use:
{var:read_data4.Data4.aid}


Best regards
Max
If your main question got answered then please mark the answer using the button!​
​Please let us know if you have any problems with the new forums text editor, we appreciate your feedback!
J
joachimliedtke
Thanks for your advice. The filename is now as I wanted, but the CSV is now completely empty. I tried several settings myself, but no solution. The debugger shows, that the data is there in an array, but the data is not written to the file.

These were my prior settings for reading data:
Order fields = aid/desc
Select type = All matching records instead of First matching record
Paging for multiple results = Disabled
Page limit = 1
Offset = 0
Fields to retrieve = All needed fields were entered
Settings for writing CSV:
Data provider = {var:read_data4.[n].Data4}
Disabled header titles
Filename = nutzertermin-{var:read_data4.Data4.aid}.csv <- As told

So I have the choice, filled CSV and not the filename I want or the filename I want, but empty CSV.

Perhaps I don't get the concept behind reading data and writing the file. So I need a little more assistance on this matter.

TIA
J
joachimliedtke
I found a solution, well more a dirty hack. Open / libraries / cegcore2 / admin / extensions / chronofc / functions / csv / csv_output.php and insert after line 56

$file_path = str_replace('[', '', $file_path);
$file_path = str_replace(']', '', $file_path);
$file_path = str_replace('"', '', $file_path);


So the special chars are removed from nutzertermin-{var:read_data4.[n].Data4.aid}.csv

Don't expect elegant code from me.
admin
Hi joachim,

Thank you for sharing the code

it depends on the syntax used, the file name shortcode should return a string, but the provider should return an array, otherwise there will be problems!

You can test nutzertermin-{var:read_data4.Data4.aid}.csv in a custom code action to check if it returns the correct filename value.

Best regards
Max
If your main question got answered then please mark the answer using the button!​
​Please let us know if you have any problems with the new forums text editor, we appreciate your feedback!