**How to open, read, transfer all content line by line in database depend upon their specification using php???????
**

I have a txt file that deals with numbers and the delimiter is not present in numbers. But i want to store that huge numbers depend upon there description.

For example :- abc.txt

34012015000220001000631931058620000607262Y22122014 N                                                                                                                                                                                                                                                                                                                    
34012015000220002000637717660206000607262Y23122014 N                                                                                                                                                                                                                                                                                                                    
34012015000220003000647245251009000607262Y23122014 N 
.
.
.
.
.
.
.
.
.
.
.
.
.

// and so on...

Here is specification is

34 is constant value.
        012015000220001-record No.
        000631931058620-Aadhar No.
        000607262-BIN no.
        Y-record status
        22122014-date
         -one blank space
        N-Mapping Status

**Each new line is a new customer with their individual details.
There are huge line present in the text file.that all line which store in the database by using there specification in there specific column in mysql database **

So please sir give me any solution to solve this problem?????

Member Avatar for diafol

Here's an example of what you could do:

<?php

class SplitAndInsert
{
    private $db;
    private $table;
    private $fields;
    private $file;
    private $pattern;
    private $transformArray = [];

    public function __construct($db, $table, $fieldArray, $file, $pattern, $transformArray=null)
    {
        $this->db = $db;
        $this->table = $table;
        $this->fields = array_map(function($item){ return '`' . $item .'`';}, $fieldArray);
        if(file_exists($file))
        {
            $this->file = $file;
        }
        $this->pattern = $pattern;
        if($transformArray) $this->transformArray = $transformArray;
    }


    private function get_split_data($line)
    {
        preg_match($this->pattern,$line,$matches);
        array_shift($matches);
        return $matches;
    }

    private function get_line_array()
    {
        $lines = file($this->file);
        $output = [];

        foreach($lines as $line)
        {
            $output[] = $this->get_split_data($line);
        }
        return $output;
    }

    public function get_array_data()
    {
        return $this->get_line_array();
    }


    public function insert_to_db()
    {
        $data = $this->get_array_data();
        $fieldString = implode(',',$this->fields);

        $sql = "INSERT INTO {$this->table} ($fieldString) VALUES ";

        $rowHolders = rtrim(str_repeat("?,", count($this->fields)),',');

        $placeHolders = rtrim(str_repeat(rtrim("($rowHolders),"),count($data)),',');

        $sql .=  $placeHolders;
        $stmt = $this->db->prepare($sql);

        $i = 1;

        foreach($data as $item)
        {
            for($j=0;$j<count($this->fields);$j++)
            {
                if(in_array($j, array_keys($this->transformArray)))
                {
                    $item[$j] = $this->transformArray[$j]($item[$j]);
                }
                $stmt->bindValue($i++, $item[$j]);
            }
        }
        $stmt->execute();
    }

}


//HELPER FUNCTION

function transformDate($date)
{
    return DateTime::createFromFormat('dmY', $date)->format('Y-m-d');
}

//USAGE

$db = new PDO('mysql:host=localhost;dbname=...dbname...','...username...','...pw...');
$table = 'mytablename';
$pattern = '/(\d{2})(\d{15})(\d{15})(\d{9})(\S)(\d{8}) (\S)/';
$file = 'myfile.txt';
$fields = ['const','record_no','aadhar_no','bin_no','record_status','date','mapping_status'];
$transformArray = [5=>'transformDate'];

$x = new SplitAndInsert($db,$table,$fields,$file,$pattern,$transformArray);
print_r($x->get_array_data());

$x->insert_to_db();

There is no error handling or anything, and the constructor is pretty ridiculous. Just to give you an idea. I've thrown the kitchen sink into this class, which is not what you should do in general.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.