pulling data from .csv file

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Noobie
Forum Commoner
Posts: 85
Joined: Sun May 15, 2005 11:38 am

pulling data from .csv file

Post by Noobie »

Hi everyone.

I'm wondering if some kind person could point me in the right direction?

I've got a client who has an excel database with amongst other info, column titles for customer name and a password.

I need to be able to do a header redirect upon the client's customer entering their password into a form on the client's website. The website then emails my client to tell them that a certain user has gone to that link. Does that make sense?

E.g. end user comes to password request box. Types in unique password and assuming password is correct they are redirected to a certain page (off-site actually).

Now I'm thinking that my client needs to save their excel spreadsheet as a .csv to be remotely useful - but here's where I'm not sure of the best way to do things.

Would it be best if:

1. Just give them an FTP programme and tell them to upload the .csv file to a certain folder and give it a certain name so that it overwrites the older version and, when the end-user enters a pw on the site, use PHP to open the file, read it, check if the PW exists, if it does - re-direct them and finally email my client to say it's done it.

2. Have the client upload the .csv file to the website via an admin panel, which (and I'm a bit unsure of getting the info from the .csv to the update query) will then take the information and store it on a MySQL DB. Then when the end user gets the password form the site will just query the DB and if the password exists, re-direct them accordingly.

I guess in either case I'll probably be using a similar PHP action (fgetcsv ?) to open and read the .csv file but I'm a little unsure of which is the best approach and what's the best flow for the process.

Thanks!
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: pulling data from .csv file

Post by requinix »

I'd say a little of both. Make them upload a CSV through some web interface (because you can't really trust them to upload the file to the right directory) and use PHP to read through the file when checking the password. That way they can upload a new file and you don't have to worry about deleting the previous data and whatnot.
This process doesn't sound like it will be happening very frequently so I think reading through a file and not a database will be okay.

Yes, you'd be using fgetcsv. Look at the example for how to use it.
Noobie
Forum Commoner
Posts: 85
Joined: Sun May 15, 2005 11:38 am

Re: pulling data from .csv file

Post by Noobie »

Thanks tasairis

According to the client the .csv is likely to have around 1500 rows (they haven't been too clear on whether that's in total or on each upload though...) and is likely to be uploaded each week.

Just reading your comment... if I understand correctly I could either use a DB or just do it with PHP and the advantage of just using PHP is speed assuming that there aren't too many rows to go through?

I hadn't considered the speed and the implication of them regularly uploading a .csv with 1500 rows hadn't (for some reason) surfaced in my little brain until I read your answer...

At what point would you say that it's easier to bring in the DB element rather than just rely on PHP? Is there a number of rows past which you'd think twice about just using PHP or am I on the wrong track.

Thanks again!
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: pulling data from .csv file

Post by requinix »

For the most part a database is faster than a flat file (ie, just a CSV file). Especially regarding lookups.
For small data sets a file is probably faster - but we're talking very small here, like a dozen or so records? Then there are exceptions, like if the database is on a remote host...

The best and most professional way would be to use a database. It's faster, cleaner, and easier to manage. If you want to do this seriously then this is the way to go.

However if this login process won't be happening often (there's no quantitative meaning of "often") then the extra overhead of your coding and the future maintenance starts to offset the gain a database provides. It's easy to update a flat-file database: just upload a new file. There's no worrying about deleting old data or the best way to backup previous versions.

However if the file starts getting long (again, no quantitative meaning to "long") then you'll notice a lag when trying to scan the entire file looking for a match. Best case is near-instantaneous (first record) and worst case is scanning the entire file and not finding a match. Average isn't strictly "half way through" but depends on things like typos and form abuse.
Noobie
Forum Commoner
Posts: 85
Joined: Sun May 15, 2005 11:38 am

Re: pulling data from .csv file

Post by Noobie »

Thanks again for your informative answer. I think I'll go with the DB solution - if for no other reason than my own peace of mind!

I'm not very good at PHP and I'm sort of used to basically using it to put info in and then retrieve information from a DB, it's sort of comforting! Might make figuring out the new bits (reading and extracting info from the .csv file) easier to manage for me in some bizarre way!
Post Reply