Database always adding 9 empty rows - php

So I am importing data from an .asc file and I am putting it into a database. Everything gets inserted but in the beginning it has a little problem.
This is the code:
<?php
function importdb()
{
include('db_config.php');
$File = 'lijst.csv';
$File2 = 'preise.asc';
$handle = fopen($File, "r");
$handle2 = fopen($File2, "r");
$arrResult = array();
$arrResult2 = array();
fgetcsv($handle);
fgetcsv($handle);
fgetcsv($handle);
fgetcsv($handle);
fgetcsv($handle);
fgetcsv($handle);
while (($data = fgetcsv($handle, 1000, ";")) !== FALSE && ($data2 = fgetcsv($handle2, 1000, ";")) !== FALSE) {
//---------------------
$artikelnmr = $data[0];
$barcode = $data[1];
$omschrijving_nl = $data[2];
$omschrijving_exp = $data[3];
$bruto_prs = $data[4];
$staffel_prs = $data[5];
$aktie_prs = $data[6];
$bruto_antl = $data[8];
$staffel_antl = $data[9];
$aktie_aantal = $data[10];
$voorraad = $data[15];
$leverdatum = $data[16];
$besteld = $data[17];
$pallet_antl = $data[19];
$artikel_groep = $data[22];
$extra_info = $data[27];
//------------------------
$type = $data2[0];
$artikel = $data2[1];
$prijs1 = $data2[6];
$prijs2 = $data2[7];
$prijs3 = $data2[8];
$prijs4 = $data2[9];
$prijs5 = $data2[10];
//----------------------
$stmt = $db->prepare("INSERT INTO `producten`(`id`, `artikelnr`, `barcode`, `omschrijving_nl`, `omschrijving_exp`, `bruto_prijs`, `bruto_aant`, `staffel_prijs`, `staffel_aantal`, `aktie_prijs`, `aktie_aantal`, `voorraad`, `leverdatum`, `besteld`, `pallet_aantal`, `artikel_groep`, `extra`)
VALUES ('', :artikelnmr,:barcode,:omschrijving_nl,:omschrijving_exp,:bruto_prijs,:bruto_aantal,:staffel_prijs,:staffel_aantal,:aktie_prijs,:aktie_aantal,:voorraad,:leverdatum,:besteld,:pallet_aantal,:artikel_groep,:extra)");
$stmt2 = $db->prepare("INSERT INTO `prijzen`(`artikelnr`, `prijs_soort`, `prijs1`, `prijs2`, `prijs3`, `prijs4`, `prijs5`) VALUES (:nmr, :soort, :prijs1, :prijs2, :prijs3, :prijs4, :prijs5)");
//----------------------
$stmt->bindParam(":artikelnmr", $artikelnmr);
$stmt->bindParam(":barcode", $barcode);
$stmt->bindParam(":omschrijving_nl", $omschrijving_nl);
$stmt->bindParam(":omschrijving_exp", $omschrijving_exp);
$stmt->bindParam(":bruto_prijs", $bruto_prs);
$stmt->bindParam(":bruto_aantal", $bruto_antl);
$stmt->bindParam(":staffel_aantal", $staffel_antl);
$stmt->bindParam(":staffel_prijs", $staffel_prs);
$stmt->bindParam(":aktie_aantal", $aktie_aantal);
$stmt->bindParam(":aktie_prijs", $aktie_prs);
$stmt->bindParam(":voorraad", $voorraad);
$stmt->bindParam(":leverdatum", $leverdatum);
$stmt->bindParam(":besteld", $besteld);
$stmt->bindParam(":pallet_aantal", $pallet_antl);
$stmt->bindParam(":artikel_groep", $artikel_groep);
$stmt->bindParam(":extra", $extra_info);
//----------------------
$stmt2->bindParam("nmr", $artikel);
$stmt2->bindParam(":soort", $type);
$stmt2->bindParam(":prijs1", $prijs1);
$stmt2->bindParam(":prijs2", $prijs2);
$stmt2->bindParam(":prijs3", $prijs3);
$stmt2->bindParam(":prijs4", $prijs4);
$stmt2->bindParam(":prijs5", $prijs5);
//----------------------
$stmt2->execute();
$stmt->execute();
echo $artikel . ': ' . $type . "<br>";
}
fclose($handle);
fclose($handle2);
}
importdb();
?>
This only happens in the second statement (stmt2). Everything is okay in the first statement (stmt)
This is what happens in the database and what should be put in the first few lines:
Database Result
What should be inserted
Do I need to skip a few lines just like I did in the first file?
Thanks in advance.

Related

How to add the results of a sql oracle query in a csv file in php?

I have a php code that I connect to an oracle database and with a request to retrieve the information I want, here is my code :
$query = "SELECT ACTIVE_SIZES FROM ADA_ACTIVE_SIZE2_VIEW WHERE ADA_STYLE = 'SCPCL4'";
$result = odbc_exec($connect, $query);
while($final = odbc_fetch_array($result)) {
print_r($final); //Array ( [ACTIVE_SIZES] => XS-S-M-L-XL-2XL )
}
Now I'm reading a csv file and I would like to adapt this code to add the results of my queries in a column at the end of the file. I already add two columns at the end of this one, but my query doesn't return anything in the csv file, how can I do please?
<?php
//Modifications on csv file
$delimiter = ";";
$csv_data = array();
if (($handle = fopen($nomcsv, 'r')) !== FALSE) {
while (($data = fgetcsv($handle, 10000, $delimiter)) !== FALSE) {
//Add two columns at the end
$data['Pictures Names'] = (!empty($data[4]) ? ($data[7] ?: '') . "_" . $data[4] . '.jpg' : '');
$data['Color-Description'] = (!empty($data[3]) ? (ltrim($data[4], '0') ?: '') . "-" . $data[3] : '');
//Query
$query = "SELECT ACTIVE_SIZES FROM ADA_ACTIVE_SIZE2_VIEW WHERE ADA_STYLE = '".$data[4]."'";
$result = odbc_exec($connect, $query);
while($final = odbc_fetch_array($result)) {
$data['Sizes'] = $final;
var_dump($final); //array(1) { ["ACTIVE_SIZES"]=> string(8) "XS-S-M-L" }array(1) { ["ACTIVE_SIZES"]=> string(8) "XS-S-M-L" }...
}
$csv_data[] = $data;
var_dump($csv_data); //["Pictures Names"]=> string(15) "SCJEG4_1041.jpg" ["Color-Description"]=> string(12) "1041-MUSTARD" ["Sizes"]=> array(1) { ["ACTIVE_SIZES"]=> string(15) "XS-S-M-L-XL-2XL" } } }
}
fclose($handle);
}
if (($handle = fopen($nomcsv, 'w')) !== FALSE) {
foreach ($csv_data as $data) {
fputcsv($handle, $data, $delimiter);
}
fclose($handle);
}
?>
At the end I have this in my csv file:
Try this, only thing is you need to define your ActiveSizes index and push it to the sizes index
$delimiter = ";";
$csv_data = array();
if (($handle = fopen($nomcsv, 'r')) !== FALSE) {
while (($data = fgetcsv($handle, 10000, $delimiter)) !== FALSE) {
//Add two columns at the end
$data['Pictures Names'] = (!empty($data[4]) ? ($data[7] ?: '') . "_" . $data[4] . '.jpg' : '');
$data['Color-Description'] = (!empty($data[3]) ? (ltrim($data[4], '0') ?: '') . "-" . $data[3] : '');
//Query
$query = "SELECT ACTIVE_SIZES FROM ADA_ACTIVE_SIZE2_VIEW WHERE ADA_STYLE = '".$data[4]."'";
$result = odbc_exec($connect, $query);
while($row = odbc_fetch_array($result)) {
$data['Sizes'][] = $row['ACTIVE_SIZES'];
}
$csv_data[] = $data;
}
fclose($handle);
}
//var_dump($csv_data);
if (($handle = fopen($nomcsv, 'w')) !== FALSE) {
foreach ($csv_data as $data) {
fputcsv($handle, $data, $delimiter);
}
fclose($handle);
}

Unable to import Full "CSV" rows using "PHP" Ajax

I'm using getcsv to import csv into database.I have huge number of datasheets in csv format. Currently i'm importing a csv which has 53,000 rows and 35 columns. when i decerase columns it's importing approx all the rows in database, but it's not importing when i want to import all the columns of csv into mysql table.
my csv has all type of character like char,numbers, symbols,specila charaters etc
Please look at my code:
Index.php:
<form action="upload.php" method="post" enctype="multipart/form-data"> <input type="text" name="type2" Placeholder='Type Folder Name' required > <input type="file" name="csv">
<input type="submit" name="submit" value="Import" class="btn btn-success waves-effect waves-light"/>
</form>
UPLOAD.php
<script>
//Declaration of function that will insert data into database
function senddata(filename){
var file = filename;
$.ajax({
type: "POST",
url: "senddata.php",
data: {file},
async: true,
success: function(html){
$("#result").html(html);
}
})
}
</script>
$csv = array();
$batchsize = 1000; //split huge CSV file by 1,000, you can modify this based on your needs
if($_FILES['csv']['error'] == 0){
$name = $_FILES['csv']['name'];
$ext = explode('.', $_FILES['csv']['name']);
$file_ext=strtolower(end($ext));
$tmpName = $_FILES['csv']['tmp_name'];
if($file_ext === 'csv'){ //check if uploaded file is of CSV format
if(($handle = fopen($tmpName, 'r')) !== FALSE) {
set_time_limit(0);
$row = 0;
while(($data = fgetcsv($handle)) !== FALSE) {
$col_count = count($data);
//splitting of CSV file :
if ($row % $batchsize == 0):
$file = fopen("minpoints$row.csv","r");
endif;
$csv[$row]['pno'] = $data[0];
$csv[$row]['area'] = $data[1];
$csv[$row]['project'] = $data[2];
$csv[$row]['flatno'] = $data[3];
$csv[$row]['balcony'] = $data[4];
$csv[$row]['parking'] = $data[5];
$csv[$row]['common_area'] = $data[6];
$csv[$row]['floor'] = $data[7];
$csv[$row]['rooms'] = $data[8];
$csv[$row]['shop'] = $data[9];
$csv[$row]['flats'] = $data[10];
$csv[$row]['offices'] = $data[11];
$csv[$row]['category'] = $data[12];
$csv[$row]['total_area'] = $data[13];
$csv[$row]['plot_no'] = $data[14];
$csv[$row]['emirates'] = $data[15];
$csv[$row]['name'] = $data[16];
$csv[$row]['area_owned'] = $data[17];
$csv[$row]['address'] = $data[18];
$csv[$row]['phone'] = $data[19];
$csv[$row]['email'] = $data[20];
$csv[$row]['fax'] = $data[21];
$csv[$row]['pobox'] = $data[22];
$csv[$row]['gender'] = $data[23];
$csv[$row]['dob'] = $data[24];
$csv[$row]['mobile'] = $data[25];
$csv[$row]['smobile'] = $data[26];
$csv[$row]['passport'] = $data[27];
$csv[$row]['issue_date'] = $data[28];
$csv[$row]['expiry_date'] = $data[29];
$csv[$row]['poissue'] = $data[30];
$csv[$row]['emirates_no'] = $data[31];
$csv[$row]['emirates_exp'] = $data[32];
$csv[$row]['residence'] = $data[33];
$csv[$row]['nationality'] = $data[34];
$csv[$row]['type2'] = $_POST['type2'];
$min = $data[0];
$points = $data[1];
$points2 = $data[2];
$points3 = $data[3];
$points4 = $data[4];
$points5 = $data[5];
$points6 = $data[6];
$points7 = $data[7];
$points8 = $data[8];
$points9 = $data[9];
$points10 = $data[10];
$points11 = $data[11];
$points12 = $data[12];
$points13 = $data[13];
$points14 = $data[14];
$points15 = $data[15];
$points16 = $data[16];
$points17 = $data[17];
$points18 = $data[18];
$points19 = $data[19];
$points20 = $data[20];
$points21 = $data[21];
$points22 = $data[22];
$points23 = $data[23];
$points24 = $data[24];
$points25 = $data[25];
$points26 = $data[26];
$points27 = $data[27];
$points28 = $data[28];
$points29 = $data[29];
$points30 = $data[30];
$points31 = $data[31];
$points32 = $data[32];
$points33 = $data[33];
$points34 = $data[34];
$points35 = $_POST['type2'];
$json = "'$min', '$points', '$points2','$points3','$points4','$points5','$points6','$points7','$points8','$points9','$points10','$points11' ,'$points12','$points13' ,'$points14','$points15','$points16','$points17','$points18','$points19','$points20','$points21','$points22','$points23','$points24','$points25','$points26','$points27','$points28','$points29','$points30','$points31','$points32','$points33','$points34','$points35'";
fwrite($file,$json.PHP_EOL);
//sending the splitted CSV files, batch by batch...
if ($row % $batchsize == 0):
echo "<script> senddata('minpoints$row.csv'); </script>";
endif;
$row++;
}
fclose($file);
fclose($handle);
}
}
else
{
echo "Only CSV files are allowed.";
}
//alert once done.
echo "<script> alert('Data imported!');</script>";
}
Senddata.php
include('connect.php');
$csvMimes = array('text/x-comma-separated-values', 'text/comma-separated-values', 'application/octet-stream', 'application/vnd.ms-excel', 'application/x-csv', 'text/x-csv', 'text/csv', 'application/csv', 'application/excel', 'application/vnd.msexcel', 'text/plain');
$data = $_POST['file'];
$handle = fopen($data, 'r');
$test = file_get_contents($data , $csvMimes);
if ($handle) {
$counter = 0;
//instead of executing query one by one,
//let us prepare 1 SQL query that will insert all values from the batch
$sql ="INSERT INTO imported_property(pno, area, project, flatno, balcony, parking, common_area, floor, rooms, shop, flats, offices, category, total_area, plot_no, emirates, name, area_owned, address, phone, email, fax, pobox, gender, dob, mobile, smobile, passport, issue_date, expiry_date, poissue, emirates_no, emirates_exp, residence, nationality,type2) VALUES ";
while (($line = fgets($handle)) !== false) {
$sql .= "($line),";
$counter++;
}
$sql = substr($sql, 0, strlen($sql) - 1);
if ($conn->query($sql) === TRUE) {
} else {
}
fclose($handle);
} else {
}
//unlink CSV file once already imported to DB to clear directory
unlink($data);

CSV import only for certain amount then exceed data go into another table

I have a problem here. I don't know how to give an appropriate question title. So here is my problem.
I have a set of csv data. Let's say 30 data. The condition is I want only 20 data goes to Table A and another 10 data goes to Table B.
Do you have any example on how to work with this logic? Can it be done by combining while loop and if statement? By the way, it is a PHP and I'm still a newbie programmer. Hopefully someone can help me. Thank you.
Edited:
My current code
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE)
{
if($leftPax > 0)
{
$registrantBean = new z_EventRegistrants();
$registrantBean->last_name = $data[1];
$registrantBean->id_no = $data[0];
$registrantBean->phone_mobile = $data[2];
$registrantBean->email1 = $data[3];
$registrantBean->gender = $data[4];
$registrantBean->date_of_birth = $data[5];
$registrantBean->age = $data[6];
$registrantBean->race = $data[7];
$registrantBean->z_eventregec5erations_ida = $registrationBean->id;
$registrantBean->save();
$count++;
error_log(print_r($_REQUEST['session'], 1));
foreach($_REQUEST['session'] as $aid=>$sid)
{
$attendanceBean = new z_EventAttendances();
$attendanceBean->name = $registrantBean->last_name;
$attendanceBean->z_eventsessions_z_eventattendancesz_eventsessions_ida = $sid;
$attendanceBean->z_eventactivities_z_eventattendances_1z_eventactivities_ida = $aid;
$attendanceBean->z_eventregistrations_z_eventattendancesz_eventregistrations_ida = $registrationBean->id;
$attendanceBean->z_eventregistrants_z_eventattendancesz_eventregistrants_ida = $registrantBean->id;
$attendanceBean->save();
}
}
else
{
$waitlistBean = new z_EventWaitlists();
$waitlistBean->name = $data[1];
$waitlistBean->save();
}
}
What I want, the 20 data will go to z_EventRegistrants and the other 10 will go to z_EventWaitlist.
You need to asked some where in your code if the $count == 20
`
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE)
{
if($leftPax > 0 && $count <= 20)
{
$registrantBean = new z_EventRegistrants();
$registrantBean->last_name = $data[1];
$registrantBean->id_no = $data[0];
$registrantBean->phone_mobile = $data[2];
$registrantBean->email1 = $data[3];
$registrantBean->gender = $data[4];
$registrantBean->date_of_birth = $data[5];
$registrantBean->age = $data[6];
$registrantBean->race = $data[7];
$registrantBean->z_eventregec5erations_ida = $registrationBean->id;
$registrantBean->save();
$count++;
error_log(print_r($_REQUEST['session'], 1));
foreach($_REQUEST['session'] as $aid=>$sid)
{
$attendanceBean = new z_EventAttendances();
$attendanceBean->name = $registrantBean->last_name;
$attendanceBean->z_eventsessions_z_eventattendancesz_eventsessions_ida = $sid;
$attendanceBean->z_eventactivities_z_eventattendances_1z_eventactivities_ida = $aid;
$attendanceBean->z_eventregistrations_z_eventattendancesz_eventregistrations_ida = $registrationBean->id;
$attendanceBean->z_eventregistrants_z_eventattendancesz_eventregistrants_ida = $registrantBean->id;
$attendanceBean->save();
}
}
else
{
$waitlistBean = new z_EventWaitlists();
$waitlistBean->name = $data[1];
$waitlistBean->save();
}
}
}
`

