hi mark!

i import data all the dang time and this is what i use (more or less)

<?

$file = file("sompath/somefile.csv");


for($a=1;$a<count($file);$a++)
{
        $d = csv_parse($file[$a],",");
                $first_name = make_safe($d[0]);
                $last_name = make_safe($d[1]);
                $company_name = make_safe($d[2]);
                $address_1_1 = make_safe($d[3]);
                $address_1_2 = make_safe($d[4]);
                $city = make_safe($d[5]);
                $state = make_safe($d[6]);
                $zipcode = make_safe($d[7]);
                $country = make_safe($d[8]);
                $start_date = make_safe($d[9]);

        $query = "some insert sql";
        $db->Insert($query); // i use a class for my mysql connections
}


function csv_parse($data, $separator)
{
        $quote = '"';
        $values = array();
        $toggle = 0;
        $len = strlen($data);
        $count = 1;
        for ($i = 0; $i < $len; $i++)
        {
                $tmp = substr ($data, $i, 1);
                if (strcmp($tmp, $quote) == 0)
                {
                        $toggle = $toggle ^ 1;
                }
                $value = $value . $tmp;
                if (strcmp($tmp, $separator) == 0)
                {
                        if (! $toggle)
                        {
                                # End of word
                                $value = ereg_replace(",$", "", $value);
                                $value = ereg_replace("^\"", "",
$value);
                                $value = ereg_replace("\"$", "",
$value);
                                $value = ereg_replace("\"+", "\"",
$value);
                                $num_of_elems = count($values);
                                $values[$num_of_elems] = $value;
                                $value = "";
                        }
                }
        }
        $value = ereg_replace("^\"", "", $value);
        $value = ereg_replace("\"$", "", $value);
        $value = ereg_replace("\"+", "\"", $value);
        $num_of_elems = count($values);
        $values[$num_of_elems] = $value;
        return ($values);
}

function make_safe($text)
{
        $text = preg_replace("/(\cM)/", " ", $text);
        $text = preg_replace("/(\c])/", " ", $text);
        $text = str_replace("\r\n", " ", $text);
        $text = str_replace("\x0B", " ", $text);
        $text = str_replace('"', " ", $text);
        $text = explode("\n", $text);
        $text = implode(" ", $text);
        $text = addslashes(trim($text));
        return($text);
}



On Sun, 2003-03-09 at 18:02, Mark Tehara wrote:
> HI, I'm looking to take the data from a CSV file, then upload it into a
> mysql databace.
> 
> I figure i will need the following.
> 
> To count the number of lines ... and cut up each line in its veriables then
> entering it into the databace.
> 
> I have:
> 
> $file = fopen("pricelist-snippet.csv","r");
> $buffer = fread($file,10000);
> echo $buffer;
> 
> This dumps the data to the screen ...
> 
> where would i start to making cutting this data up by the line?
> 
> / Mark
> 
> 


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

Reply via email to