Jump to content

get only first coulmn to concat with each second column rows from csv file using php


jackgoddy123

Recommended Posts

Hi all,

 

I am going through a very critical situation where I cannot find bright ideas to work upon.

1. I have an google spread sheet which contains few rows and columns.

2. I want to combine the row together including its column value.(1 row with four columns which results to a single sentence)

3. Simultaneously each row should form a single sentence from the google spread sheet.

 


 

I dont know how to strt of with this and which function, code to prefer ?

Any kind of help is appreaciated.

Hello all,

 

Below is the code which I have tried so far and returns the output which I wanted. smile.gif

<?php
if ($_FILES["file"]["error"] > 0)
{
   echo "Error: " . $_FILES["file"]["error"] . "<br>";

echo "No files...!!!!!";

echo "<script>alert('No files...!!!!!');window.location.href='upload_file.php';</script>";
}
else
{
$a=$_FILES["file"]["tmp_name"];
$csv_file = $a; 

if (($getfile = fopen($csv_file, "r")) !== FALSE) {
        $data = fgetcsv($getfile, 1000, ",");
  while (($data = fgetcsv($getfile, 1000, ",")) !== FALSE) {
    //$num = count($data);
  //echo $num;
       //for ($c=0; $c < $num; $c++) {
           $result = $data;
       	$str = implode(",", $result);
       	$slice = explode(",", $str);
       
           $col1 = $slice[0];
           $col2 = $slice[1];
           $col3 = $slice[2];
		   echo $col1."".$col2."".$col3."<br/>";

}
}

}

?>
But now the case is that the files are been uploaded from .csv file.
I want the fetch data from directly "google spread sheet" URL.
In the baove code what will be the logic to add the spread sheet URl ?
any help is appreciated.
 
Thankyou...!!

fgetcsv() returns the current rows data in an array for you, so there is then no need for any of this code

$result = $data;
$str = implode(",", $result);
$slice = explode(",", $str);

You'd concatenate $data[0] and $data[1] to join the first two columns together

if ($_FILES["file"]["error"] > 0)
{
   echo "Error: " . $_FILES["file"]["error"] . "<br>";
   echo "No files...!!!!!";
   echo "<script>alert('No files...!!!!!');window.location.href='upload_file.php';</script>";
}
else
{
    $csv_file = $_FILES["file"]["tmp_name"];

    if (($getfile = fopen($csv_file, "r")) !== FALSE)
    {
        $data = fgetcsv($getfile, 1000, ",");      // this will ignore the first row - which is usually the column headings
        while (($data = fgetcsv($getfile, 1000, ",")) !== FALSE)
        {
            $col_1_and_2 = $data[0] . $data[1];    // concatenate column 1 and 2

            echo $col_1_and_2 . '<br />';           // output the concatenated string
        }
    }
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.