1

What I am trying to do is Upload a CSV file with Php. The first line is the Column names and below that the data (of course). Each column name can change depends on the end user uploads. So the main column names we need can change spots (A1 or B1 etc...) So lets say the column I need is B1 and I need to get all the data in B. Not sure on how to go by it. So far this is what I have. Any ideas?

        ini_set("allow_url_fopen", 1);

        $handle = fopen($_FILES['fileToUpload']['tmp_name'], 'r') or die ('cannot open the file');

        while(!feof($handle)) {
            $data[] = fgetcsv($handle);
        }
        var_dump($data);

        fclose($handle);

UPDATE: I am importing this file from .CSV to PHP Screenshot

I need to search for column header that starts with “SKU” and then “COST” From there once those are found then I want the whole column… B, E. But those column letters can change, depends on how it is being exported by the end user. I do not need the rows, just columns.

mrmcg
  • 173
  • 6
  • 17
  • _“Then when I get the array [1] I just need to get the rest of the column[1]'s data”_ – so, loop through the array, and for each “line” access the element with that index …? Not clear what your actual problem is here. – CBroe Sep 14 '16 at 15:15
  • I updated on what I am looking for in my main question. Thank you. – mrmcg Sep 14 '16 at 15:36
  • You can drop the first line of your code -- you don't need `allow_url_fopen` in order to `fopen()` something from `$_FILES`. – Simba Sep 14 '16 at 15:46
  • Search for the index of the column name in the header line first (go check array functions in the manual, if you don't know how.) After that, use those indexes to get the data out of the corresponding columns. If you need values from multiple columns, then it might make sense to put those indexes in an array first, that you then can loop over each time. – CBroe Sep 14 '16 at 16:06

2 Answers2

1

Once the file is uploaded into the server, use something like the following code to parse it and actually use it as an array[];

Code:

$filename = "upload/sample.csv";
if (($handle = fopen($filename, 'r')) !== FALSE){
    while (($row = fgetcsv($handle, 1000, ",")) !== FALSE){
   print_r($row);
 }
}

That's one way of doing it, you could also read more about it here.

FluxCoder
  • 1,266
  • 11
  • 22
  • This is what I am doing... $key = array_search('column1', $row); ..Now giving me this error. Warning: array_search() expects parameter 2 to be array, boolean given – mrmcg Sep 14 '16 at 14:09
  • Please read my answer on here: http://stackoverflow.com/a/39477226/6749661 – FluxCoder Sep 14 '16 at 14:47
  • The array out put that I am getting is this sample: array(1652) { [0]=> array(14) { [0]=> string(5) "title" [1]=> string(3) "sku" [2]=> string(6) "vendor" [3]=> string(10) "cost_price" .... $key = array_search('sku',$data); Then when I get the array [1] I just need to get the rest of the column[1]'s data. – mrmcg Sep 14 '16 at 15:13
  • I'm sorry, but you'll need to either contact me in private or post another question as I'm having a hard time understanding what you wnat. – FluxCoder Sep 14 '16 at 15:15
  • I updated on what I am looking for in my main question. Thank you. – mrmcg Sep 14 '16 at 15:36
0

If you want the value of a specific column for each row then you need to loop through the results and pick it out. It looks like you are getting an array of arrays so...(EDITED to get the column based on the header name):

$header = $data[0];
unset($data[0]); // delete the header row so those values don't show in results
$sku_index = '';
$cost_index = '';

// get the index of the desired columns by name
for($i=0; $i < count($header); $i++) {
     if($header[$i] == 'SKU') {
         $sku_index = $i;
     }
     if($header[$i] == 'COST') {
         $cost_index = $i;
     }
}

// loop through each row and grab the values for the desired columns
foreach($data as $row) {
    echo $row[$sku_index];
    echo $row[$cost_index];
}

Should get what you want.

lps
  • 1,403
  • 16
  • 28