MySQL and group_concat
I have a view called view_places_citatons and it hooks together places and the citations that mention them.
I wanted to be able to provide a list of citations and the places associated with them, like this:
Apollod. Epit..1.13 (Camicus, Icarion Sea)
The idea is that if the user clicks on Apollod. Epit..1.13 they will add all of the places listed in parentheses to the map. However, if they click on an individual place in the parentheses (like Camicus) they'll only add that place to the map.
The MySQL function to use is GROUP_CONCAT:
SELECT `citation`,
GROUP_CONCAT(DISTINCT `place_name` ORDER BY place_name ASC SEPARATOR ', ') AS place_name,
GROUP_CONCAT(DISTINCT CAST(`place_id` AS CHAR(20)) ORDER BY place_name ASC SEPARATOR ', ') AS place_id,
GROUP_CONCAT(DISTINCT CAST(`latitude` AS CHAR(20)) ORDER BY place_name ASC SEPARATOR ', ') AS latitude,
GROUP_CONCAT(DISTINCT CAST(`longitude` AS CHAR(20)) ORDER BY place_name ASC SEPARATOR ', ') AS longitude
FROM `view_places_citations`
WHERE `citation` LIKE '%epit%'
GROUP BY `citation`
ORDER BY `citation`
Int values need to be cast as strings (but make sure you tell MySQL how long your string is {e.g. AS CHAR(20)} or you'll get funny results), because you can't catenate ints. I use the AS to provide a hook in php - the field names seem to get lost in the catenation process. To order my output according to citation I need to include the GROUP BY.
Results from PHPMyAdmin look like this:
Apollod. Epit..1.13 | Camicus, Icarion Sea | 253, 252 | 37.36, 37.72 | 13.88, 26.09
Now, the idea is to take the output and create javascript arrays from it. Clicking the citation will sequentially add places in the array to the map.