How do I save a .csv file to my server?

0 favourites
  • 13 posts
From the Asset Store
Very simple code without excess options (15 events for server and 11 events for client)
  • Hello,

    I am working on a little "game" that I'll use for my research on Japanese language. This "game" takes the form of a survey in which Japanese speaker will create abbreviations of pairs of loanwords.

    I made a draft of the game that works pretty well (even though it may not be very elegant. I'm still a beginner at programming).

    The thing is, I'm going to upload it to a server (from the lab where I work) and of course I want to collect the answers of the participants on the server. Basically, all the data I need is in the .csv file the browser download at the end of the game. So I need to get that .csv file to be saved on the server instead of the user's computer. I thought the AJAX object might help, but it seems more complicated than I expected. So I'm asking for your help on that.

    Here is the .capx. Note that I used the BHT smart random plugin :

    drive.google.com/open

    Another, less important question :

    The abbreviations I am studying are made of two words (or "constituants", hereafter C1 and C2). In the draft, I only included 8 C1s and 2 sets of 8 C2s. Thus, you can see 24 text instances with different instance variables on the main layout, but outside the window. In the final version, I will have 120 C1s and 2 sets of 120C2s. All the informations about those C1s/C2s are all ready in a Excel file. So I would like to know if there is any way not to create manually each instance of text, or even not using text instances at all. Basically my question is "do you know an elegant way to put all informations on my C1s/C2s in the game" ?

    Anyway, it's not gonna take too much time to do it manually, so my question is more about learning new stuff than anything.

  • Answering your second question - you can import the CSV file with all C1s/C2s into the project and then use this plugin to read it:

    construct.net/forum/extending-construct-2/addons-29/plugin-csv-csv2array-csv2dicti-41868

    You can load data from CSV into an array or dictionary, or create text instances, or just request individual values as you need them.

    Make sure to save the csv file in Unicode.

  • Oh ! That sounds neat !

    The explanations were a bit complex for me, and in the end I'll end up with a lot of text instances (be it generated by the plug-in or manually), so I decided to do it manually this time. But I'm glad to know this plug-in exists.

  • Sorry for the double post. I have made some good progress on my project, but I'm still stuck on getting data from the game to be saved... anyhow.

    So my experiment is fully fonctional now, but I still don't know how to get the answers back from my participants.

    I have considered having them sent by e-mail. So I tried to follow this tutorial : scirra.com/tutorials/4897/sending-an-email-from-construct2

    It's even a simpler version of it since the e-mail adress to send to, and the subject would be the same everytime. But it doesn't work for me.

    I must admit I'm copying things blindly without understanding what I'm doing. So maybe I did something wrong.

    In this .capx I tried to send a test message "hello world" to my professional e-mail address (I changed it for a fake example before making the .capx file). But it seems nothing is sent.

    drive.google.com/open

    My experiment is hosted here : lpl-aix.fr/~lamarque

    And the .php file I uploaded is this one : drive.google.com/open

    Does anybody know what I'm doing wrong ?

    By the way, I don't actually care in what way I get the data back. If anybody has a better solution to get (text) informations back from a player, I will gladly try it.

    Thank in advance

  • You should try to remove addons from your capx before posting it here, many people don't like installing new addons..

    How much information your game needs to send? Is it in English or Japanese?

    If it's just something like player name and score, there is an option to send this data to Google Spreadhseet. I'm currently writing a tutorial on this.

  • Oh, sorry about the add-on. Here goes the .capx without the smart random:

    drive.google.com/open

    The data I need all fits in a dictionary (with about 200 keys/values). If I can get the informations of that dictionary in any way. That would be great.

    (regarding my initial problem, the tech guy from my lab told me the problem was caused by the "~" in the URL I'm hosting the game to. It's a rewriting of the actual URL (which is not usable either). He thinks he can solve that.)

  • Zakeru

    I finished my tutorial!

    scirra.com/tutorials/9704/sending-data-from-construct-23-to-google-spreadsheet

    I think you should be able to send the entire dictionary as JSON to the spreadsheet. Make sure to use URLEncode(Dict1.AsJSON)

    There may be a size limit though, when I tried to send a dictionary with 200 keys, I got an error. 20 keys work fine..

  • Hello,

    Thank you very much for your help. In the end, I managed to solve my problem using the e-mail sending tutorial above.

    Now I get all my data perfectly fine, sent directly in an e-mail :D

  • Hi

    If you want to save the data as a file to your server, here is the php script.

    <?php

    $myfile = fopen("text.txt", "w") or die("Unable to open file!");

    $text = $_POST['my_data'];

    fwrite($myfile, $text);

    fclose($myfile);

    ?>

    You have to create a file first on your server, I have used "text.txt"

    Then you send your text from Construct as a variable, I have used "my_data", but it could be anything.

    From Construct your Ajax Post would look like this.

    This refers to your php file, which in this case I have called it texttest.php, which you will have to put on your server in the same directory.

    Hope this helps.

  • Yes! Thanks! I wanted to do this kind of things at first.

    But if I have a single .txt file, won't every player erase the data from the previous player? Is there a way to prevent this ? Have several files ? Have each player's data be written after the previous player's data in the same file?

    Anyway. I'm satisfied with my e-mail solution. I'm just asking out of curiosity. So if you don't already know the solution, don't think about it too much :)

  • Yes! Thanks! I wanted to do this kind of things at first.

    But if I have a single .txt file, won't every player erase the data from the previous player? Is there a way to prevent this ? Have several files ? Have each player's data be written after the previous player's data in the same file?

    Anyway. I'm satisfied with my e-mail solution. I'm just asking out of curiosity. So if you don't already know the solution, don't think about it too much :)

    You could have a new file for each user file.

    Just post the data of the required file name.

    Example

    <?php

    $myfile = fopen("$user.txt", "w") or die("Unable to open file!");

    $text = $_POST['my_data'];

    $user = $_POST['my_user'];

    fwrite($myfile, $text);

    fclose($myfile);

    ?>

    This will create a .txt file if it doesn't exist, and will be called whatever the user inputs as it's name.

    $myfile = fopen("$user.txt", "w") or die("Unable to open file!");

    $user = $_POST['my_user'];

    I haven't tried it, you may have to use.

    $myfile = fopen($user.".txt", "w") or die("Unable to open file!");

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • So with your second method, I do not need to create an empty text file on my server right?

    For some reason I can't make it work. I think it's because php files don't have the authorization to write on my server or something like that.

    I will check that with the I.T guy of my lab and try again ^^

    Thanks anyway !

    Just out of curiosity. What part of the php code deals with the creation of a new file if there is no matching file name on the server ? Is it just how "fopen" works ? Then why do you need to create in advance a text.txt file with the first method ?

  • So with your second method, I do not need to create an empty text file on my server right?

    For some reason I can't make it work. I think it's because php files don't have the authorization to write on my server or something like that.

    I will check that with the I.T guy of my lab and try again ^^

    Thanks anyway !

    Just out of curiosity. What part of the php code deals with the creation of a new file if there is no matching file name on the server ? Is it just how "fopen" works ? Then why do you need to create in advance a text.txt file with the first method ?

    Its the "w" parameter

    $myfile = fopen("text.txt", "w") or die("Unable to open file!");

    "r" read: Open file for input operations. The file must exist.

    "w" write: Create an empty file for output operations. If a file with the same name already exists, its contents are discarded and the file is treated as a new empty file.

    "a" append: Open file for output at the end of a file. Output operations always write data at the end of the file, expanding it. Repositioning operations (fseek, fsetpos, rewind) are ignored. The file is created if it does not exist.

    "r+" read/update: Open a file for update (both for input and output). The file must exist.

    "w+" write/update: Create an empty file and open it for update (both for input and output). If a file with the same name already exists its contents are discarded and the file is treated as a new empty file.

    "a+" append/update: Open a file for update (both for input and output) with all output operations writing data at the end of the file. Repositioning operations (fseek, fsetpos, rewind) affects the next input operations, but output operations move the position back to the end of file. The file is created if it does not exist.

    So should write a new file to the server, if it does not exist.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)