Codeigniter generate increment ID for looping

pramudityad :

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":"[email protected]",
      "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":"[email protected]",
      "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

Hasta Dhana :

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,
    ...

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=401854&siteId=1