Forum Moderators: coopster

Message Too Old, No Replies

Spreadsheet into Postgresql using PHP

Need help

         

Clay213

1:32 am on Aug 4, 2009 (gmt 0)

10+ Year Member



Using PHPexcel, I have an excel file loaded into an array. I need to insert these records into a table in my postgresql database.

I need the column headers from the file to be placed in the right colums in the table.

phppgadmin has a function which does essentially what I am trying to do but will only work with CSV or XML. When I upload a CSV it doesn't matter which order the columns are in-- the values are inserted into the correct column in the table.

I've tried rewriting the function from that package to suit my needs but I can't get it to work.

How can I write a loop which will verify the column names instead of just looping through and putting the values in sequentially across the row?

Thanks, I appreciate the help

Clay213

3:20 am on Aug 5, 2009 (gmt 0)

10+ Year Member



Anyone?

messageboy

3:25 am on Aug 5, 2009 (gmt 0)

10+ Year Member



I not sure what you are trying to do and I got no experience with postgresql. (more mysql) However you might be able to do what you want with php with stristr in a if statement or case statement.

[us2.php.net...]

Clay213

3:42 am on Aug 5, 2009 (gmt 0)

10+ Year Member



Right now I am hung up on this line here:

while ($line = fgetcsv($fd, $csv_max_line, $csv_delimiter))

That is from the phppgadmin dataimport. It's followed by a foreach loop

I am trying to translate it since I am not using a csv.

If I understand correctly fgetcsv returns an array right? Well so I thought maybe I could just replace everything after the = with my array variable.. but it doesn't seem to work

Clay213

3:44 am on Aug 5, 2009 (gmt 0)

10+ Year Member



Actually, scratch that last message, I think I just figured that part out

No, I didn't.

It bombs out here and returns Row 2

// Get first line of field names
$fields = $sheetArray;
$row = 2; //We start on the line AFTER the field names
while ($line = $sheetArray) {
// Build value map
$vars = array();
$nulls = array();
$format = array();
$types = array();
$i = 0;
foreach ($fields as $f) {
// Check that there is a column
if (!isset($line[$i])) {
echo '<br><br>' . $row;
exit;
}