[PHP] fopen and file dump to a databace...

2003-03-09 Thread Mark Tehara
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,1);
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



Re: [PHP] fopen and file dump to a databace...

2003-03-09 Thread Jimmy Brake
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;$acount($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,1);
 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



Re: [PHP] fopen and file dump to a databace...

2003-03-09 Thread Bob Irwin
I would use something like this (assuming you're just going to read the
entire file and bung it into the database).

Be careful reading large files in, it can fill up PHP's allocated memory
resource.  I've used this with 4 meg files though, and its worked ok.


$fp = file(yourfile.txt);
//This reads the file into an array, each element of the array is a line.
Which you can then, test,  manipulate, explode etc to put into the database.

while (list($barry, $tbone) = each($fp))
{
//barry is the index of the array element, $tbone is the value of
that element in the array.

echoIndex: $barry Value: $tbone BR;

   //just insert into the database what you need to.

}


Best Regards
Bob Irwin
Server Admin  Web Programmer
Planet Netcom
- Original Message -
From: Mark Tehara [EMAIL PROTECTED]
To: [EMAIL PROTECTED]
Sent: Monday, March 10, 2003 1:02 PM
Subject: [PHP] fopen and file dump to a databace...


 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,1);
 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


 Scanned by PeNiCillin http://safe-t-net.pnc.com.au/



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