I'm spinning my wheels here... I'm having a problem with the code below.
I'm successfully breaking out the comma delimited fields into their own arrays for database inserts (the data is not exactly the same as the table structure). That all works fine and dandy, my problem is I wan't to do a lookup of the cost centers in the database and associate that unique ID with the cost center I am getting in my file.
Here is the first four lines of my csv file:
"YEAR","PAY PERIOD #","COST CENTER NUMBER","COST CENTER NAME","HOURS","UNITS",
"2004","11","6010","6010 ICU-CCU","716","30",
"2004","11","6170","6170 MEDICAL/SURGICAL","2053","248",
"2004","11","6380","6380 OB/GYN","700.75","54",
The third field is my cost center numbers which I would like to see like this:
2004,11,1,
2004,11,2,
2004,11,5,
etc...
Here is my code so far:
Currently this is what I would see:
2004,11,1
2004,11,
2004,11,
2004,11,
etc...
The 1 value above is correct, yet I will get a 1 if I include the first line ($i=0 in my first for loop) which isn't right since it is a header.
Thanks,
John
I'm successfully breaking out the comma delimited fields into their own arrays for database inserts (the data is not exactly the same as the table structure). That all works fine and dandy, my problem is I wan't to do a lookup of the cost centers in the database and associate that unique ID with the cost center I am getting in my file.
Here is the first four lines of my csv file:
"YEAR","PAY PERIOD #","COST CENTER NUMBER","COST CENTER NAME","HOURS","UNITS",
"2004","11","6010","6010 ICU-CCU","716","30",
"2004","11","6170","6170 MEDICAL/SURGICAL","2053","248",
"2004","11","6380","6380 OB/GYN","700.75","54",
The third field is my cost center numbers which I would like to see like this:
2004,11,1,
2004,11,2,
2004,11,5,
etc...
Here is my code so far:
Code:
<?
$db = mysql_connect('db' , u' , 'p') or die ("Unable to connect to database server.");
mysql_select_db('prod') or die ("Unable to connect to database name.");
// open the file for importing
$f = fopen ("C:\\TEST.TXT", "r");
// read the file
$text = fread($f, filesize("C:\\TEST.TXT"));
fclose($f);
$query = "SELECT deptid, cost_center FROM depts";
$result = mysql_query($query, $db);
$num = mysql_num_rows($result);
// break the file down line by line
// each line represents one element in our array
$text = str_replace("\"", "", $text);
$text = explode("\n", $text);
for ($i=1; $i < count($text); $i++) {
list($year[$i], $pay_period[$i], $dept[$i], $name[$i], $hour[$i], $unit[$i]) = explode(',', $text[$i]);
for ($n=0; $n < $num; $n++) {
$cost = mysql_fetch_object($result);
if ($dept[$i] == $cost->cost_center) {
$cost_cen[$i] = $cost->deptid;
}
}
echo $year[$i].", ".$pay_period[$i].", ".$cost_cen[$i]."<br>";
}
for ($t=0; $t < $num; $t++) {
$test = mysql_fetch_object($result);
echo $test->deptid;
}
echo $num;
?>
Currently this is what I would see:
2004,11,1
2004,11,
2004,11,
2004,11,
etc...
The 1 value above is correct, yet I will get a 1 if I include the first line ($i=0 in my first for loop) which isn't right since it is a header.
Thanks,
John