2

I want to get the value of column header in getcsv but I can't.

Example inputs:

Name , Address , Age
John , California, 26
Michael, L.A , 29

If I am in row2 column2, I want to get the name of header which is Address. But my code returns as L.A.

This is my code:

   <?php
    echo '<table class="tbl">'; 
    while(($data = fgetcsv($handle, 1000, ",")) !== false)
    {  
        $col    = count($data);  
        $header = $data[0]; //I AM SUPPOSED to get this value but this returns to the current cell and not the column header.
        echo '<tr>';
        for ($c=0; $c < $col; $c++) { 
            $cell   = esc_html( $data[$c] );
            $colnum = $c + 1;
            if( $row == 0)
            {    
                echo "<td  style='text-transform:uppercase;'><br><br>". 
                        "<b>{$cell}</b></td>"; 
            }
            else
            { 
                echo "<td>{$cell} = {$header}</td>";  //{$row} = {$colnum} 
            } 
        }
        echo '</tr>'; 
        $row++; 
    }  
    echo '</table>';

Based on the inputs and code above, here is the output:

Name , Address , Age
John = John , California = California , 26 = 26
Michael= Michael , L.A = L.A , 29 = 29

I am supposed to get the header value of each column as shown below:

Name , Address , Age
John = Name , California = Address , 26 = Age
Michael= Name , L.A = Address , 29 = Age

cosmoonot
  • 2,161
  • 3
  • 32
  • 38
smzapp
  • 809
  • 1
  • 12
  • 33

1 Answers1

1

There might be a better answer, but the code below will work.

Your CSV data is being returned in the manner below:

Array ( [0] => Name [1] => Address [2] => Age )  
Array ( [0] => John [1] => California [2] => 26 ) 
Array ( [0] => Michael [1] => L.A [2] => 29 )

Updated Answer

<?php
     $headerValues  = array();
     $counter = 0;
     $row = 0;
     if (($handle = fopen("test.csv", "r")) !== FALSE) {
        echo '<table class="tbl">'; 
        while(($data = fgetcsv($handle, 1000, ",")) !== false)
        {  
            // You need to grab the header values on first iteration
            if ($counter == 0) {
              // store them in an array
              $headerValues = $data;
              // increment counter
              $counter++;                  
            }                

            $col    = count($data); 
            echo '<tr>';
            for ($c=0; $c < $col; $c++) {
                // grab column name here
                $headerName = $headerValues[$c]; 
                $cell   = $data[$c];
                $colnum = $c + 1;
                if( $row == 0)
                {                    
                    echo "<td  style='text-transform:uppercase;'><br><br>". 
                            "<b>{$headerName}</b></td>"; 
                }
                else
                { 
                    echo "<td>{$cell} = {$headerName}</td>";  //{$row} = {$colnum} 
                } 
            }
            echo '</tr>'; 
            $row++; 

        }  

        echo '</table>';
    }
    ?>  
cosmoonot
  • 2,161
  • 3
  • 32
  • 38