-
Notifications
You must be signed in to change notification settings - Fork 1
/
addWord.php
72 lines (56 loc) · 1.87 KB
/
addWord.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
<?php
header('Location:mapa_index.php');
$string = file_get_contents('words.txt');
//$latitude=40.19213;
//$longitude=-8.50894;
$palavras = json_decode($string);
var_dump($palavras);
echo "<br>"."<br>";
if (isset($_POST['textdata'])) {
$words_array = $palavras->features;
$word_input = $_POST['textdata'];
$latitude = round($_POST['latitude'], 4);
echo "LATITUDE". $latitude;
$longitude = round($_POST['longitude'],4);
$word_exists = false;
foreach ($palavras->features as $word) {
$words_coordinates = $word->geometry->coordinates;
$words_properties = $word->properties;
$rep = $words_properties->rep;
if( $word_input === $words_properties->palavra){
$word_exists = true;
$rep += 1;
echo "REPETIDO" . " - " . $words_properties->palavra . $rep . "<br/>";
$words_properties->rep = $rep;
break;
}
}
//-----------------ACABA FOREACH------------------
if($word_input != $words_properties->palavra && $word_input!=='') {
echo "NAO REPETIDO" . '<br>';
$new_word = array(
'type' => 'Feature',
'geometry' => array(
'type' => 'Point',
'coordinates' => array($latitude, $longitude)
),
'properties' => array(
'palavra' => $word_input,
'rep' => 1,
));
array_push($palavras->features, $new_word);
}
}
echo "<br>"."WORDS ARRAY"."<br>";
var_dump($words_array);
echo "<br>"."<br>";
echo "<br>"."PALAVRAS"."<br>";
var_dump($palavras);
echo "<br>"."<br>";
//array de volta p json string
$encodedString = json_encode( $palavras) . PHP_EOL;
echo "<br>"."ENCODED: " ."<br>". $encodedString;
//guardar ficheiro texto
if (file_put_contents('words.txt', $encodedString)) echo 'Data successfully saved';
else echo 'Unable to save data';
?>