Re: fgetcsv

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Jan 9, 2008 5:35 PM, Danny Brow <maillist@xxxxxxxxxxxxxxx> wrote:
> Hi Everyone,
>
> I'm trying to compare a value to the first field in a csv fILE (example
> of the data below). Using while takes too long and I can't figure out
> how to compare just one row at a time. I've tried some variations of the
> following.
>
>
> //Common for all trials
> $demoID = fopen("newDemoID.csv", "r");;
> $ID = "43";
>
> $data = fgetcsv($demoID);
>
>
>
> First try with while:
>
> /*
> while ($data) {
>         if ($data[0] == $wolfID) {
>                 print $data[1] . "," . $data[2] . "," . $data[3] .
> ".\n";
>         }
> }
> */

    As an alternative, you could try this:
<?
$wolfID = "43";

$data = file_get_contents($filename);
$line = explode("\n",$data);

for($i=0;$i<count($line);$i++) {
    $field = explode(",",$line[$i]); // Replace the comma with your separator.
    $match = str_replace("'",'',str_replace('"','',$field[0])); //
Strip quotes, if they exist.
    if($match == $wolfID) {
        echo $line[$i]."\n";
    }
}
?>


-- 
</Dan>

Daniel P. Brown
Senior Unix Geek and #1 Rated "Year's Coolest Guy" By Self Since 1979.

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux