We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 31104
    • 108 Posts
    Hello, can anyone please point me in right direction as how to best accomplish the following:

    I work for a real estate company and we've chosen MODX for our property listings site. We have around 300 listings at the moment and growing.

    Each listing has all it's data saved as Template Variables (over 50 TVs per listing). (property_name, sign_number, lot_area, price, etc, etc.)

    We're now working on a kiosk-type map application that we will place on some large touchscreens for our sales room, using the google maps api. (Both the site and the map app are on the same server).

    Here's my big question: How could I access all the listings-data that we already have in MODX instead of creating a new alternate DB and adding each listing again one by one for the map app?

    The map app is being developed solely with html, js, and css (no php). Although I could probably write a php script that accesses the DB and exports the data as JSON for easy access with AJAX.

    I'm thinking maybe an array in js that relates all map marker IDs to our MODX resource IDs and then access MySQL extracting all TV info per ID? I'm not sure if I'm even taking the right approach or how to access the TV data, I've perused the DB looking for the data to no avail sad

    Any ideas, scripts, Extras or links pointing to helpful resources are truly appreciated in advance. Thank you all.

    This question has been answered by upiorz. See the first response.

    [ed. note: upiorz last edited this post 8 years, 7 months ago.]
    • discuss.answer
      • 31104
      • 108 Posts
      Ok, got it, turns out I was drowning in a cup of water, it was pretty simple =)

      In case this shows up in searches and someone else is looking for this, here goes my approach:

      If you have parent folders in your site structure you can get the children and their respective TVs

      //  getMany returns an array
      $children = $modx->resource->getMany('Children');
      
      // now loop through it
      foreach ($children as $child) {
      
          // make an array out of each child
          $new_item = array(
      			'name' => $child->get('pagetitle'), // use (-->get) for page properties
      			'alias' => $child->get('alias'),
      			'latlng' => $child->getTVValue('map_lat_lng') // use (-->getTVValue) for TVs
          );
      
          // finally push all arrays into a single array
          array_push($main_array, $new_item);
      }
      
      // now you can encode to json
      $encoded = json_encode($main_array);
      file_put_contents('path/to/your/file.json', $encoded);


      Cheers!
        • 52064
        • 120 Posts
        pay attention that if they are many it is better to use iterator.

        https://docs.modx.com/xpdo/2.x/class-reference/xpdo/xpdo.getiterator
          FerX - Developer at Eracom s.r.l.
          • 31104
          • 108 Posts
          Quote from: ferx77 at Mar 14, 2018, 04:10 AM
          pay attention that if they are many it is better to use iterator.

          https://docs.modx.com/xpdo/2.x/class-reference/xpdo/xpdo.getiterator

          Thank you ferx77, will definitely look into it =)