i want to import excel as json data in Codeigniter with generate id.
my model
public function generateImpId() {
$now = date('ymd');
$check = "SELECT COUNT(*) as count FROM OP_IMP_15 WHERE DATE(OP_IMP_15.created_at) = DATE(NOW())";
$querycheck = $this->db->query($check);
$id = $querycheck->row()->count;
return 'IMPDEMO'.$now.str_pad($id, 4, '0', STR_PAD_LEFT);
}
my controller
//count row
$lastRow = $objPHPExcel->setActiveSheetIndex($sheetnumber)->getHighestRow();
$countrow = $lastRow - 1;
//start loop excel from 2nd row. Row 1 is title row
for ($j=2; $j < $lastRow; $j++ ){
$myArray[] = array(
'site_id' => $objWorksheet->getCell('C'.$j)->getValue(),
'site_name' => $objWorksheet->getCell('D'.$j)->getValue(),
'id_site_doc'=> $objWorksheet->getCell('J'.$j)->getValue(),
'id_project_doc' => $objWorksheet->getCell('K'.$j)->getValue(),
//generate ID from model
'implementation_id' => $this->M_MRCR_A->generateImpId(),
...
the result
{ "site_id":"AR001",
"site_name":"Site AR 001",
"id_site_doc":"5df1b4223269f818adab55af",
"id_project_doc":"5da43895e619143bcff53ab1",
"implementation_id":"IMPDEMO2003310001",
"status":"implementation_created",
"endstate":false,
"created_by":"sgph_pm@mittapp.com",
"counter_mr":"0",
"tech_boq_ids":[
],
...
{ "site_id":"AR002",
"site_name":"Site AR 002",
"id_site_doc":"5df1b4223269f818adab55af",
"id_project_doc":"5da43895e619143bcff53ab2",
"implementation_id":"IMPDEMO2003310001",
"status":"implementation_created",
"endstate":false,
"created_by":"sgph_pm@mittapp.com",
"counter_mr":"0",
"tech_boq_ids":[
],
my expectation
to make "implementation_id" increment based on how many rows the data will be imported and format based on custom ID i'd made (i can't use uuid). ex: i have 3 row to be imported, so the value of $implementation_id will be : IMPDEMO2003310001, IMPDEMO2003310002, IMPDEMO2003310003
You shoulnd't try to do everything in one single loop.
Maybe you should use a first iteration (for loop) to create the array, and then, a second iteration to fill this implementation_id field.
You can do this by getting the start implementation ID at the start of the loop and just increment it each time (PHP can cope with incrementing a string like this)...
$implementationID = $this->M_MRCR_A->generateImpId();
//start loop excel from 2nd row. Row 1 is title row
for ($j=2; $j < $lastRow; $j++ ){
$myArray[] = array(
// ...
//generate ID from model
'implementation_id' => $implementationID++,
that should give you values like...
IMPDEMO2003310001
IMPDEMO2003310002
IMPDEMO2003310003
Well since you already have the generated implementation_id
value, and I have no idea what the generateImpId()
function do, you could manually replace each generated implementation_id
with the $j
you have :
//count row
$lastRow = $objPHPExcel->setActiveSheetIndex($sheetnumber)->getHighestRow();
$countrow = $lastRow - 1;
//start loop excel from 2nd row. Row 1 is title row
for ($j=2; $j < $lastRow; $j++ ){
$implementation_id = $this->M_MRCR_A->generateImpId();
$implementation_id = substr($implementation_id, 0, -4) . str_pad($j-1, 4, '0', STR_PAD_LEFT); // since $j starts with 2
$myArray[] = array(
'site_id' => $objWorksheet->getCell('C'.$j)->getValue(),
'site_name' => $objWorksheet->getCell('D'.$j)->getValue(),
'id_site_doc'=> $objWorksheet->getCell('J'.$j)->getValue(),
'id_project_doc' => $objWorksheet->getCell('K'.$j)->getValue(),
//generate ID from model
'implementation_id' => $implementation_id,
...
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.