I have a TXT
file with no punctuation between them. I would like to shred this file by the table column widths in the database and save it.
Let me tell you this step by step…
I’m creating a table in the database with my
tabloolustur.php
page. The column numbers and column widths of the tables I create will not be the same.There are no punctuation marks between the
TXT
file data. First, I want to split the TXT file rows by column width.$result = $baglanti->prepare("SHOW COLUMNS FROM customers where Field NOT IN('id')"); $result->execute(); $colcount = $result->columnCount()-1; $columLen = array(); foreach($result as $key => $col){ preg_match('/\d+/', $col['Type'], $len); $len = (isset($len[0]))? $len[0] : ''; $fieldname = $col['Field']; $columLen[$fieldname] = $len; }
For this, I get the number of columns and column widths with the code.
Then, I separate the data with commas with the following function.
function txtBol($metin, $genislik){ $parcala=array(); foreach ($genislik as $sutunadi => $lenght) { $parcala[$sutunadi] = substr($metin, 0, $lenght); $metin = substr($metin, $lenght); } return $parcala; }
I also get column names with the following code. (ps: to use in a query)
$KolAdi = $baglanti->query("SHOW COLUMNS FROM customers where Field NOT IN('id')"); $KolAdi->execute(); $colonAdi= $KolAdi->fetchAll(PDO::FETCH_COLUMN); $colonAdi=implode(',', $colonAdi);
It prints the data i split correctly when printing it to the screen. So far, so good. But I can’t create the right query with PDO. How should I create the query? (ps: Table column names and column widths are not the same. There will be different numbers and width columns for each table) I would appreciate it if you could help. I proceeded by implementing some solutions from your site.
Table
:
id | name | cev1 | cev2 | cev3 |
---|---|---|---|---|
1 | MARTIN EDEN | AAAAAA | BBBBB | CCCC |
txt
:
MARTIN EDEN........AAAAAABBBBBDDDD