$candidate) { $score = 0; if ($candidate['Pledge'] === "y") $score++; if ($candidate['q1'] === "Yes") $score++; if ($candidate['q3'] === "Yes") $score++; if ($candidate['q4'] === "Yes") $score++; if ($candidate['q7'] === "Yes") $score++; $candidate_data[$key]['Score'] = $score; } $header = ["Ward", "Candidate Name", "Rating", "Pledge", "Picture"]; /* Generate candidates-generic.csv */ foreach ($lga_list as $lga) { $lga_candidates = array_filter($candidate_data, function ($candidate) use ($lga) { return $candidate['match_lga'] === $lga['slug']; }); if (count($lga_candidates) === 0) continue; $dir = dirname($lga['config-file']); $dir_files = scandir($dir); $output_file = $dir."/candidates-generic.csv"; if (($handle = fopen($output_file, "w")) === FALSE) { error_log('Error opening output file'); exit(1); } if (fputcsv($handle, $header) === FALSE) { error_log('Error writing headers to output file'); exit(3); } foreach ($lga_candidates as $candidate) { /* Add extension to photo hash */ if (strlen($candidate['Photo'])) { foreach ($dir_files as $file) { if (preg_match("/\.json$/", $file)) continue; if (strstr($file, $candidate['Photo'])) { $candidate['Photo'] = $file; } } } $fields = [ $candidate['match_ward'], $candidate['Name'], $candidate['Score'], $candidate['Pledge'], $candidate['Photo'], ]; if (fputcsv($handle, $fields) === FALSE) { error_log('Error writing candidate to output file'); exit(3); } } } exit(0);