Why is fgetcsv skipping the first row?

I'm using the php function fgetcsv() to read from a csv file. The structure of the csv file is such that, the very first row contains the data I want to import as opposed to the column title. I'd like to import from the very first row but I'm noticing that the first row is being ignored.
If I leave the first row empty, the same behavior persists. It is only when I add some dummy data in the first row's cells that the second row which contains the data I want to be imported actually gets imported and all other rows after that are imported as would be expected.
<?php
$handle = fopen($_FILES['materials']['tmp_name'], "r");
$data = fgetcsv($handle, 1000, ",");
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
if($this->checkMaterialsDuplicate($data[3]) != false) {
Session::setSession('import_failure', 'The Materials for TX '.
$this->_tx_no.' have already been added.');
Helper::redirect('?section=store&subsection=import_design&page=import_design');
}
echo "<pre>";print_r($data);echo "</pre>";exit;
$this->_fields[] = 'implicit_qty_of_fittings';
$this->_values[] = $this->db->escape($data[1]);
$this->_fields[] = 'qty_of_desired_materials';
$this->_values[] = $this->db->escape($data[2]);
$this->_fields[] = 'tx_no';
$this->_values[] = $this->db->escape($data[3]);
$this->_fields[] = 'contractor_name';
$this->_values[] = $this->db->escape($data[4]);
$this->_fields[] = 'contractor_id';
$this->_values[] = $this->db->escape($data[5]);
$this->_fields[] = 'issue_date';
$this->_values[] = $this->db->escape($data[6]);
$this->_fields[] = 'due_date';
$this->_values[] = $this->db->escape($data[7]);
$this->_fields[] = 'fitting_id';
$this->_values[] = $this->db->escape($data[8]);
$this->_fields[] = 'qty_multiplied_fittings';
$this->_values[] = $this->db->escape($data[9]);
$this->_fields[] = 'imported';
$this->_values[] = 1;
$sql = "INSERT INTO `{$this->_table_6}` (`";
$sql .= implode("`, `", $this->_fields);
$sql .= "`) VALUES ('";
$sql .= implode("', '", $this->_values);
$sql .= "')";
if (!$this->db->query($sql)) {
$error[] = $sql;
}
$this->_subcontractor_name = $data[4];
$this->_subcontractor_id = $data[5];
unset($this->_fields);
unset($this->_values);
}
Link to sample csv here
You've got an extra call to fgetcsv() before the loop begins - this will be fetching the first row and then discarding it, which means the loop will start on the second row.
Just remove this line and it should work correctly
$data = fgetcsv($handle, 1000, ",");

