I am having one csv file with three columns and Name , month and expanses. The file name is : name.csv. The file format is like this.
NAME MONTH EXPANSES
JHON JAN 200
GATES JAN 150
JHON MAR 150
BILL APP 100
Whereas I am having one php file: name.php which will fetch the required data from the csv file and the sours code is:
<?php
$x="JHON"; // to be checked in the first column
$handle = fopen("name.csv","r")or die("file dont exist");
$output = '';
while (!feof($handle )){
$data = fgetcsv($handle,4096,",");
if(($data[0] ==$x)){
$output .= sprintf( "Month: %s Expanses: %d)<br>",
$data[1], $data[2]);
}
}
echo $output;
fclose($handle);
?>
I want to sort out where the name is “JHON”, with the above code and the result is as following.
Month: JAN Expanses: 200
Month: MAR Expanses: 150
Month: AUG Expanses: 250
But what I want is heading like: Expanses of JHON and at bottom, total expenses: amount
MONTH WISE EXPANSES OF JHON
Month: JAN Expanses: 200
Month: MAR Expanses: 150
Month: AUG Expanses: 250
TOTAL EXPANCES: 600
Can anybody help me in correcting the php code please?