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.

Link to comment
Share on other sites

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...!!
Link to comment
Share on other sites

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
        }
    }
}
Edited by Ch0cu3r
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.