| Help with CSV files - Still Learning Any help/pointers would be appreciated. |
Dayo_UK

msg:1309420 | 8:25 am on Jan 14, 2004 (gmt 0) | Done a search on the Net for help with CSV and ended back up here :) I need a bit of help with Reading from a CSV file which is in the following format:- Stephen,14,Wales,S4545 John,18,England,S6406 Becca,25,Scotland,S4564 Rob,25,Northern Ireland,S4508 Ian,25,Scotland,S4839 I know the basic and I can open the file store it in arrays and print the output. But how would I query the file so that for example I wanted just the results returned for those matching 25 in column 2? and then if necessary running another query and having returned those that are 25 in column 2 and Scotland in column 3? Any help would be appreciated (still learning PHP and so much is not covered in the books:()
|
jatar_k

msg:1309421 | 8:31 am on Jan 14, 2004 (gmt 0) | you would read as you already know how but you would only put the ones with 25 in col2 into the array. You could then re walk the array to see which of those have scotland in col 3. flatfile really isn't much fun. <? $sfp = fopen('/path/to/source.csv','r'); $counter = 0; while ($row = fgetcsv($sfp,10000,",","")) { if ($row[1] == 25) { $myarr[$counter] = $row; $counter++; } } fclose($sfp); ?> then go through the array again after to see if $myarr[$counter][2] == "Scotland";
|
Dayo_UK

msg:1309422 | 9:02 am on Jan 14, 2004 (gmt 0) | Thanks for your help. So to re-walk the array would it be along these lines:- (Sorry to be a pain) <? $sfp = fopen('/path/to/source.csv','r'); $counter = 0; while ($row = fgetcsv($sfp,10000,",","")) { if ($row[1] == 25) { $myarr[$counter] = $row; if ($myarr[$counter][2] == Scotland) { $myarr[$counter] = $myarr[$counter]; $counter++; } } fclose($sfp); ?>
|
dcrombie

msg:1309423 | 10:20 am on Jan 14, 2004 (gmt 0) | Or you can just use the command-line: (1) awk -F[,] '($2 ~ 25){print}' yourdata.csv (2) awk -F[,] '($2 ~ 25 && $3 ~ "Scotland"){print}' yourdata.csv ;)
|
rpking

msg:1309424 | 11:51 am on Jan 14, 2004 (gmt 0) | Or try the built in function array_filter(). There are examples on this page [uk.php.net] of filtering multi-dimension arrays
|
Dayo_UK

msg:1309425 | 12:00 pm on Jan 14, 2004 (gmt 0) | Thanks dcrombie and rpking. I think I am going to go with Jatar_K suggestion I understand that one ;) and it looks like what I need. (I cant test until this evening though!) Does anyone know if the way I adjusted Jatar_K looks correct?
|
jatar_k

msg:1309426 | 4:13 pm on Jan 14, 2004 (gmt 0) | | I think I was being a little thick last night actually. You could just test both at once. <? $sfp = fopen('/path/to/source.csv','r'); $counter = 0; while ($row = fgetcsv($sfp,10000,",","")) { if ($row[1] == 25 && $row[2]) == "Scotland") $myarr[$counter] = $row; } fclose($sfp); ?> You may have to play around with the case for the string depending on whether you want it to match just the string or be case sensitive. Good thing I went to get some sleep before answering again ;) [1][[b]edited by[/b]: jatar_k at 7:59 pm (utc) on Jan. 14, 2004][/1]
|
coopster

msg:1309427 | 4:42 pm on Jan 14, 2004 (gmt 0) | LOL! >>You could just test both at once. I wondered why you didn't go that route in the first place -- now I know: >>I think I was being a little thick last night actually...<snip>...Good thing I went to get some sleep... Lack of sleep will do that, my friend. We've all been there and mistakes start happening. Sleep is sometimes the best resolution! And, to quote a well-known sage in these parts, jatar_k, "don't be so hard on yourself"! Nice recovery, by the way ;)
|
Dayo_UK

msg:1309428 | 7:51 pm on Jan 14, 2004 (gmt 0) | I am sorry - I just can not get my head around this! - this is how I am extracting data from the file:- <?php if($fp = @fopen("path/to/file", "rb")){ $data = fread($fp, filesize($fp)); while(!feof($fp)) { $data .= fgets($fp, 1024); } fclose($fp); $values = explode(",", $data); } else{ echo ""; } ?> So I am left with $value array with the data - but I just can get it to only query the required row. I have treid querying the file as per above but it just dont work for me :( - Sorry I did say I was a beginner.
|
jatar_k

msg:1309429 | 8:03 pm on Jan 14, 2004 (gmt 0) | how about trying just this to start <? $sfp = fopen('/path/to/source.csv','r'); $counter = 0; while ($row = fgetcsv($sfp,10000,",","")) { if ($row[1] == 25) { $myarr[$counter] = $row; $counter++; } } fclose($sfp); echo "<pre>"; print_r($myarr); echo "</pre>"; ?> just change the path to the real file path. also are the fields delimited and are they seperated by commas?
|
coopster

msg:1309430 | 8:15 pm on Jan 14, 2004 (gmt 0) | Your original example will work, jatar_k, but with a few modifications. I had to remove the optional string enclosure parameter in the fgetcsv function as it threw an error since it is limited to one character. Also, we need to post-increment the $counter variable:
<? $myarr = array(); $counter = 0; $sfp = fopen('/path/to/source.csv','r'); while ($row = fgetcsv($sfp,10000,",")) { if ($row[1] == 25 && $row[2] == "Scotland") $myarr[$counter++] = $row; } fclose($sfp); print '<pre>'; print_r($myarr); exit('</pre>'); ?>
|
Dayo_UK

msg:1309431 | 8:21 pm on Jan 14, 2004 (gmt 0) | Excellent Both of You. The ball is rolling now - I was just getting no returns at all! - I now see data :) Thanks Loads.
|
jatar_k

msg:1309432 | 8:38 pm on Jan 14, 2004 (gmt 0) | good thing you're on this thread coop, I seem to be having endless idiot moments here. Maybe that bit of sleep didn't help as much as I thought. ;)
|
|
|