Forum Moderators: coopster
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:()
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";
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);
?>
(1)
awk -F[,] '($2 ~ 25){print}' yourdata.csv
(2)
awk -F[,] '($2 ~ 25 && $3 ~ "Scotland"){print}' yourdata.csv
;)
There are examples on this page [uk.php.net] of filtering multi-dimension arrays
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?
<?
$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]
>>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 ;)
<?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.
<?
$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?
<?
$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>');
?>
The ball is rolling now - I was just getting no returns at all! - I now see data :)
Thanks Loads.