How to store csv file to the database in cake php by coding

by admin on February 20th, 2012

function admin_exceladd(){
if (!empty($this->data)) {
if(is_uploaded_file($this->data[‘Webshop’][‘excelfile’][‘tmp_name’])){
move_uploaded_file($this->data[‘Webshop’][‘excelfile’][‘tmp_name’], ‘img/files/’.$this->data[‘Webshop’][‘excelfile’][‘name’]);
$file = ‘img/files/’.$this->data[‘Webshop’][‘excelfile’][‘name’];

$handle = fopen($file, “r”);

// read the 1st row as headings
$header = fgetcsv($handle);

// read each data row in the file
$i = 0; // for the message
while (($row = fgetcsv($handle)) !== FALSE) {
$i++;
$data = array();
$data1 = array();

// for each header field
foreach ($header as $k=>$head) {
// get the data field from Model.field
if (strpos($head,’.’)!==false) {
$h = explode(‘.’,$head);
$data1[$h[0]][$h[1]]=(isset($row[$k])) ? $row[$k] : ”;
}
// get the data field from field
else {
$data1[$head]=(isset($row[$k])) ? $row[$k] : ”;
}
}
print_r($data1);exit;
//get network id
if($data1[‘Affiliate network’] == ‘Tradedoubler’){
$data1[‘network_id’] = ‘2’;
}elseif($data1[‘Affiliate network’] == ‘M4N’){
$data1[‘network_id’] = ‘1’;
}

if(!$data1[‘Official site URL’]){
$data1[‘Official site URL’] = ‘none’;
}

if(!$data1[‘Webshop discription’]){
$data1[‘Webshop discription’] = $data1[‘Description’];
}

if(!$data1[‘Percentage commission’]){
$data1[‘Percentage commission’] = ‘0’;
}

if(!$data1[‘amount commission’]){
$data1[‘amount commission’] = ‘0’;
}

$data[‘Webshop’][‘shopname’] = $data1[‘Name’];
$data[‘Webshop’][‘shopurl’] = $data1[‘Official site URL’];
$data[‘Webshop’][‘description’] = $data1[‘Webshop discription’];
$data[‘Webshop’][‘percentcommission’] = $data1[‘Percentage commission’];
$data[‘Webshop’][‘amountcommission’] = $data1[‘amount commission’];
$data[‘Webshop’][‘affiliatelink’] = $data1[‘Affiliate link’];
$data[‘Webshop’][‘network_id’] = $data1[‘network_id’];

//Get categories from array, get their id from categories table and join them with categories in categories_webshops table
if($data1[‘Category 1’]){
$cat_id = $this->Webshop->Category->findByName($data1[‘Category 1’]);
$data[‘Category’][‘Category’][0] = $cat_id[‘Category’][‘id’];
}
if($data1[‘Category 2’]){
$cat_id = $this->Webshop->Category->findByName($data1[‘Category 2’]);
$data[‘Category’][‘Category’][1] = $cat_id[‘Category’][‘id’];
}
if($data1[‘Category 3’]){
$cat_id = $this->Webshop->Category->findByName($data1[‘Category 3’]);
$data[‘Category’][‘Category’][2] = $cat_id[‘Category’][‘id’];
}

//Check if this Webshop is present in the database, same webshop names can be under two networks, so check for both webshop name and network_id
if(!$this->Webshop->find(‘first’, array(‘conditions’ =>
array(‘Webshop.shopname’ => $data[‘Webshop’][‘shopname’], ‘Webshop.network_id’ => $data[‘Webshop’][‘network_id’])))){
$this->Webshop->create();
if ($this->Webshop->save($data)) {
$this->Session->setFlash(__(‘The Webshop has been saved’, true));
$this->Webshop->id = false;
}else{
$data2[$i][‘name’] = $data[‘Webshop’][‘shopname’];
}
}

} //while

}
}
} //function exceladd ends here

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>