24/7 Support: 800.608.6482


Developer Videos

Videos | Reading a CSV file

ver. 9.0 and later

Video Transcript

In the last video I introduced to how to work with Miva Merchant's file system from the template language. We were able to check and see if the file existed, delete it if it did and create a new file with some sample data. In this video I want to take that one step further. Here, we're going to read in a csv file, which has some sample data. We're going to iterate through the data and display a certain row. So I created a simple CSV that has three columns; login, customer name and credit and three sample entries. I'm going to take this file and ftp it to the server. Here's my credit.csv and I'm logged in via ftp in the web root, which is httpdocs. All right so now my file is on the server, credit.csv. Let's take a look at some code. So back here in my text editor I have this code written out and we will walk through it line by line like we did in the last video. None of these functions should be new to you. Instead, we're combining a bunch of different tags together to be able to do what we need. So the first one is this file read function. This was the same function we used in the last video. Here we're passing in the full path to the file to whatever directory we are using. the script or data. For this purpose we're using the script because it's in the webfoot. The third parameter is g.data. You can name this whatever you want. What this function does, it's going to take the entire contents of the file, read it into a string, and save it back into g.data. So now g.data contains everything that was in our file. Now that we have that, we need a way to iterate through the file to be able to read line by line to be able to output the data we need. So to do that I'm going to use a while loop and a counter. So here I'm creating a variable called g.counter and giving it a value of one. Now, in Miva Merchant everything is a one index instead of zero index, so all loops will always start at one. Now, the first thing we want to do with this data is split it by the line break. Now, if we go back and look here, there's a header row and then a line break, there's a pin entry and then a line break, another entry and a line break and then a third entry and a line break. So when this file gets read in using that file read function, not only does it read the contents of the file, but it also reads the characters you can't see, like the line break. So, we can test for that in our code. So this asciichar(10) function here, we used this in our last video to define a line break. Here we're using it to say split the data by the line break and in order to do that we're using a gettoken function that we looked at earlier.

Gettoken will split some data by any token or delimiter you want. So we're passing data and we're saying split it by the line break and then counter which is one. This is the first token I want to grab because we have to find one here. All that is going to be saved back into l.settings:line. All this does is give us the first line of the file. Now, in order to iterate through all three lines or every line in the file, we're going to have a while loop. So we're testing while not isnull l.settings line. l.settings line right now contains the first line in the file. So now in the while loop we know we're operating in the first line of the file. So we can test for a certain condition to see if this is the right line we want to output. So if bheyde is CIN l.settings:line, and this CIN is a case sensitive comparison. So if that is true, we're going to set three variables. We have l.settings:credit:login, :name and :amount. Here you see I used this credit prefix. What I'm actually doing is creating a structure. So I have a structure called l.settings:credit and it has three variables; login, name and amount. Now, in order to get the login, name and amount I have to use gettoken again to split it by the comma because I'm here I'm operating on a single line. That's the variable I have. Within that line I know I have a comma separated list, which has three values. I do the same thing we did above. We use gettoken, pass in l.settings:line, split it by the comma which is asciichard(44), that's the comma, and then I have one, two and three which define the columns that I want to save. So this gettoken function will save the value back into login, name and amount. Once I have that it's super easy to output them to the page. &mvt:credit:login that will print the login and the same thing for name and amount.

Now it's important to note here since these are not global variables, I had to make sure they were l.settings in order to be able to output them to the page. If this was just l.credit:login it wouldn't be available here to be able to output on the page.

So the last thing we want to do within our while loop is increment the counter. So here I'm resetting g.counter to g.counter +1 and then I want to reset the line. This is the same function we use up top here, but instead the only difference is g.counter got incremented so instead of up here where g.counter was one, down here because it got incremented now it's two. Every iteration of the while loop will be two, three, four and so on for however much data we have. This while loop will keep iterating over that data until l.settings line returns null. So let's grab this code and put it over into Miva and make sure it works. So here I am back at the Miva Merchant Admin and I'm going to go back to our storefront page again and go into the content section. I'm going to paste in this code. Now let's go back into our front end and hit refresh, and it gives us exactly what we wanted. So that read in the file, it iterated through all the entries in the file, or all the lines in the file, and since we can explicitly test to see if the line contained behead we were able to grab that line, save it to three variables and output it to the page. Let's jump back to the code here and let's say this wasn't working, where it wasn't reading the file correctly. Maybe because we had the path incorrectly or permissions not set correctly or maybe this was data instead of scripts. One thing that we can do to verify that the file was actually read is add some error validation here. So we know from looking at Mivascript.com and looking at the docs file of this function is that this file takes in three parameters and it outputs the length of what was read in or negative one on error. So we can test for that to verify that the file was actually read incorrectly. So let's add some additional logic to our code to validate that the file was read correctly. So here we want to say, g.file_read. So that's either going to contain a -1 or the length of the content read, so if g.file_read NE -1 then we want it to say "The file was read!" So if the file was equal to -1, that means there was an error and we can output a message to the page. So, let's output this and what we should see is "The file was read!" Perfect, so we have "The file was read!" We need a line break here and then we have the login, name and amount. Now let's make an error on purpose to validate that the file read does actually return a -1. So let's change this to the data which would be the Miva data file which is not the correct folder that we're in. We'll go back to the front end and refresh and it gives us an error message we'd expect. There's not data that's output because it never read the file. So this is a great way to validate that things are actually working as you intended or to trouble shoot if something isn't working. If you're just getting a blank page with no output, look at the return value for the functions and output them to the page in an appropriate manner.

Example Code

Looking for Developer Docs?

We have a whole section for that, including: Developer Training Series, Template Language docs, Module Development tutorials and much, much more.

Head to the Developer Section

This website uses cookies to identify visitors, track visitors to our website, store login session information and to remember your user preferences. By continuing to use this site you agree to our use of cookies. Learn More.

This website uses cookies. By continuing to use this site you agree to our use of cookies. Learn More.


Copyright © 1997 – 2024 Miva®, Miva Merchant®, MivaPay®, MivaCon®, Camp Miva®, Miva Connect®, Miva, Inc. All Rights Reserved.