Click to See Complete Forum and Search --> : Can I use CSV with PHP?


AmazingAnt
05-31-2006, 01:53 PM
ok, for whomever answers this, it probably seems like a stupid question, but can I? I'm having enough problems keeping the languages for html, javascript, C++, Basic,(yes i still use basic)PHP, English, and French all seperate in my mind. if i could just use PHP it would save me the time and headaches of trying to learn how to work MySQL or some BS like that.

bokeh
05-31-2006, 02:07 PM
Yes! PHP will have no problem working on this type of file as long as it is correctly formatted.

AmazingAnt
05-31-2006, 02:17 PM
ok, that sorta helps.
correctly formatted
would that include like if i save a spreadsheet from M$ Excel as a csv? and if so, would i need to use the MS-DOS format (unlikely), the Mac format (also unlikely), or the comma delimited format? and where might i find something like a tutorial for this? most of the php i'm learning that works is from htmlgoodies.com, and there really isn't a lot about using databases there. it's more like it mentions a few different things similar to MySQL i could use which doesn't help...

bokeh
05-31-2006, 02:28 PM
CSV === comma seperated values... Save a sample file and then open it with a text editor and make sure that's what it really is.

AmazingAnt
05-31-2006, 02:30 PM
yea, that's what it is. any of the cells that have a comma in them are enclosed in quotation marks when you save it as a csv. so if cell A1 had "tvs, computers, and Cds" written in it, then it would put
"tvs, computers, and Cds",
notice it adds a comma after the quotes to signal the end of the cell. would excel saving it like that cause any problems, or work fine?

the tree
05-31-2006, 02:30 PM
Here's an example I made once.http://google.com/,Google,Popular search engine
http://webdeveloper.com/,Web Developer,Awesome discussion forum<?php
$handle = fopen('links.csv','r');

while (($data = fgetcsv($handle, 50, ',')) !== FALSE){
echo '<dt><a href="'.$data[0].'">'.$data[1].'</a></dt><dd>'.$data[2].'</dd>';
}

fclose($handle);
?>The fgetcsv(); function is pretty simple and gives you an easy array to work with.

AmazingAnt
05-31-2006, 02:39 PM
ok, so that little bit of code works, but can you explain which parts do what and how to change each part? i mean, of course I can figure out the echo part, but how the rest works is greek to me. like for instance, how does it determine the difference between the first and second lines in the csv file?

