I have this code that reads a csv file and displays it in this form :
REFERENCE;COLOR;QUANTITY;TURNOVER;SELL TROUGH;COMMENT GJK0C9;8952;3;90;3%;Pack S GJKCS4;399;2;19;10%;Windows GSIJS5;9224;18;128;12%;New co BBBOF1;90;17;116;13%;In sales ...
First the header and then all the lines.
I would like to display like this:
REFERENCE : GJK0C9 COLOR: 8952 QUANTITY: 3 TURNOVER : 90 SELL TROUGH: 3% HOW: Pack S REFERENCE : GJKCS4 COLOR: 399 ....
And so on.
How to display the result with this format?
<?php $row = 1; if (($handle = fopen($nomcsv, 'r')) !== FALSE) { echo '<table>'; // Get headers if (($data = fgetcsv($handle, 1000, ',')) !== FALSE) { echo '<tr><th>'.implode('</th><th>', $data).'</th></tr>'; } // Get the rest while (($data = fgetcsv($handle, 1000, ',')) !== FALSE) { echo '<tr><td>'.implode('</td><td>', $data).'</td></tr>'; } fclose($handle); echo '</table>'; } ?>
Advertisement
Answer
Your code will work with a bit modification:
1.Get headers first and assigne them to an array
2.Now loop over the values and combine them to header array to make them key value pair array
3.Loop over this key-value pair array and print it in desired format
4.Your delimiter is ;
not ,
(according to what you shown in code sample)
<?php $column = []; //create an array if (($handle = fopen($nomcsv, 'r')) !== FALSE) { // Get headers if (($data = fgetcsv($handle, 1000, ';')) !== FALSE) { $column = $data; // assign header value to array } // Get the rest while (($data = fgetcsv($handle, 1000, ';')) !== FALSE) { $row = array_combine($column,$data); // combine header with values foreach($row as $key=>$value){ echo $key." : ".$value; //print key value pair to get desired output echo PHP_EOL;//you can use '<br>' as well } } fclose($handle); } ?>