Jump to content

Output data in XML file


combicart
 Share

Recommended Posts

Hello,

I'm currently trying to implementing a store locator on my website (https://github.com/bjorn2404/jQuery-Store-Locator-Plugin). To render the stores on the map the stores are being loaded from an XML file. To geocode the locations, i've installed the Maps Marker from Ryan (http://modules.processwire.com/modules/fieldtype-map-marker/).

So far so good, however now I'm stuck at generating the actual XML file. Especially in printing the individual stores in a foreach loop. For the XML file, i've take some inspiration from the sitemap.xml file

The stores all have a template called 'location'. Can I just load those locations with $pages->find("template=location")? The code that I have so far:

<?php

function renderLocation(Page $page) {
  return
    "\n<marker " .
    "name=" . $page->title .
    "/>";
}

function renderLocationsXML(array $locations = array()) {
  $out = '<?xml version="1.0" encoding="utf-8"?>';
  $out .= '\n<markers>';

  // Foreach loop?

  $out .= '\n</markers>';
  return $out;
}

header("Content-Type: text/xml");
echo renderLocationXML;

The XML file has to be in the following format

<?xml version="1.0" encoding="utf-8"?>
<markers>
	<marker name="Chipotle Minneapolis" lat="44.947464" lng="-93.320826" category="Restaurant" address="3040 Excelsior Blvd" address2="" city="Minneapolis" state="MN" postal="55416" country="US" phone="612-922-6662" email="info@chipotle.com" web="http://www.chipotle.com" hours1="Mon-Sun 11am-10pm" hours2="" hours3="" featured="" features="" />
	<marker name="Chipotle St. Louis Park" lat="44.930810" lng="-93.347877" category="Restaurant" address="5480 Excelsior Blvd." address2="" city="St. Louis Park" state="MN" postal="55416" country="US" phone="952-922-1970" email="info@chipotle.com" web="http://www.chipotle.com" hours1="Mon-Sun 11am-10pm" hours2="" hours3="" featured="" features="Online Ordering " />
</markers>

Could someone point in the right direction in how to create the function / foreach loop to generate the marker data / locations inside the XML file?

Link to comment
Share on other sites

For a Map Marker field named 'map'...

function renderLocationsXML($locations) {
    $out = '<?xml version="1.0" encoding="utf-8"?>';
    $out .= '\n<markers>';
    
    foreach ($locations as $location) {
        // use any fields of $location
        $out .= "<marker name='{$location->title}' lat='{$location->map->lat}' lng='{$location->map->lng}' />";
    }

    $out .= '\n</markers>';
    return $out;
}

$locations = $pages->find("template=location");

header("Content-Type: text/xml");
echo renderLocationXML($locations);

 

  • Like 2
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...