bokeh
05-31-2006, 02:48 PM
fgetcsv() (http://www.php.net/fgetcsv)

AmazingAnt
05-31-2006, 02:49 PM
whoa! the php.net manual! why didn't i think of that? thanks!

LiLcRaZyFuZzY
05-31-2006, 03:14 PM
whoa! the php.net manual! why didn't i think of that? thanks!
Yeah, keep that in mind, it's very useful, just type http://php.net/<name_of_function>

AmazingAnt
05-31-2006, 03:18 PM
yea. i'll probably still end up coming here though, because the php manual isn't exactly easy to understand all of. yes, they manage to explain what each thing is, but if you goto htmlgoodies you can see that he does a much better job of explaining things... it's just too bad he didn't do any more tutorials on php....

AmazingAnt
06-02-2006, 10:41 PM
oh, while i've got a thread going that you're probably still subscribed to, i need a little more help with that csv thing.

If I have this php code:
PHP Code:
<?php
$handle = fopen('webpages.csv','r');

while (($data = fgetcsv($handle, 0, ',')) !== FALSE){
echo $data[0];
}

fclose($handle);
?>

What exactly is it that's controlling what line in the "webpages.csv" file the "$data[0]" is coming from?
better? plus, i asked what was controlling the line it's coming from because i can't read those hyroglyphics you called a php manual. official site or not. so wait, would i be right in saying that if the csv is like this:

something, somethingelse, lastsomething,
andthen, no and then, andthenagain,

that would make $data[4] the way to get "andthen"?

NogDog
06-02-2006, 10:51 PM
If there is a newline after "lastsomething,", then "andthen" will start a new line (or new record if want to think of it that way) and so will be element 0 (zero) for that line. The while loop in TheTree's code is looping through the CSV file one line at a time, as fgetcsv() reads the current line from the file then moves the line pointer to the next line.

AmazingAnt
06-03-2006, 06:46 AM
ok, so i could rewrite this to just have
$handle = fopen('webpages.csv','r');
$data = fgetcsv($handle, 0, ',')
echo $data[0];
fclose($handle);
and then when i wanted $data[0] to be coming from the second line, i would need to run the "$data = fgetcsv($handle, 0, ',')" again? doing this as opposed to having it use the "while" to loop, until it runs out of lines. yes?

bokeh
06-03-2006, 06:54 AM
The whole purpose of the while loop is so the process happens automatically.

bokeh
06-03-2006, 07:01 AM
Yes! PHP keeps track of its position in the file and each time you call fgetcsv this position will be advanced to correspond with the ammount of data requested.

AmazingAnt
06-03-2006, 02:32 PM
well, i played around with it and got this:

$handle = fopen('webpages.csv','r');
while ($data[0] != "3")
{$data = (fgetcsv($handle, 0, ','));}
echo $data[1];echo " ";echo $data[2];echo " ";echo $data[3];
fclose($handle);

and that seems to work for what i'm looking for. just so long as i make sure to use the first collum in excel to copy the row numbers into, i should beable to get to any row i want easilly enough. before i start using this piece of code though, do you see any problems i should fix? just wanna make sure. and one last question. to get everything to go back to the beginning of the csv file, i would need to use
"fclose($handle);"
and
"$handle = fopen('webpages.csv','r');"
again, right?

bokeh
06-03-2006, 03:13 PM
to get everything to go back to the beginning of the csv file, i would need to use
"fclose($handle);"
and
"$handle = fopen('webpages.csv','r');"
again, right?If your sole purpose is to return to the beginning of the file you would use rewind($handle)Also as a precautionary measure I would consider modifying your while loop and adding !feof($handle)just in case the other condition never fails.
$handle = fopen('webpages.csv','r');
while ($data[0] != "3")
{That's not very nice because $data hasn't even been set yet.

AmazingAnt
06-03-2006, 03:41 PM
If your sole purpose is to return to the beginning of the file you would use rewind($handle)Also as a precautionary measure I would consider modifying your while loop and adding !feof($handle)just in case the other condition never fails.
That's not very nice because $data hasn't even been set yet.
ok, so i would want to make sure that when feof is true, the while loop stops, so as to make sure it's not trying to go past the end of the document. Normally impossible, it would of course, probably throw errors everywhere just to make my life more fun. I'll be sure to remember the rewind function of course. and although true that $data hasn't been set yet, it seems to be work fine. I guess it's deciding that the fact that the variable doesn't exist yet, counts twards not being 3. so thanks again, and for now i think i'm done asking questions. (which of course means i'll be back here asking something within 24 hours....)

bokeh
06-03-2006, 03:47 PM
It's only working because you have error reporting set at such a low level. Forget feof() I was starting to get confused because your code is a bit messy. I would do it something like the following:<?php

$filename = './webpages.csv';
$stop_at_line = 3;

$handle = fopen($filename, 'r') or die("I could not open $filename");
while(($data = fgetcsv($handle)) !== false)
{
if($data[0] == $stop_at_line)
{
break;
}
}
fclose($handle);

echo $data[1] . " " . $data[2] . " " . $data[3];


?>

bokeh
06-03-2006, 04:00 PM
Or even better... if you want to count the line numbers rather than rely on the line numbers contained in the file itself:<?php

$filename = './webpages.csv';
$stop_at_line = 3;

$handle = fopen($filename, 'r') or die("I could not open $filename");
while(($stop_at_line) and (($data = fgetcsv($handle)) !== false))
{
$stop_at_line--;
}
fclose($handle);

echo $data[1] . " " . $data[2] . " " . $data[3];

?>

AmazingAnt
06-03-2006, 04:36 PM
It's only working because you have error reporting set at such a low level. Forget feof() I was starting to get confused because your code is a bit messy.
Oh, thanks... extreme boost for my confidence. oh, and the error reporting level is still at whatever the default is, so yea. messy or not, low error level or not, it's working and so yea... i'll see how well i can work with the code you have there.

AmazingAnt
06-03-2006, 04:48 PM
The "or die("I could not open $filename");" part didn't quite work. it gave me it's default error message, and yours. other than that, it works fine.

bokeh
06-03-2006, 06:10 PM
I tested that code and it ran fine.

AmazingAnt
06-03-2006, 11:16 PM
yea, maybe for you. but on my apache server with the fancy PHP 5.X, it gave me a default error along with "I could not open-". either way, i've got that last code you posted working fine. now, i just need to copy a rather large amount of data into the csv file. i've got about 150 different groups, each of which has anywhere between 5-20 subgroups, and each subgroup in itself has an average of 12 more sub-subgroups, each of which then have about 10 different pieces of data to enter.... so this may take a little while to finish up... but apart from the double error, that last code is working very well.

NogDog
06-04-2006, 02:14 AM
If you don't want the default error message, use the "@" to suppress error reporting:

$handle = @fopen($filename, 'r') or die("I could not open $filename");

(Just be careful, some people overdo this when initally creating code and then have a hell of a time finding where their errors are.)

AmazingAnt
06-04-2006, 05:05 AM
naw, i'm good. if i have code that works, i really don't need custom errors.

bokeh
06-04-2006, 05:36 AM
i really don't need custom errors.It's not a custom error. It is there to abort the script if for some reason the file were not available.

AmazingAnt
06-04-2006, 05:45 AM
yea yea, w/e. Just because PHP isn't client-side, doesn't mean i won't take the time to make sure there aren't errors. And, if i know it works anyway, then i really don't need it to stop the PHP when it can't find the file.

bokeh
06-04-2006, 06:48 AM
Ant! Adding things such as this is about having a little foresight. If you change hosts in 5 years time and your script no longer works it would be nice if the script immediately reported to you what caused the error.

Also having the script continue when important variables are inaccessible is not the most sensible thing.

AmazingAnt
06-04-2006, 07:50 AM
Ant! Adding things such as this is about having a little foresight. If you change hosts in 5 years time and your script no longer works it would be nice if the script immediately reported to you what caused the error.

Also having the script continue when important variables are inaccessible is not the most sensible thing.
I'm not going to change hosts in 5 years, because i can't get better bandwidth for the price of free without having all sorts of limits put on me. If for some reason, i do decide i don't want to keep running a server on my home computer, then i'll pay for a good service and i'll re-write everything. plus, where other than your own computer can you get an 80GB hard drive at your disposal? and besides, if my script stops working, so be it. this is only something i'm doing for fun.

bokeh
06-04-2006, 09:12 AM
Ant! When you are standing next to a red car but the person you are with keeps trying to convince you it is a green one it soon become obvious that further discussion would be fruitless.

AmazingAnt
06-04-2006, 10:06 AM
ok... i'm not quite sure what you said, but ok. your point is that if i just learn how to do this differently, it will save me time in the future. my point is that i'm not looking to make a web site for anyone and their brother to visit whenever. i'm just making a small site for the purpose of entertainment. if it isn't the newest, best way to write the code, so be it. all i'm looking for is for it to work.

bokeh
06-04-2006, 10:33 AM
Ant! Your argument is synonous with suggesting that car safety belts are unnecessary based on one particular journey without incident.

AmazingAnt
06-04-2006, 03:21 PM
Bok! i think you meant "synonymous"... yes? anyway. if your car has seat belts, you wear them, that is slightly different, because you have no clue when you're going to get into an accident, and if you do, you're definatly going to be upset if you didn't wear a seat belt (given that you live). but how often do you get into your car for the purpose of finding your wallet, (which is somewhere under your seat,) and decide to put on your seatbelt? because i hope you realize that another car might drive off the road and smash through your garage. while sitting in your garage, there is still a risk of being hit by another car, although it is indeed a very low risk. plus, even if a car were to come through your garage, it's highly unlikely it would do any harm to you unless it was moving rather fast. in having a small site for the purpose of entertainment, i may run the risk of having outdated code, but if the car that's about to hit you is a hotwheels toy, does it really matter? and how many problems am i going to have with this code that works fine in PHP 5.1.4, if i don't upgrade to whatever the next version is?

bokeh
06-04-2006, 03:49 PM
My next door neighbour popped round today and while she was here her 3 year old son told her "mum, have you seen the swimming pool? Look how clean the water is, and there are no bugs in it". She says they don't bother keeping their pool as clean as ours because, "after all its only for us, it's not as if it's public". Well I want the water in my pool to be perfect and it wouldn't matter how much the neighbour tried to convince me otherwise I would still want it to be perfect.

AmazingAnt
06-04-2006, 03:52 PM
then enjoy your clean ool. and try to remember that there's no "p" in the "ool". (or candy bars that look like, well... yea....)