Click here to Skip to main content
15,887,676 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
My table is as follows:
Array ( [0] => Array ( [0] => Hotel [1] => 20 ) [1] => Array ( [0] => Hotel [1] => 210 ) [2] => Array ( [0] => Food [1] => 29.04 ) [3] => Array ( [0] => Fuel [1] => 186 ) [4] => Array ( [0] => Food [1] => 55 ) )


So where Hotel and Food are twice i want them to be added.My code is as follows:
PHP
<pre><!DOCTYPE html>
<html>
  
<body>
    <center>
        <h1>DISPLAY DATA PRESENT IN CSV</h1>
        <h3>CSV File</h3>
  
        <?php
        // include 'file.php';
        echo "<html><body><center><table>\n\n";
        // $fileName = $_POST['csvfile'];
        $fileName = "./test.csv";
        $summary = array();
        $i = 0;
        $sum;
        $index = null;

        $file = fopen($fileName, "r");

        while($data = fgetcsv($file)){
            if($index !== $data[2]){
                $summary[$i] = array($data[0], $data[1] * $data[2]);
                if($i > 0){
                    $summary[$i][0] = $data[0];
                }
                $index = $data[1];
                ++$i;
            echo "<tr>";
            }
        }
        //

        //

        fclose($file);
        print_r($summary);
        // print_r($res);

        // function csvToArray($csvFile){
 
        //     $file_to_read = fopen($csvFile, 'r');
         
        //     while (!feof($file_to_read) ) {
        //         $lines[] = fgetcsv($file_to_read, 1024);
         
        //     }
        //     fclose($file_to_read);
        //     return $lines;
        // }
         
        // $csvFile = 'test.csv';
        // $csv = csvToArray($csvFile);
        // echo '<pre>';
        // print_r($csv);
        // echo '
';

echo "\n";
?>







What I have tried:

I tried to loop it and also tried foreach but i cant get it done.
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900