Reading a txt file in php and parsing the data to MySQL - php

im new to php and i hope you can help with an easy to get script. I have a txt file with this general format:
5TB-2A, L+, 1, 1695, 3255, l.jpg
5TB-2A, L-, 2, 1965, 3270, l.jpg
5TB-2A, D-, 7, 2970, 3270, l.jpg
5TB-2A, DFOK, 8, 3225, 3300, l.jpg
I want to read this txt file using php to retrive every data separated by "," into my MySQL table. Thanks for any help.

$lines=file($yourTxt);
foreach($lines as $v) {
$values = explode(',',$v);
PDO::prepare("INSERT INTO tbl (?,?,?,?,?)");
PDO::execute($values);
}
You can make a further check too see if the line just read is ok like:
foreach($lines as $v) {
$values = explode(',',$v);
if (count($values)!=6)
continue;
}

You could use fgetcsv (http://php.net/fgetcsv) to read the file into an array in PHP and then write a query to insert it into the table one row at a time.

<?php
$results = array();
$rows = file($filename, FILE_SKIP_EMPTY_LINES);
if(empty($rows)){
// no data to parse
} else{
foreach($rows as $row){
$values = explode(', ', $row);
// keep only those values which have the 5 variables
if(count($values) > 5){
$results[] = $values;
}
}
}
// see if we have the correct data
print_r($results);
// ... then do your db inserts here
?>

Related

CSV league not skipping empty records

I am using PHPleague to parse csv and insert it to the database. https://csv.thephpleague.com/
And I have code as follows:
$csv = Reader::createFromPath($path, 'r');
$csv->skipEmptyRecords();
$csv->setHeaderOffset(0);
$csv_header = $csv->getHeader();
$stmt = (new Statement())
->offset('0')
->limit('20')
;
$records = $stmt->process($csv);
foreach($records as $record){
print_r($record); // this show that it has processed empty records as well
}
exit();
I could trim the empty records myself as well as by calling following function as $records = $this->trimArray($records);
public function trimArray($arr)
{
$final = array();
foreach($arr as $k => $v)
{
if(array_filter($v)) {
$final[] = $v;
}
}
return $final;
}
However, I want it to be trimmed before than that. Instead of adding my own layer to filter it is there anyway on csvleague to skipemptyrecords. I have tried with $csv->skipEmptyRecords(); but it is not skipping empty records. Am I doing anything wrong?
My file looks like:
I want to skip all those empty records.
I had a simple experience with League\CSV and tried to catch the data from a csv file. The package codes depend strongly on the version that you have installed. So at first step check the version of League. I think you should read the League documentation page about stream filters.
https://csv.thephpleague.com/9.0/connections/filters/

How to add an entry to mysqli array

I am trying to add a single column at the beginning of a csv file using the code below:
while ($row = mysqli_fetch_array($rows, MYSQL_ASSOC)) {
$list = "'2795', $row";
fputcsv($output, $list);
}
What am I missing? I know it's something simple. Thank you in advance.
You can't just join those values together:
$list = "'2795', $row";
Since $row returns a row result array, treat it as such, push that value inside:
$output = fopen('whatevername.csv', 'a+');
while ($row = mysqli_fetch_array($rows, MYSQLI_ASSOC)) {
$row[] = '2795'; // `$row` is an associative array
fputcsv($output, $row);
}
fclose($output);
Sidenote: This is a truncated code, so just make sure you have that file handle above this code that you presented.

Read the excel data and inserting in below format

I have data in excel sheet which looks like below.
I have to read the data(i'll try csv format, please suggest otherwise). Using PHP to read data...
Create two tables, hopefully like below.
1.
2.
I am expecting ExtJs tree like this.
I am having trouble in step 1. How shall I read my excel sheet, csv file so that database is updated like table 1
Here's a solution, though I did not maintain the same Data_Id's as you did (you seem to increment the id by depth and node, but I just used the row number).
<?php
$data = <<<EOT
Eatables,,
,Fruits,
,,Apple
,,Mango
,Vegetables,
,,Tomatoes
,,Potatoes
EOT;
$mysqli = new mysqli("localhost", "username", "password", "test");
$dataInsertStatement = $mysqli->prepare("INSERT INTO Data (Data_Id, Data_Value) VALUES (?, ?)");
$treeInsertStatement = $mysqli->prepare("INSERT INTO Tree (parent_id, child_id) VALUES (?, ?)");
$lines = explode("\n", $data);
$path = array();
foreach ($lines as $rowNum => $line) {
// convert line of csv to array
$row = str_getcsv($line);
// loop through the row's fields and find the one that's not empty
foreach ($row as $column => $value) {
if (!empty($value)) {
// use row number + 1 as $Data_Id
$Data_Id = $rowNum + 1;
// track our depth in the tree
$path[$column] = $Data_Id;
// insert the data
$dataInsertStatement->bind_param('is', $Data_Id, $value);
$dataInsertStatement->execute();
// check if this node has a parent
if (isset($path[$column - 1])) {
// connect this node to its parent in the tree
$treeInsertStatement->bind_param('ii', $path[$column - 1], $Data_Id);
$treeInsertStatement->execute();
}
continue;
}
}
}
$mysqli->close();
something like
lastvalues=array(); // To hold the last value on each level
data=array();
foreach($lines as $i=>$line){
elems=explode(',',$line);
foreach($elems as $n=>$e){
if($e>''){
$data[$i]=$e;
lastvalues[$n]=$i;
if($n){ // makes no sense for 0th level
$tree[]=array('parent'=>$lastvalues[$n-1],child=>$i);
}
}
}
}
Should give you the data structures. You may use SQL inserts rather than the $data and $tree arrays.

PHP explode function loop to be used on column headers to return desired values into an excel file

So I used this code to return an array from a mysql query headers into an Excel file.
while($row_data = mysql_fetch_assoc($result)) {
$row = 1;
if ($col == 1) {
$row_headings = array_keys($row_data);
foreach($row_headings as $value) {
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $value);
$row++;
}
$row = 1;
$col++;
These query headers are returned as values such as 1111X2222X3333. I want to explode these values using this explode function somewhere in there
list($sid,$gid,$qid) = explode("X", $string);
After this function is used I want to use this query to replace all these column headers with my desired values using this query in the loop
select * from lime_questions where sid = 1111 and gid = 2222 and qid = 3333
If there is a better way to do this please let me know. I appreciate the help

PHP Array Explode

I am imploding data inside of an array, called the following:
["Levels_SanctionLevel_array"]=>
string(14) "IR01,IR02,IR03"
I need to explode this data and input each value as a row inside of mysql. Such as
pri_id value
---------------
01 IR01
02 IR02
03 IR04
Now where I am getting stuck is this:
The array listed above could have 1 value, 3 values (right now I am showing three values) or 5 values, and I dont want to input NULL values inside of my database.
Appreciate any guidance anyone can share...
$data = explode(',',$your_string);
foreach ($data AS $value) {
// INSERT INTO DB
}
A simple loop works correctly, but has the drawback of making multiple queries to the database:
$str = "IR01,IR02,IR03";
$vals = explode(',', $str);
foreach ($vals AS $value) {
// insert into database
}
As DB operations are a more significant bottleneck than building a query in PHP, I would opt to generate the SQL code for a single query as follows:
$str = "IR01,IR02,IR03";
$vals = explode(',', $str);
$query = 'INSERT INTO my_data VALUES ';
foreach ($vals as $value) {
$query .= "('', '".$value."'),";
}
$query = rtrim($query, ',');
// insert into database
Why don't you use an iterator over the array to construct the sql query? That way you will only insert as many elements as you have in the array.
Like the answer above. I think we were answering at the same time.
Supposing your major array is $data:
foreach ($data as $values)
{
$value = explode(",", $values);
foreach ($value as $v)
{
$sql = "INSERT INTO table(value) VALUES('$v')";
}
}

Categories