annotate get_coordinates_for_listed_books.php @ 2:b85894a9b2dc

bug fixed: mysql query error due to updated coordinates_books table
author Zoe Hong <zhong@mpiwg-berlin.mpg.de>
date Tue, 24 Mar 2015 16:23:48 +0100
parents de2c442b6cbb
children 30b59e7b88c0
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
57bde4830927 first commit
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
diff changeset
1 <?php
1
de2c442b6cbb config to development server
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents: 0
diff changeset
2 include_once('config/Lib_mb_utf8.php');
de2c442b6cbb config to development server
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents: 0
diff changeset
3 include_once('config/config.php');
0
57bde4830927 first commit
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
diff changeset
4
57bde4830927 first commit
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
diff changeset
5 set_time_limit(0);
57bde4830927 first commit
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
diff changeset
6 ini_set('memory_limit', '-1');
57bde4830927 first commit
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
diff changeset
7
57bde4830927 first commit
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
diff changeset
8 $link_mysql = mysql_connect($mysql_server, $mysql_user, $mysql_password);
57bde4830927 first commit
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
diff changeset
9 mysql_query("SET NAMES utf8");
57bde4830927 first commit
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
diff changeset
10
57bde4830927 first commit
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
diff changeset
11 if (!$link_mysql) {
57bde4830927 first commit
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
diff changeset
12 die('Could not connect: ' . mysql_error());
57bde4830927 first commit
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
diff changeset
13 }
57bde4830927 first commit
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
diff changeset
14
57bde4830927 first commit
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
diff changeset
15 $db_selected = mysql_select_db($mysql_database, $link_mysql);
57bde4830927 first commit
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
diff changeset
16 if (!$db_selected) {
57bde4830927 first commit
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
diff changeset
17 die ('Can\'t use foo : ' . mysql_error());
57bde4830927 first commit
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
diff changeset
18 }
57bde4830927 first commit
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
diff changeset
19
57bde4830927 first commit
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
diff changeset
20 function readCsvFile($fileName){
1
de2c442b6cbb config to development server
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents: 0
diff changeset
21 global $server_host;
de2c442b6cbb config to development server
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents: 0
diff changeset
22 $fp=fopen($server_host."LGSearch/csv_files/".$fileName,"r");
0
57bde4830927 first commit
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
diff changeset
23 $data=fgetcsv($fp);
57bde4830927 first commit
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
diff changeset
24 $columnNameMappingArray=['BOOK_ID','LEVEL1','LEVEL2',
57bde4830927 first commit
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
diff changeset
25 'Name','PERIOD','TimeSpan:begin','TimeSpan:end','PAGE','SECTION','CONTENT',
57bde4830927 first commit
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
diff changeset
26 'Description']; //columns from input array/file
2
b85894a9b2dc bug fixed: mysql query error due to updated coordinates_books table
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents: 1
diff changeset
27 $columnNameArray=['Address','LEVEL1','LEVEL2',
b85894a9b2dc bug fixed: mysql query error due to updated coordinates_books table
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents: 1
diff changeset
28 'Name','PERIOD','TimeSpan:begin','TimeSpan:end','Longitude','Latitude',
b85894a9b2dc bug fixed: mysql query error due to updated coordinates_books table
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents: 1
diff changeset
29 'PAGE','SECTION','CONTENT',
b85894a9b2dc bug fixed: mysql query error due to updated coordinates_books table
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents: 1
diff changeset
30 'BOOK_ID','CHGIS_ID','1820_ID','1911_ID','CBDB_ID','Description']; //columns for output array/file (for map.php)
b85894a9b2dc bug fixed: mysql query error due to updated coordinates_books table
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents: 1
diff changeset
31 /*
0
57bde4830927 first commit
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
diff changeset
32 $columnNameArray=['Address','ADMIN_TYPE','LEVEL1','LEVEL2',
57bde4830927 first commit
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
diff changeset
33 'Name','PERIOD','TimeSpan:begin','TimeSpan:end','Longitude','Latitude',
57bde4830927 first commit
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
diff changeset
34 'PAGE','SECTION','CONTENT',
57bde4830927 first commit
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
diff changeset
35 'BOOK_ID','CHGIS_ID','1820_ID','1911_ID','CBDB_ID','Description']; //columns for output array/file (for map.php)
2
b85894a9b2dc bug fixed: mysql query error due to updated coordinates_books table
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents: 1
diff changeset
36 */
0
57bde4830927 first commit
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
diff changeset
37 $columnNameArray=array_flip($columnNameArray); //key, value swap in order to keep the order of the columns after merge
57bde4830927 first commit
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
diff changeset
38 $bookArray=array();
57bde4830927 first commit
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
diff changeset
39 $count=0;
57bde4830927 first commit
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
diff changeset
40 $line=0;
57bde4830927 first commit
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
diff changeset
41 while(!feof($fp)){
57bde4830927 first commit
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
diff changeset
42 $data=fgetcsv($fp);
57bde4830927 first commit
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
diff changeset
43 if(sizeof($data)<sizeof($columnNameMappingArray)){
57bde4830927 first commit
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
diff changeset
44 continue;
57bde4830927 first commit
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
diff changeset
45 }
57bde4830927 first commit
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
diff changeset
46 ///$bookId=$data[0];
57bde4830927 first commit
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
diff changeset
47 foreach($columnNameMappingArray as $idx=>$newName){ //use column names as the array indices (instead of 0,1,2,... as from $data)
57bde4830927 first commit
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
diff changeset
48 $bookInfo[$newName]=$data[$idx];
57bde4830927 first commit
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
diff changeset
49 }
57bde4830927 first commit
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
diff changeset
50 $bookId=$bookInfo['BOOK_ID'];
57bde4830927 first commit
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
diff changeset
51 $coordinateArray=getCoordinateFromDatabase($bookId);
57bde4830927 first commit
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
diff changeset
52 foreach($coordinateArray as $coordinate){ //merge a record from $booArray and ots cooresponding coordinateArray
57bde4830927 first commit
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
diff changeset
53 $bookArray[$count]=array_merge($bookInfo,$coordinate);
57bde4830927 first commit
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
diff changeset
54 $bookArray[$count]=array_merge($columnNameArray,$bookArray[$count]);
57bde4830927 first commit
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
diff changeset
55 $count++;
57bde4830927 first commit
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
diff changeset
56 }
57bde4830927 first commit
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
diff changeset
57 }
57bde4830927 first commit
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
diff changeset
58 fclose($fp);
57bde4830927 first commit
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
diff changeset
59 return $bookArray;
57bde4830927 first commit
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
diff changeset
60 }
57bde4830927 first commit
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
diff changeset
61 function writeCsvFile($fileName,$bookArray){
2
b85894a9b2dc bug fixed: mysql query error due to updated coordinates_books table
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents: 1
diff changeset
62 /*
0
57bde4830927 first commit
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
diff changeset
63 $columnNameArray=['Address','ADMIN_TYPE','LEVEL1','LEVEL2',
57bde4830927 first commit
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
diff changeset
64 'Name','PERIOD','TimeSpan:begin','TimeSpan:end','Longitude','Latitude',
57bde4830927 first commit
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
diff changeset
65 'PAGE','SECTION','CONTENT',
57bde4830927 first commit
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
diff changeset
66 'BOOK_ID','CHGIS_ID','1820_ID','1911_ID','CBDB_ID','Description'];
2
b85894a9b2dc bug fixed: mysql query error due to updated coordinates_books table
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents: 1
diff changeset
67 */
b85894a9b2dc bug fixed: mysql query error due to updated coordinates_books table
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents: 1
diff changeset
68 $columnNameArray=['Address','LEVEL1','LEVEL2',
b85894a9b2dc bug fixed: mysql query error due to updated coordinates_books table
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents: 1
diff changeset
69 'Name','PERIOD','TimeSpan:begin','TimeSpan:end','Longitude','Latitude',
b85894a9b2dc bug fixed: mysql query error due to updated coordinates_books table
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents: 1
diff changeset
70 'PAGE','SECTION','CONTENT',
b85894a9b2dc bug fixed: mysql query error due to updated coordinates_books table
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents: 1
diff changeset
71 'BOOK_ID','CHGIS_ID','1820_ID','1911_ID','CBDB_ID','Description'];
0
57bde4830927 first commit
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
diff changeset
72 $fp=fopen("./datasets/".$fileName,"w");
57bde4830927 first commit
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
diff changeset
73 fputcsv($fp,$columnNameArray);
57bde4830927 first commit
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
diff changeset
74
57bde4830927 first commit
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
diff changeset
75 foreach($bookArray as $book){
57bde4830927 first commit
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
diff changeset
76 fputcsv($fp,$book);
57bde4830927 first commit
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
diff changeset
77 }
57bde4830927 first commit
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
diff changeset
78 fclose($fp);
57bde4830927 first commit
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
diff changeset
79 }
57bde4830927 first commit
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
diff changeset
80 function getCoordinateFromDatabase($bookId){
2
b85894a9b2dc bug fixed: mysql query error due to updated coordinates_books table
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents: 1
diff changeset
81 /*
0
57bde4830927 first commit
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
diff changeset
82 $query="SELECT place_name AS Address, admin_type AS ADMIN_TYPE, x AS Longitude, y AS Latitude,
57bde4830927 first commit
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
diff changeset
83 chgis_id AS CHGIS_ID, 1820_id AS 1820_ID, 1911_id AS 1911_ID, cbdb_id AS CBDB_ID
57bde4830927 first commit
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
diff changeset
84 FROM coordinates_books
57bde4830927 first commit
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
diff changeset
85 WHERE books_id='".$bookId."'";
2
b85894a9b2dc bug fixed: mysql query error due to updated coordinates_books table
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents: 1
diff changeset
86 */
b85894a9b2dc bug fixed: mysql query error due to updated coordinates_books table
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents: 1
diff changeset
87 $query="SELECT place_name AS Address, x AS Longitude, y AS Latitude,
b85894a9b2dc bug fixed: mysql query error due to updated coordinates_books table
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents: 1
diff changeset
88 chgis_id AS CHGIS_ID, 1820_id AS 1820_ID, 1911_id AS 1911_ID, cbdb_id AS CBDB_ID
b85894a9b2dc bug fixed: mysql query error due to updated coordinates_books table
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents: 1
diff changeset
89 FROM coordinates_books
b85894a9b2dc bug fixed: mysql query error due to updated coordinates_books table
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents: 1
diff changeset
90 WHERE books_id='".$bookId."'";
0
57bde4830927 first commit
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
diff changeset
91 $result = mysql_query($query);
2
b85894a9b2dc bug fixed: mysql query error due to updated coordinates_books table
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents: 1
diff changeset
92 if (!$result) {
b85894a9b2dc bug fixed: mysql query error due to updated coordinates_books table
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents: 1
diff changeset
93 echo mysql_error();
b85894a9b2dc bug fixed: mysql query error due to updated coordinates_books table
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents: 1
diff changeset
94 return;
b85894a9b2dc bug fixed: mysql query error due to updated coordinates_books table
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents: 1
diff changeset
95 }
0
57bde4830927 first commit
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
diff changeset
96 $coordinateArray=array();
57bde4830927 first commit
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
diff changeset
97 while ($row = mysql_fetch_assoc($result)) {
57bde4830927 first commit
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
diff changeset
98 $coordinateArray[]=$row;
57bde4830927 first commit
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
diff changeset
99 }
57bde4830927 first commit
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
diff changeset
100 return $coordinateArray;
57bde4830927 first commit
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
diff changeset
101 }
57bde4830927 first commit
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
diff changeset
102
57bde4830927 first commit
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
diff changeset
103 $fileName=$_GET['file'];
57bde4830927 first commit
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
diff changeset
104 $name=$_GET['name'];
57bde4830927 first commit
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
diff changeset
105 $bookArray=readCsvFile($fileName);
57bde4830927 first commit
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
diff changeset
106 writeCsvFile($fileName,$bookArray);
57bde4830927 first commit
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
diff changeset
107 header("Location: map.php?mode=1&file=".$fileName."&name=".$name);
57bde4830927 first commit
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
diff changeset
108 ?>