How to remove the last line of CSV file

<?php
$row = 0;
$handle = fopen("data20150804.log", "r");
while (!feof($handle) ) {
$line_of_text = fgetcsv($handle, 1024, ",");
$num = count($line_of_text);
$timeStamp = $line_of_text[0];
$visibility = $line_of_text[1];
$pressure = $line_of_text[2];
$rain = $line_of_text[3];
$temperature = $line_of_text[4];
$humidity = $line_of_text[5];
$dewpoint = $line_of_text[6];
$R1windSpeed = $line_of_text[7];
$R1wSpeed3s = $line_of_text[8];
$R1wSpeed2min = $line_of_text[9];
$R1wSpeed10min = $line_of_text[10];
$R1windDirDeg = $line_of_text[11];
$R1wDirDeg3s = $line_of_text[12];
$R1wDirDeg2min = $line_of_text[13];
$R1wDirDeg10min = $line_of_text[14];
$R2windSpeed = $line_of_text[15];
$R2wSpeed3s = $line_of_text[16];
$R2wSpeed2min = $line_of_text[17];
$R2wSpeed10min = $line_of_text[18];
$R2windDirDeg = $line_of_text[19];
$R2wDirDeg3s = $line_of_text[20];
$R2wDirDeg2min = $line_of_text[21];
$R2wDirDeg10min = $line_of_text[22];
$gust = $line_of_text[23];
//$row++;
}
fclose($handle);
?>
my csv file is keeping generate the data in a few second.
I want to remove the last row of the csv because my system is reading the last row data and my last row is blank.
If you check the documentation for fgetcsv you'll see the note about blank line returns. So if you do a check after:
$line_of_text = fgetcsv($handle, 1024, ",");
Along the lines of:
if (count($line_of_text) == 0 || $line_of_text[0] === null) {
continue;
}
This should do what you need.
Try this one
<?php
$row = 0;
$file = new SplFileObject("data20150804.log.csv");
$file->setFlags(SplFileObject::READ_CSV);
$file->setFlags(SplFileObject::SKIP_EMPTY);
foreach ($file as $line_of_text) {
$num = count($line_of_text);
$timeStamp = $line_of_text[0];
$visibility = $line_of_text[1];
$pressure = $line_of_text[2];
$rain = $line_of_text[3];
$temperature = $line_of_text[4];
$humidity = $line_of_text[5];
$dewpoint = $line_of_text[6];
$R1windSpeed = $line_of_text[7];
$R1wSpeed3s = $line_of_text[8];
$R1wSpeed2min = $line_of_text[9];
$R1wSpeed10min = $line_of_text[10];
$R1windDirDeg = $line_of_text[11];
$R1wDirDeg3s = $line_of_text[12];
$R1wDirDeg2min = $line_of_text[13];
$R1wDirDeg10min = $line_of_text[14];
$R2windSpeed = $line_of_text[15];
$R2wSpeed3s = $line_of_text[16];
$R2wSpeed2min = $line_of_text[17];
$R2wSpeed10min = $line_of_text[18];
$R2windDirDeg = $line_of_text[19];
$R2wDirDeg3s = $line_of_text[20];
$R2wDirDeg2min = $line_of_text[21];
$R2wDirDeg10min = $line_of_text[22];
$gust = $line_of_text[23];
}
?>
I hope it will help full for you.

Categories