PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, February 16, 2022

[FIXED] How to read columns from text file and insert in database php

 February 16, 2022     cakephp, php     No comments   

Issue

I have a text file with multiple rows and columns inside them.

I want to read each and every row and column, store them in array and save all data in database using cakephp.

Below is my code, I wrote some row and column reading logic which i wants to implement.

Please help me to do this critical thing.

public function importfile(){
        $fp = 'C:/wamp64/www/jhraut/webroot/uploads/120518SU';
        $handle = fopen($fp, "r");
        if ($handle) {
            while (($line = fgetc($handle)) !== false) {

                $data['GunNo'] = "first 5 characters of $line is GunNo than 1 character is space";
                $data['FatGun'] = "Second 3 characters of $line is FatGun than 1 character is space";
                $data['LoinGun'] = "Third 3 characters of $line is LoinGun than 1 character is space";
                $data['ScaleWt'] = "fourth 5 characters of $line is ScaleWt than 1 character is space";
                $data['Partial'] = "if P or M than 1 character is space";
                $data['TimeofReading'] = "last 8 characters of $line is TimeofReading";
                echo $line;
            }
            $this->Event_program->saveAll($data);
        }        
        fclose($fp);
        exit;
    }

My file data

parti 011 058 145.6 P  06:37:01
00002 016 049 175.8    06:37:08
00003 009 072 150.8    06:37:15
00004 009 053 146.8    06:37:22
00005 011 054 169      06:37:29
00006 009 052 152.4    06:37:37
00007 018 059 194.8    06:37:44
00008 009 060 139.4    06:37:51
parti 008 069 134.8 P  06:37:58
00010 023 054 194.2    06:38:05
miss          197.2    06:38:13
00011 023 052 150      06:38:20
00012 008 059 146.6    06:38:27
00013 010 067 156      06:38:34
00014 013 049 190.8    06:38:41

Solution

Try something like this:

// set path to file
$file = WWW_ROOT.'uploads/120518SU';
// check if file exists
if (file_exists($file)) {
   // Reads an entire file into an array with file() method
   // and loop array 
   foreach (file($file) as $line) {
      // convert line value to new array
      $arr = explode(' ', $line);

      // do something with your data..
      $data = [
         'GunNo' => $arr[0],
         // ...
      ];

      $entity = $this->EventProgram->newEntity($data);
      $this->EventProgram->save($entity);
   }
}

Update with some test:

$line = '00003 009 072 150.8    06:37:15';
$arr = explode(' ', $line);

print_r($arr);

// output

Array
(
    [0] => 00003
    [1] => 009
    [2] => 072
    [3] => 150.8
    [4] => 
    [5] => 
    [6] => 
    [7] => 06:37:15
)

$line = 'parti 008 069 134.8 P  06:37:58';

// .. 

Array
(
    [0] => parti
    [1] => 008
    [2] => 069
    [3] => 134.8
    [4] => P
    [5] => 
    [6] => 06:37:58
)

Then:

// do something with your data..
$data = [
   // ...
  'TimeofReading' => end($arr),

];

Update: reading as csv file

Use fgetcsv()

The fgetcsv() function parses a line from an open file, checking for CSV fields.

The fgetcsv() function stops returning on a new line, at the specified length, or at EOF, whichever comes first.

This function returns the CSV fields in an array on success, or FALSE on failure and EOF.

fgetcsv(file,length,separator,enclosure);


Answered By - Salines
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing