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);
}
}
}
?>
Related
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);
}
I have a code where a csv file is uploaded ,the data is extracted from it and uploaded to database.Everything works fine,but how can i skip the empty rows and continue reading the rows with data in it.
This is the code where i extract data from csv file
if (($source = fopen( $csv_file, "r")) !== FALSE)
{
//read data from excel
while (($data = fgetcsv($source, 1000, ",")) !== FALSE)
{
$question=$data[0];
$point=$data[1];
$header=$data[2];
$footer=$data[3];
$type_value=$data[4];
$group_name=$data[5];
echo $question;
}// while end
}
If you use PHP's SplFileObject instead of the basic fgetcsv() function, it has built-in options to skip empty lines:
$file = new SplFileObject($csv_file);
$file->setFlags(SplFileObject::READ_CSV SplFileObject::SKIP_EMPTY | SplFileObject::DROP_NEW_LINE);
foreach ($file as $data) {
...
}
if ($data[0] == NULL)
continue;
This is because fgetcsv returns a non-empty array with a null element inside.
Try it with
if ($data === null) continue;
I didn't test it, but I sthink it should work.
Try this
if (($source = fopen( $csv_file, "r")) !== FALSE)
{
// read data from excel
while (($data = fgetcsv($source, 1000, ",")) !== FALSE)
{
if ((string) $data[0] != '0' and empty($data[0]))
{
continue;
}
$question = $data[0];
$point = $data[1];
$header = $data[2];
$footer = $data[3];
$type_value = $data[4];
$group_name = $data[5];
echo $question;
}
}
I have a CSV file loaded from an URL, and I want to loop over the lines with PHP.
Here is a typical line of this CSV:
1004000018;active;"TEST1";"TEST2";"TEST3";"TEST4"
I would like to get this result, for each row:
1004000018
active
TEST1
TEST2
TEST3
TEST4
You can achieve this using the php function fgetcsv, this should work :
PHP
$file = fopen('file.csv', 'r');
while (($line = fgetcsv($file)) !== FALSE) {
//$line[0] = '1004000018' in first iteration
print_r($line);
}
fclose($file);
This will help you for read csv:
if (($handle = fopen("$source_file", "r")) !== FALSE) {
$columns = fgetcsv($handle, $max_line_length, $delemietr);
if (!$columns) {
$error['message'] = 'Empty';
return ($error);
}
while (($rows = fgetcsv($handle, 10000, "\t")) !== false) {
if ($rows[1] && array(null) !== $rows) { // ignore blank lines
$data1 = $rows[1];
}
}
}
I have a csv file with id of the image and the link of the image.
I want to make a php script that call the id and open the suitable image of the id. Now I am using this code to show the csv file.
$row = 1;
if (($handle = fopen("file.csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
print_r($data);
}
fclose($handle);
}
My csv file got only two columns
with ID and Image Link
Any idea how to do it?
This is what you need:
<?php
$row = 0;
if (($handle = fopen("file.csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, "\t")) !== FALSE) {
if ($row > 0) {
if ($data[0] == '1234') echo $data[1];
}
$row++;
}
fclose($handle);
}
$data[0] will be your check for ID, $data[1] will be your image link. I have added the row check to exclude the first row as sounds like you have your first row as the heading.
Replace:
'ID_HERE'
With:
$_POST['id'];
//or
$_GET['id'];
// or however you get your id you want to check
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;
}
...