php CODEIGNITER:自动将parent_id插入mysql

so I have a web program to upload a TSV file that contains table like this :

------------------------------------------------
SANDI     PENDAPATAN                            
------------------------------------------------
4.1       PENDAPATAN BUNGA/BAGI HSL & OPR UTAMA 
410       PENDAPATAN BUNGA/BAGI HASIL           
4100      PENDAPATAN BUNGA PENEMPATAN DANA
4101      PEND BNG/DISKON SRT BERHARGA YG DIBELI
4102      PENDAPATAN BNG.DOKUMEN/FASILITAS
4103      PEND BNG/PREMI TAGIHAN DERIVATIF
417       PENDAPATAN OPERASI UTAMA SYARIAH
4170      PENDAPATAN MARGIN MURABAHAH
4171      PENDAPATAN SALAM PARALEL
4172      PENDAPATAN ISTISHNA PARALEL
4173      PENDAPATAN BERSIH SEWA IJARAH

now take a look to the field "sandi" you can see the IDs are hierarchical-sorted.

4.1  => as a parent
410 and 417 => as a child of 4.1
4100-4103 => as a sub child of 410
4170-4172 => as a sub child of 417

I want process those flat data to be a tree data like this:

4.1 PENDAPATAN BUNGA/BAGI HSL & OPR UTAMA 
    410 PENDAPATAN BUNGA/BAGI HASIL           
        4100 PENDAPATAN BUNGA PENEMPATAN DANA
        4101 PEND BNG/DISKON SRT BERHARGA YG DIBELI
        4102 PENDAPATAN BNG.DOKUMEN/FASILITAS
        4103 PEND BNG/PREMI TAGIHAN DERIVATIF

This is the example of table structure to save the records :

COLUMN NAME        DATA TYPE 
------------------------------------
id_detail          int
sandi              varchar
pendapatan         varchar
parent_id          int

I have already searched about hierarchy data, parent and child etc. But I didnt found what I need. The question is: How to insert a field "parent_id" automatically based of the IDs on "sandi" with looping PHP?

Example:

id_detail  sandi         pendapatan                              parent_id
1          4.1           PENDAPATAN BUNGA/BAGI HSL & OPR         0
2          410           PENDAPATAN BUNGA/BAGI HASIL             1
3          4100          PENDAPATAN BUNGA PENEMPATAN DANA        2
4          4101          PEND BNG/DISKON SRT BERHARGA YG DIBELI  2

please help me, with a PHP CodeIgniter syntax.. thank you for your attention

Based on your detailed description of your question (although you didn't try anything), i'll show you how to accomplish things like that in CodeIgniter

first you need a controller

class ImportTSV extends CI_Controller
{

    public function import()
    {
        $this->load->model("ImportTSV_Model");
        $this->ImportTSV_Model->import();

    }
}

and a Model which contains the model it self and a Class for your Data Objects

class ImportTSV_Model extends CI_Model
{

    private $strFile = "data.tsv";
    private $masterNodeId = "41";
    private $arrDataObjects = array();

    public function __construct()
    {
        $this->prepareDataFromFile();
    }


    public function import()
    {
        $objMasterNode = $this->arrDataObjects[$this->masterNodeId];
        $this->save($objMasterNode);
    }

    private function save($objNode)
    {
        $arrInsertToDbData = array(
            "sandi" => $objNode->sandi,
            "pendapatan" => $objNode->pendapatan,
            "parent_id" => $objNode->mysqlParentId
        );

        $this->db->insert("your_table", $arrInsertToDbData);
        $id = $this->db->insert_id();

        if (count($objNode->arrChilds) > 0)
        {
            foreach($objNode->arrChilds AS  $objNode)
            {
                $objNode->mysqlParentId = $id;
                $this->save($objNode);
            }
        }
    }

    private function prepareDataFromFile()
    {
        $file = FCPATH."assets/".$this->strFile;

        $objFile = new SplFileObject($file);
        while(!$objFile->eof())
        {
            $line = $objFile->fgets();
            $arrDataPerLine = explode("\t",$line);

            $objTSVData = new TSVData_Object;
            $objTSVData->prepareDataFromLine($arrDataPerLine);
            $this->arrDataObjects[$objTSVData->formattedSandi] = $objTSVData;
        }

        foreach($this->arrDataObjects AS $objItem)
        {
            //set parent ID
            $parent = substr($objItem->formattedSandi,0,-1);
            if (isset($this->arrDataObjects[$parent]))  $this->arrDataObjects[$parent]->addChild($objItem);

        }
    }
}


class TSVData_Object
{
    public $sandi;
    public $pendapatan;
    public $formattedSandi;
    public $mysqlParentId = 0;
    public $arrChildObjects = array();


    public function prepareDataFromLine($arrLine)
    {
        $this->sandi = $arrLine[0];
        $this->pendapatan = $arrLine[1];
        $this->formattedSandi = str_replace(".","",$this->sandi);
    }

    public function addChild(TSVData_Object $objItem)
    {
        $this->arrChildObjects[] = $objItem;
    }
}

i assumed your file is in the directory FCPATH."assets/".