I modified to fit with Codeigniter were the data access and XML generation. And in both of these cases it was more about reworking the code structure to fit into the MVC format that Codeigniter utilizes. First we will look at the data access.
I created a model called MapData to hold my database query.
3 | class MapData extends CI_Model { |
4 | function __construct() { |
6 | $this ->load->database(); |
9 | function getmappoints() { |
10 | $sql = "SELECT * FROM markers WHERE 1" ; |
11 | $query = $this ->db->query( $sql ); |
12 | if ( $query ->num_rows() > 0) { |
Yes I know that I do not thoroughly test if there was a failure with the database connection, left it out for brevity. But as you can see it is just a simple database look up that returns the results object. Now on to the controller.
The controller is all the other XML creation occurs. To generate the XML I created a function calledsendXML that will generate the XML and echo it to the page when requested.
1 | public function sendXML() { |
2 | $dom = new DOMDocument( "1.0" ); |
3 | $node = $dom ->createElement( "markers" ); |
4 | $parnode = $dom ->appendChild( $node ); |
5 | $this ->load->model( 'MapData' ); |
6 | $mapdata = new MapData(); |
8 | foreach ( $mapdata ->getmappoints()->result() as $row ) { |
9 | $node = $dom ->createElement( "marker" ); |
10 | $newnode = $parnode ->appendChild( $node ); |
11 | $newnode ->setAttribute( "name" , $row ->name); |
12 | $newnode ->setAttribute( "address" , $row ->address); |
13 | $newnode ->setAttribute( "lat" , $row ->lat); |
14 | $newnode ->setAttribute( "lng" , $row ->lng); |
15 | $newnode ->setAttribute( "type" , $row ->type); |
17 | header( "Content-type: text/xml" ); |
Once again this code is very straight forward and it almost exactly as you will find in the tutorial code. Now to render the map I simply created a view and pasted all the tutorial code went into the view verbatim, with the exception of the of the URL for the XML. Then I called the view form the index function in the controller like so.
1 | public function index() { |
2 | $this ->load->view( 'googlemap_mapview.php' ); |
The XML is passed via the Codeigniter controller instead directly from a php file. And so my entry for URL in the javascript was a Codeigniter styled URL as such.
2 | downloadUrl( "/codeigniter_test/index.php/googlemaptest/sendXML/" , function (data) { |
And that wraps it up. I hope this was useful and interesting and I will post more about the project that I am working on later.
PHP PDO tutorial - programming databases in PHP PDO
ReplyDeleteConvert Text to Image in PHP
PHP SimpleXML - Get Node/Attribute Values
PHP Form Validation
SQL Injection Prevention
Characteristics Of A Good Programming Languages
Submit form without page reloading
Split database results into 3 equal columns - PHP