Very novice student programmer here π I want to read part of a CSV file and insert into phpMyadmin using sql insert statement. I am a bit stuck… there is a column UN sub-region name and I only want each region to populate my database table once with its relevant regional score.
I have tried array_unique to print each region once however I also want to be able to get the corresponding score and be able insert both into a table in phpMyAdmin.
Here is my output: .
Any help is greatly appreciated π
<?php
include("../connection/conn.php");
$file = "Global_Index_Data.csv";
$regionName =[];
$regionScore =[];
if (file_exists($file)) {
$filepath = fopen($file, "r");
while (($line = fgetcsv($filepath)) != FALSE) {
$region = $line[5];
$score = $line[7];
$regionName[] = $region;
$regionScore[] = $score;
}
$resultName = array_unique($regionName);
echo sizeof($resultName)."<br>";
print_r($resultName);
print_r($regionScore);
//$regionNameScore = [$result, $line[7]];
//print_r($regionNameScore);
}
?>
2
Answers
@Jeroen van der Laan here is what I did. I wanted to print_r to check.
Consider using a single associative array to pair and sum up a region’s score across all rows provided by your CSV file:
Edit: mind the CSV column headers when processing the region scores this way.
Edit 2: I seem to have misunderstand your desired result. Instead of summing up the region scores (as single insert statements), you seem to want to group the individual regional score records by their region name (and insert them individually).
So instead of using an associative array to sum up a region’s score, we can use it to group all region scores by their respective region name: