Reputation: 153
I'm getting a list of columns from an Excel with PHPExcel, so far everything is fine, I can get the array and see the data on screen, even the columns that interest me and omitting the first rows, the problem is that I can not insert these array results in a database, I can not even insert the first record in the array. Can you help me? Thank you!
<?php
require_once 'connect.php';
require_once 'Excel/PHPExcel/IOFactory.php';
$objPHPExcel = PHPExcel_IOFactory::load('prices.xlsx');
$worksheet = $objPHPExcel->getSheet(0);
foreach($objPHPExcel->getWorksheetIterator() as $worksheet)
{
$highestRow = $worksheet->getHighestRow();
$highestColumn = $worksheet->getHighestColumn();
for($row=8; $row<=$highestRow; $row++)
{
$column1 = $worksheet->getCellByColumnAndRow(5, $row)->getValue();
$column2 = $worksheet->getCellByColumnAndRow(10, $row)->getValue();
$column3 = $worksheet->getCellByColumnAndRow(11, $row)->getValue();
$finaldata[] = array(
'Key' => trim($column1),
'Price' => trim($column2),
'currency' => trim($column3),
);
}
}
echo "<pre>";
print_r($finaldata);
die();
?>
I get the following:
Array
(
[0] => Array
(
[Key] => 100001
[Price] => 40.83
[currency] => USD
)
[1] => Array
(
[Key] => 100002
[Price] => 624.94
[currency] => USD
)
[2] => Array
(
[Key] => 100003
[Price] => 69.74
[currency] => USD
)
[3] => Array
(
[Key] => 100004
[Price] => 150.62
[currency] => USD
)
[4] => Array
(
[Key] => 100005
[Price] => 223.15
[currency] => USD
)
[5] => Array
(
[Key] => 100006
[Price] => 92.94
[currency] => USD
)
)
Now I'm trying to insert all the records in my database with the following code:
<?php
DB::insert('prices', array(
'Key' => $column1,
'Price' => $column2,
'currency' => $column3
));
?>
I hope you can help me, thank you very much in advance for your time!
Upvotes: 1
Views: 73