php excel to mysql problem - php

i am trying to build a csv to mysql and i did everything right,
butthe first row of the titles keep entering too, i want it to start from the second row in the csv file,
can you please help me:
$row = 1;
if (($handle = fopen($file, "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$num = count($data);
$userid = mysql_real_escape_string($Sid);
$amount = mysql_real_escape_string($data[0]);
$cardHolderName = mysql_real_escape_string($data[1]);
$cardHolderAddress = mysql_real_escape_string($data[2]);
$cardHolderZipcode = mysql_real_escape_string($data[3]);
$cardHolderCity = mysql_real_escape_string($data[4]);
$cardHolderState = mysql_real_escape_string($data[5]);
$cardHolderCountryCode = mysql_real_escape_string($data[6]);
$cardHolderPhone = mysql_real_escape_string($data[7]);
$cardHolderEmail = mysql_real_escape_string($data[8]);
$cardNumber = mysql_real_escape_string($data[9]);
$cardSecurityCode = mysql_real_escape_string($data[10]);
$cardExpireMonth = mysql_real_escape_string($data[11]);
$cardExpireYear = mysql_real_escape_string($data[12]);
$PurchaseDate = mysql_real_escape_string($data[13]);
$StoreId = mysql_real_escape_string($data[14]);
$ItemCode = mysql_real_escape_string($data[15]);
$ItemName = mysql_real_escape_string($data[16]);
$ItemQuantity = mysql_real_escape_string($data[17]);
$itemShippingType = mysql_real_escape_string($data[18]);

You need to get the first row of data (title row) and "throw it away". Your code example is starting processing without skipping the first row.
if (($handle = fopen($file, "r")) !== FALSE) {
$data = fgetcsv($handle, 1000, ",");
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
...

I'm not sure what you're doing with your $row variable, but you could do something like this:
$row = 1;
if (($handle = fopen($file, "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
if($row == 1)
{
$row++;
continue;
}
...

Related

How to concatenate two columns in a csv file in php?

I have a csv file like this:
I would like to concatenate the values of the style_color columns in this csv file. To have for example SCJEG4_1014.
I wrote a script, it creates this last column with the header 'Pictures Names' but in each cell I just have "_".
How can I solve my problem?
<?php
//uploaded xlsx file recovery
$xlsx="C:/wamp64/www/Extract_pictures_Excel/xlsx_files/".date('Y_m_d H-i-s')."_file.xlsx";
move_uploaded_file($_FILES["mon_fichier"]["tmp_name"],$xlsx);
// Excel in CSV
require_once 'PHPExcel/Classes/PHPExcel/IOFactory.php';
$excel = PHPExcel_IOFactory::load($xlsx);
$writer = PHPExcel_IOFactory::createWriter($excel, 'CSV');
$writer->setDelimiter(";");
$writer->setEnclosure("");
$nomcsv = "C:/wamp64/www/Extract_pictures_Excel/csv/".date('Ymd_His').".csv";
$writer->save($nomcsv);
$delimiter = ";";
$csv_data = array();
$row = 1;
if (($handle = fopen($nomcsv, 'r')) !== FALSE) {
while (($data = fgetcsv($handle, 1000, $delimiter)) !== FALSE) {
$names_pictures = $data[7].'_'.$data[4];
$csv_data[] = $data;
$row++;
}
fclose($handle);
}
$extra_columns = array('Pictures Names' => $names_pictures);
foreach ($csv_data as $i => $data) {
if ($i == 0) {
$csv_data[$i] = array_merge($data, array_keys($extra_columns));
} else {
$csv_data[$i] = $data = array_merge($data, $extra_columns);
}
}
if (($handle = fopen($nomcsv, 'w')) !== FALSE) {
foreach ($csv_data as $data) {
fputcsv($handle, $data, $delimiter);
}
fclose($handle);
}
?>
It looks like you only add in the details from the last row ( as you only use the value of $names_pictures once). It would be better (IMHO) to add this value into the data at the point at which you generate the $csv_data array...
while (($data = fgetcsv($handle, 1000, $delimiter)) !== FALSE) {
$data['Pictures Names'] = $data[7] . '_' . $data[4];
$csv_data[] = $data;
$row++;
}
You could then remove the foreach ($csv_data as $i => $data) { loop
If you had a different file for the output you could open the output file before the above loop and write the data directly to the output file rather than using $csv_data...
if (($handle = fopen($nomcsv, 'r')) !== FALSE
&& ($ohandle = fopen($nomcsvOutput, 'w')) !== FALSE) {
while (($data = fgetcsv($handle, 1000, $delimiter)) !== FALSE) {
$data['Pictures Names'] = $data[7] . '_' . $data[4];
fputcsv($ohandle, $data, $delimiter);
}
fclose($handle);
fclose($ohandle);
}

Show CSV rows in reverse order with PHP

Trying to get this code to echo the rows it gets from the CSV file in reverse order. Any idea how to do this?
<?php
$row = 1;
$FILE = "file.csv";
if (($handle = fopen($FILE, "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$num = count($data);
for ($c=0; $c < $num; $c++) {
if ($c == 0) $first = $data[$c];
if ($c == 1) $second = $data[$c];
}
}
fclose($handle);
}
?>
Create a specific function for this task and call the function. It is always easier to split your program into smaller pieces. You need to read the data into an array and the reverse the order.
<?php
function loadCSV($file) {
$rows = array();
if (($handle = fopen($file, "r")) !== FALSE) {
while (($data = fgetcsv($handle, 0, ",")) !== FALSE) {
array_push($rows, $data);
}
fclose($handle);
}
return array_reverse($rows);
}
?>
Here is how to use this function:
<?php
$data = loadCSV('data.csv');
print_r($data);
?>

php read csv data by column

I'm trying to recieve data from a .csv. Here the code I have so far:
if(($handle = fopen("test.csv", "r")) !== FALSE){
while(($data = fgetcsv($handle, 1000, ",")) !== FALSE){
}
}
But this puts all the data I need in the first entry in the $data array. What I need to do is $data[1] and then get all the values from the second column. I've done it before but I can't it to work this time. How do I do it?
EDIT
Here is my .csv file structure
| ID | Name |
|------|----------|etc
| 1 |Product 1 |
Here's a screenshot of the file http://imgur.com/BlaCvjo
And with echo $data[1] it should return 'Product 1'
Please be specific to your question. Anyway, these codes will help you to your question.
<?php
$csv = array();
// check there are no errors
if($_FILES['csv']['error'] == 0){
$name = $_FILES['csv']['name'];
$ext = strtolower(end(explode('.', $_FILES['csv']['name'])));
$type = $_FILES['csv']['type'];
$tmpName = $_FILES['csv']['tmp_name'];
// check the file is a csv
if($ext === 'csv'){
if(($handle = fopen($tmpName, 'r')) !== FALSE) {
// necessary if a large csv file
set_time_limit(0);
$row = 0;
while(($data = fgetcsv($handle, 1000, ',')) !== FALSE) {
// number of fields in the csv
$col_count = count($data);
// get the values from the csv
$col1 = $data[0];
$col2 = $data[1];
$col3 = $data[2];
$col4 = $data[2];
// inc the row
$row++;
}
fclose($handle);
}
}
}
?>

How to find the last iteration of a "fgetcsv" while() loop in PHP

How to find the last iteration:
Here is an example while loop. I need to find the last iteration so that a comma will not be added to the end. How is this done?
for($i=0;$i<count($files);$i++){
if (($handle = fopen($files[$i], "r")) !== FALSE) {
$isfirst = true;
while (($data = fgetcsv($handle, 1000, ',')) !== FALSE) {
if ($isfirst){
$isfirst = false;
continue;
}
$timestamp = strtotime($data[0].' '.$data[1]);
fputs($out, '['.(int)$timestamp.','.(float)$data[2].'],');
}
fclose($handle);
}
}
Answer:
for($i=0;$i<count($files);$i++){
if (($handle = fopen($files[$i], "r")) !== FALSE) {
$isfirst = true;
$firstline = true;
while (($data = fgetcsv($handle, 1000, ',')) !== FALSE) {
if ($isfirst){
$isfirst = false;
continue;
}
if ($firstline) {
$timestamp = strtotime($data[0].' '.$data[1]);
fputs($out, '['.(int)$timestamp.','.(float)$data[2].']');
$firstline = false;
} else {
$timestamp = strtotime($data[0].' '.$data[1]);
fputs($out, ',['.(int)$timestamp.','.(float)$data[2].']');
}
}
fclose($handle);
}
}
for($i=0;$i<count($files);$i++){
if (($handle = fopen($files[$i], "r")) !== FALSE) {
$firstLine = TRUE;
while (($data = fgetcsv($handle, 1000, ',')) !== FALSE) {
if (!$firstLine) {
fputs($out, ',');
} else {
firstLine = FALSE;
}
$timestamp = strtotime($data[0].' '.$data[1]);
fputs($out, '['.(int)$timestamp.','.(float)$data[2].']');
}
fclose($handle);
}
}

How do I skip the first piece of data when extracting from a csv file with PHP

The following code is what I am using to get an entire column of data from my csv files. (There are many columns, but I only need #2). This code will put all of the data into the array called $cbsmin. What I need to do is skip the very first piece of data in each file as it is the category name not actually a piece of data.
How would this best be done?
for($i=0;$i<count($files);$i++){
if (($handle = fopen($files[$i], "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$cbsmin[] = $data[2];
}
fclose($handle);
}
}
Just use a simple flag to mark if it is the first line. If it is, reset the flag to false and skip that line.
$isfirst = true;
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
if ($isfirst)
{
$isfirst = false;
continue;
}
$cbsmin[] = $data[2];
}
Just execute a single
fgetcsv($handle, 1000, ",");
before the while loop.
Just throw in an extra read prior to going into the while loop
for($i=0;$i<count($files);$i++){
if (($handle = fopen($files[$i], "r")) !== FALSE) {
// eat the header
fgetcsv($handle, 1000, ",");
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$cbsmin[] = $data[2];
}
fclose($handle);
}
}
Another technique I use for this is to use the header row for keys in the data. This works if the header rows are consistent over time and from file to file.
for($i=0;$i<count($files);$i++){
if (($handle = fopen($files[$i], "r")) !== FALSE) {
// eat the header
$header = fgetcsv($handle, 1000, ",");
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$data = array_combine($header, $data);
$cbsmin[] = $data["Customer Name"];
}
fclose($handle);
}
}
It beats having to use field offsets if you are doing anything non-trivial with a lot of the fields.

Categories