PHP and calculating two values from separate .csv files

Hi (i have attached all files relevant to my question),

I have a slight problem with a project I am creating for school. The project has asked to first of all display values from a .csv file using php which I have done…

The next second task however actually asks to multiply a value from the first csv file with a number from a second csv file.

The CSV files look like the following

The calculation would be 11.97(from metals.csv) * 1.57 (from rates.csv) and thus creating an exchange rate.

=============================================
[size=14pt]metals.csv[/size]
[table]
[tr]
[td]Type of Metal[/td]
[td]Price Per Gram (GBP)[/td]
[/tr]
[tr]
[td]9 Carat Gold[/td]
[td]11.97[/td]
[/tr]
[tr]
[td]Silver[/td]
[td]0.49[/td]
[/tr]
[/table]

=============================================

[size=14pt]rates.csv[/size]
[table]
[tr]
[td]USD[/td]
[td]EUR[/td]
[/tr]
[tr]
[td]1.57[/td]
[td]1.23[/td]
[/tr]
[/table]

=============================================

The results of the calculation would then need to be displayed in the table.

Here is my code so far, which basically just displays information from the metals.csv file but I have no idea how to conduct a calculation between two csv files??? someone please help:

[php]<?php
$cnx = fopen(“metals.csv”, “r”); //open example.csv
echo(“”); // echo the table
while (!feof ($cnx)) { // while not end of file
$buffer = fgets($cnx); // get contents of file (name) as variable
$values = explode(",", $buffer); //explode “,” between the values within the contents
echo “”;
for ( $j = 0; $j < count($values); $j++ ) { //
echo("");
}
echo"";
};
echo("

Type of Metal Price Per Gram (GBP) Price Per Gram (USD) Price Per Gram (EUR)
$values[$j]
");
fclose($cnx); //close filename variable
?>[/php]

calculation.zip (921 Bytes)

You have different layout in the two csv files

metals.csv is
type, value
type, value

while rates.csv is
type, type
value, value

I’d suggest changing rates.csv to

GBP,1.02 USD,1.57 EUR,1.23

[hr]

Then you can use this code
[php]<?php

function readCSV($filename) {
$row = 1;
$return = array();
if (($handle = fopen($filename, “r”)) !== FALSE) {
while (($data = fgetcsv($handle, 1000, “,”)) !== FALSE) {
$return[] = $data;
$row++;
}
fclose($handle);
}
return $return;
}

$metals = readCSV(‘metals.csv’);
$rates = readCSV(‘rates.csv’);
?>

Untitled Document <?php foreach ($metals as $metal) { ?> <?php foreach ($rates as $rate) { ?> <?php } ?> <?php } ?> [/php]

Output

Type of Metal Price Per Gram (GBP) Price Per Gram (USD) Price Per Gram (EUR) 9 carat gold 12.2094 18.7929 14.7231 18 carat gold 24.2046 37.2561 29.1879 Silver 0.4998 0.7693 0.6027 Platinum 28.0704 43.2064 33.8496

Type of Metal Price Per Gram (GBP) Price Per Gram (USD) Price Per Gram (EUR)
<?= $metal[0] ?><?= $metal[1] * $rate[1] ?>

Hi,

Thank you so much for this. I am most grateful.

I have also tried to apply formatting to the numbers as they need to be to 2 decimal places.

How do you think I might apply the number_format((float)$rates, 2, ‘.’, ‘’); to the numbers that are being output?

Just add it to where it outputs the values

[php]

<?= $metal[1] * $rate[1] ?>[/php]–>
[php] <?= number_format(($metal[1] * $rate[1], 2, '.', '') ?>[/php]
Sponsor our Newsletter | Privacy Policy | Terms of Service