· 6 years ago · Sep 14, 2019, 08:56 PM
1<?php
2$array = $fields = array(); $i = 0;
3$city_counter = array();
4$handle = @fopen("test.csv", "r");
5if ($handle) {
6 while (($row = fgetcsv($handle, 4096)) !== false) {
7 if (empty($fields)) {
8 $fields = $row;
9 continue;
10 }
11 foreach ($row as $k=>$value) {
12 $array[$i][$fields[$k]] = $value;
13 if($fields[$k] == "city") {
14 if(isset($city_counter[$value])) {
15 $city_counter[$value] += 1;
16 }
17 else {
18 $city_counter[$value] = 1;
19 }
20 }
21 }
22 $i++;
23 }
24 if (!feof($handle)) {
25 print "Error: unexpected fgets() fail\n";
26 }
27 fclose($handle);
28}
29
30// sort array by lastName
31$last_name = array_column($array, 'last_name');
32array_multisort($last_name, SORT_ASC, $array);
33
34
35print "<table cellpadding='10' cellspacing'10' border='1'>";
36print "<tr> <th>First Name</th> <th>Last Name</th> <th>Phone Number</th> <th>Street</th> <th>City</th> </tr>";
37foreach($array as $arr) { // Clicking on a username, will open a new "person card" that shows his details. +
38 print "<tr> <td >$arr[first_name]</td> <td onclick='javascript: document.body.innerHTML=\"<h1>Person Card</h1>First Name: {$arr['first_name']}<br /> Last Name:{$arr['last_name']} <br />Phone Number: {$arr['phone']} <br />Street: {$arr['street']} <br />City: {$arr['city']}\";'><a>$arr[last_name]</td> </a><td>$arr[phone]</td> <td>$arr[street]</td> <td>$arr[city]</td> </tr>";
39}
40print "</table>";
41
42//The footer of the index file should have a summary, how many users you have from every city.
43print "<div id='user_info' align='center'></div>";
44
45foreach($city_counter as $city=>$total) {
46 print "{$city} = {$total}<br />\r\n";
47}
48// google image search requires API key thus i didn't use it
49?>