Linux ip-172-26-7-228 5.4.0-1103-aws #111~18.04.1-Ubuntu SMP Tue May 23 20:04:10 UTC 2023 x86_64
Your IP : 18.116.85.96
<?php
function uploadMCdetails($aobj_context)
{
$aobj_context->mobj_db->SetFetchMode(ADODB_FETCH_ASSOC);
session_start();
require_once dirname(__FILE__) . '/../PHPExcel/Classes/PHPExcel/IOFactory.php';
$univcode = $aobj_context->mobj_data["univcode"];
$filename = $aobj_context->mobj_data["gfilename"];
$type = $aobj_context->mobj_data["type"];
$date = date("dmYhis");
$folderpath = $aobj_context->main_src."/upload/";
$temp_path = $aobj_context->main_src."/upload/".$filename;
if(!file_exists($temp_path))
{
$data = "File is not uploaded and try again";
echo $aobj_context->mobj_output->ToJSONEnvelope($data,-1,"failure");
return;
}
// var_dump($temp_path);
$inputFileName = $temp_path;
$inputFileType = 'Excel2007';
///////////////////// Code To convert xlsx to csv \\\\\\\\\\\\\
$objReader = PHPExcel_IOFactory::createReader($inputFileType);
$objPHPExcelReader = $objReader->load($inputFileName);
$loadedSheetNames = $objPHPExcelReader->getSheetNames();
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcelReader, 'CSV');
$i = 0;
foreach($loadedSheetNames as $sheetIndex => $loadedSheetName)
{
if($i != 0)
break;
$objWriter->setSheetIndex($sheetIndex);
$csvFile = $folderpath.$date.$loadedSheetName.'.csv';
$objWriter->setUseBOM(true);
$objWriter->save($csvFile);
if($type == "DC Upload"){
$table_name = "DC_Details_{$univcode}";
}else{
$table_name = "MC_Details_{$univcode}";
}
$drop_query = "DROP TABLE IF EXISTS {$table_name}";
$results = $aobj_context->pobj_db->Execute($drop_query);
// var_dump($results); TEMPORARY
$create_query = "CREATE TABLE {$table_name} (
`fregno` varchar(10) NOT NULL DEFAULT '',
`fdegree` varchar(10) NOT NULL DEFAULT '',
`fexamno` varchar(1) NOT NULL DEFAULT '',
`fmcno` varchar(25) NOT NULL DEFAULT '',
PRIMARY KEY (`fregno`,`fdegree`,`fexamno`,`fmcno`),
KEY `fregno` (`fregno`),
KEY `fdegree` (`fdegree`),
KEY `fexamno` (`fexamno`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;";
$results = $aobj_context->pobj_db->Execute($create_query);
$query = "LOAD DATA LOCAL INFILE '{$csvFile}'
INTO TABLE {$table_name}
FIELDS TERMINATED BY \",\"
OPTIONALLY ENCLOSED BY '\"'
LINES TERMINATED BY \"\n\"
IGNORE 1 LINES
(fregno,fdegree,fexamno,fmcno)";
// var_dump($csvFile);
// die();
$results = $aobj_context->pobj_db->Execute($query);
// var_dump($results);
$rows_affected = $aobj_context->pobj_db->affected_rows();
// var_dump($rows_affected);
// return;
if(!$results)
{
$data = "Upload failure. Error while loading excel data to database";
echo $aobj_context->mobj_output->ToJSONEnvelope($data,-1,"failure");
return;
}
// Delete All the blank values from table
$query = "delete from {$table_name} where fregno = '' or fmcno = ''";
$results = $aobj_context->mobj_db->Execute($query);
if($type == "MC Upload"){
$query = "INSERT IGNORE INTO mcmsg(fregno, fdegree, fexamno, fmcno)
SELECT fregno, fdegree, fexamno, fmcno
FROM {$table_name}";
}else{
$query = "INSERT IGNORE INTO dcmsg(fregno, fdegree, fexamno, fmcno)
SELECT fregno, fdegree, fexamno, fmcno
FROM {$table_name}";
}
// var_dump($query);
// die();
$results = $aobj_context->pobj_db->Execute($query);
}
if($results)
{
$arr['msg'] = "Uploaded Successfully.";
echo $aobj_context->mobj_output->ToJSONEnvelope($arr,0,"success");
return;
}
else
{
$arr['msg'] = "Error while updating";
echo $aobj_context->mobj_output->ToJSONEnvelope($arr,-1,"error");
return;
}
}
|