-
Notifications
You must be signed in to change notification settings - Fork 0
/
backend.php
74 lines (61 loc) · 2.27 KB
/
backend.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
73
74
<?php
/**
* Simple php script which handles adding markers to the map and removing them
* Storing the stuff in a sqllite db
*/
/**
* CREATE TABLE markers (id integer PRIMARY KEY,name text NOT NULL,typ VARCHAR NOT NULL, description text , langX long NOT NULL, langY long NOT NULL);
*/
function connectDB() {
try {
$db = new PDO("sqlite:db/dayz.db");
return $db;
} catch(PDOException $e) {
die($e->getMessage());
}
}
function queryDB($db,$sql) {
$qh = $db->prepare($sql);
if(!$qh) {
die("query error ($sql)");
}
$qh->setFetchMode(PDO::FETCH_ASSOC);
$qh->execute();
return $qh;
}
$db = connectDB();
// add a marker
if(!empty($_POST['langX']) && !empty($_POST['langY']) && !empty($_POST['name']) && !empty($_POST['typ'])) {
$dbh = $db->prepare("INSERT INTO markers (name,typ,langx,langy,description) VALUES (:name, :typ, :langX, :langY, :description)");
$dbh->bindParam(':name', $_POST['name']);
$dbh->bindParam(':typ', $_POST['typ']);
$dbh->bindParam(':description', $_POST['description']);
$dbh->bindParam(':langX', $_POST['langX']);
$dbh->bindParam(':langY', $_POST['langY']);
$dbh->execute();
}
// edit marker
if(!empty($_POST['id']) && !empty($_POST['name']) && !empty($_POST['typ'])) {
$dbh = $db->prepare("UPDATE markers SET name=:name, typ=:typ, description=:description WHERE id=:id");
$dbh->bindParam(':id', $_POST['id']);
$dbh->bindParam(':name', $_POST['name']);
$dbh->bindParam(':typ', $_POST['typ']);
$dbh->bindParam(':description', $_POST['description']);
$dbh->execute();
}
// delete a marker
if($_GET['action'] == 'delMarker' && !empty($_GET['id'])) {
$dbh = $db->prepare("DELETE FROM markers where id = :id");
$dbh->bindParam(':id', $_GET['id'],PDO::PARAM_INT);
$dbh->execute();
}
// repos marker
if($_GET['action'] == 'reposMarker' && !empty($_GET['id']) && !empty($_GET['langX']) && !empty($_GET['langY'])) {
$dbh = $db->prepare("UPDATE markers SET langx=".$_GET['langX'].", langy=".$_GET['langY']." WHERE id=".$_GET['id']."");
$dbh->execute();
}
$markers = queryDB($db,"SELECT * FROM markers ORDER BY typ ASC,name DESC");
$typCats = queryDB($db,"SELECT DISTINCT(typ) FROM markers ORDER BY typ ASC");
$returnVal = array("types" => $typCats->fetchAll(), "markers" => $markers->fetchAll());
echo json_encode($returnVal);
?>