We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 21783
    • 5 Posts
    A script to find the nearest store or what ever, to you, by inputting your address or zip code and a radius in miles.
    Please note: I borrowed a lot of this from a tutorial, then took a lot from this forum. I kept working until it ran, but my code is not the best.
    1. Set up a table, then make a way for “brokers” to add themselves to the table.
    (This requires one to access the table, verify the data, use a geocoder to find the coordinates, and insert them into the record). The nice thing is it will not output blank coordinates, so my intervention is required for all new additions. Oh, and you must get your own google API for your site.
    2. Set up a map that would access the table and plot markers and a side bar with info to the nearest point based on the radius and address or zip they input. I also must tell you that the map window covers my menu on the side of the template I will work on this later.
    Mysql 5.0.45, modx 9.5

    The table created inside modx db
    CREATE TABLE `markers` (
      `id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
      `name` VARCHAR( 60 ) NOT NULL ,
      `address` VARCHAR( 80 ) NOT NULL ,
      `citystatezip` VARCHAR( 80 ) NOT NULL ,
      `phone` VARCHAR( 12 ) NOT NULL ,
      `type` VARCHAR( 80 ) NOT NULL ,
      `lat` FLOAT( 10, 6 ) NOT NULL ,
      `lng` FLOAT( 10, 6 ) NOT NULL ,
      `visable` VARCHAR( 2 ) NOT NULL ,
      ‘date’ DATETIME
    )  ;
    


    The fields are pretty straight forward, ‘visable’ was going to be used to not print the record unless I changed it to ‘Y’ but later I found out that blank coordinates will not return a record.

    The Form Chunk titled {{AddBroker}}

    <p class="error">[+validationmessage+]</p>
    Here is where you add your name and address to our map.<br>
    Enter your full name, then your street address, then (on one line) your city, state zip.<br>
    However you input your phone number is how it will display- so please use an area code.<br>
    Your name will not show up until I code your longitude and latitude based on your street address 
    so please be patient.<br>
    <form method="post" action="[~[*id*]~]" id="AddBroker" name="AddBroker">
    		<table id="Add-a-Broker" cellpadding="0" cellspacing="0" border="0">
    		
    		<td style="text-align:right">
                      <label for="sname">Full Name:</label></td>
    		  <td><input name="name" id="sname" class="text" type="text" eform="Full Name::1:"/> *</td>
    		</tr>
    		<tr>
    			<td style="text-align:right"><label for="saddress">Street Address:</label></td>
    			<td><input name="address" id="saddress" class="text" type="text" eform="Street Address::1:"/> *</td>
    		</tr>
    		<tr>
    			<td style="text-align:right"><label for="scitystatezip">City, State zip:</label></td>
    			<td><input name="citystatezip" id="scitystatezip" class="text" type="text" eform="City, State zip::1:"/> *</td>
    		</tr>
    		<tr>
    			<td style="text-align:right"><label for="sphone">Phone:</label></td>
    			<td><input name="phone" id="sphone" class="text" type="text" eform="Phone::1:"/> *</td>
    		</tr>
    		<tr>
    			<td style="text-align:right"><label for="stype">Type:</label></td>
    			<td>
                             <select name="type" id="stype" eform="Type::1:">
    			<option value="" selected="selected">Select Type</option>
    				  <option value="Broker">Broker</option>
    				  <option value="Lender">Lender</option>
    				  <option value="other">other</option>
    			  </select> *			
    			</td>
    		</tr>
    
    		<tr>
    			<td colspan="2" style="text-align:center"><input type="submit" name="submit" value="Submit"></td>
    		</tr>
    	</table>
    </form>
    


    The Snippet [[insertbroker]]

    <?php
    function escape($s){return mysql_escape_string($s);
    } 
    function insertbroker( &$fields )
    	{		
    		// Bring needed resources into scope
    		global $modx;
    		// Init our array
    		$dbTable = array(); // key = DB Column; Value = Insert/Update value
    		// Insert field/value pairs to insert/update in our table
    		$dbTable['ID'] = 'NULL';
    		$dbTable['name'] = $modx->db->escape($fields['name']);
    		$dbTable['address'] = $modx->db->escape($fields['address']);
    		$dbTable['citystatezip'] = $modx->db->escape($fields['citystatezip']);
    		$dbTable['phone'] = $modx->db->escape($fields['phone']);
    		$dbTable['type'] = $fields['type'];
    		$dbTable['lat'] = '';
    		$dbTable['lng'] = '';
                    $dbTable['visable'] = 'N';
    		$dbTable['date'] = date( 'YmdHis', strtotime( $fields[postdate] ) ); 
    		
    			
    
    	               // print_r($dbTable);
    	
    		// Run the db insert query
    	 $dbQuery = $modx->db->insert( $dbTable, 'markers');	
    	
           return true;
    	}
    ?>



    The code to call the above was put on a page, name it what ever.

    [!insertbroker!] 
     [!eForm? &noemail=`true` &formid=`AddBroker` &eFormOnBeforeMailSent=`insertbroker` &tpl=`AddBroker` &thankyou=`46`!] 


    my thank you page on the menu has the (46) after it, put the number of your thank you page in there.
    I now know what the s and the cf are used for in the Form, thanks, and I think the ::1 tells eform that the field is mandatory.

    The Map page- put your googlemap API after the key=
    The API is free from Google

    The Chunk {{GoogleMap}}

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
      <head>
        <meta http-equiv="content-type" content="text/html; charset=utf-8"/>
        <title>Find a Broker Near You</title>
        <script src="http://maps.google.com/maps?file=api&v=2&key=ABQIAAAA7xs7vzRuaoYKnoxqRadLdBQX-cOEUPOBREUpYXvpOUtYHDbrHBSl9m13vrGrCn_replace all this with your API"
                type="text/javascript"></script>
        <script type="text/javascript">
        //<![CDATA[
        var map;
        var geocoder;
    <!-- change the center of the map, and the zoom level below -->
        function load() {
          if (GBrowserIsCompatible()) {
            geocoder = new GClientGeocoder();
            map = new GMap2(document.getElementById('map'));
            map.addControl(new GSmallMapControl());
            map.addControl(new GMapTypeControl());
            map.setCenter(new GLatLng(41.2, -73.8), 6);
          }
        }
    
       function searchLocations() {
         var address = document.getElementById('addressInput').value;
         geocoder.getLatLng(address, function(latlng) {
           if (!latlng) {
             alert(address + ' not found');
           } else {
             searchLocationsNear(latlng);
           }
         });
       }
    
       function searchLocationsNear(center) {
         var radius = document.getElementById('radiusSelect').value;
         var searchUrl = 'locator.php?lat=' + center.lat() + '&lng=' + center.lng() + '&radius=' + radius;
         GDownloadUrl(searchUrl, function(data) {
           var xml = GXml.parse(data);
           var markers = xml.documentElement.getElementsByTagName('marker');
           map.clearOverlays();
    
           var sidebar = document.getElementById('sidebar');
           sidebar.innerHTML = ''; <!-- markers is the name of the database -->
           if (markers.length == 0) {
             sidebar.innerHTML = 'No results found.';
             map.setCenter(new GLatLng(41.2, -73.8), 6);<!-- Change map center, and zoom level -->
             return;
           }
    
    var bounds = new GLatLngBounds();
           for (var i = 0; i < markers.length; i++) {
             var name = markers[i].getAttribute('name');
             var address = markers[i].getAttribute('address');
             var distance = parseFloat(markers[i].getAttribute('distance'));
             var point = new GLatLng(parseFloat(markers[i].getAttribute('lat')),
                                     parseFloat(markers[i].getAttribute('lng')));
             var type = markers[i].getAttribute('type');         
             var marker = createMarker(point, name, address, type); <!-- leave point alone, put fields here -->
             map.addOverlay(marker);
             var sidebarEntry = createSidebarEntry(marker, name, address, distance, type); <!-- keep distance next to address, then add other fields here -->
             sidebar.appendChild(sidebarEntry);
             bounds.extend(point);
           }
           map.setCenter(bounds.getCenter(), map.getBoundsZoomLevel(bounds));
         });
       }
    
        function createMarker(point, name, address, type) {
          var marker = new GMarker(point);
          var html = '<b>' + name + '</b> <br/>' + address + '<br/>' + type; <!-- quotes around tags, plus sign to make string longer, don't forget the semicolon -->
          GEvent.addListener(marker, 'click', function() {
            marker.openInfoWindowHtml(html);
          });
          return marker;
        }
    <!-- all names must match database exactly, keep address next to distance add new fields to end -->
        function createSidebarEntry(marker, name, address, distance, type) {
          var div = document.createElement('div');
          var html = '<b>' + name + '</b> (' + distance.toFixed(1) + ')<br/>' + address + '<br/>' + type;
          div.innerHTML = html;
          div.style.cursor = 'pointer';
          div.style.marginBottom = '5px'; 
          GEvent.addDomListener(div, 'click', function() {
            GEvent.trigger(marker, 'click');
          });
          GEvent.addDomListener(div, 'mouseover', function() {
            div.style.backgroundColor = '#eee';
          });
          GEvent.addDomListener(div, 'mouseout', function() {
            div.style.backgroundColor = '#fff';
          });
          return div;
        }
        //]]>
    
      </script>
      </head>
    
      <body onload="load()" onunload="GUnload()">
    Find a Broker near you- test page, Downstate NY only<br/>
    Simply put your address or Zipcode in the box, and choose a radius<br/>
        Your Address, or Zip: <input type="text" id="addressInput"/>
         
        Radius: <select id="radiusSelect">
    
          <option value="25" selected>25</option>
          <option value="50">50</option>
          <option value="100">100</option>
          <option value-"150">150</option>
          <option value="200">200</option>
        </select>
    
        <input type="button" onclick="searchLocations()" value="Search Locations"/>
        <br/>    
        <br/>
    <div style="width:600px; font-family:Arial, 
    sans-serif; font-size:11px; border:1px solid black">
      <table> 
        <tbody> 
          <tr id="cm_mapTR">
    
            <td width="200" valign="top"> <div id="sidebar" style="overflow: auto; height: 400px; font-size: 11px; color: #000"></div>
    
            </td>
            <td> <div id="map" style="overflow: hidden; width:400px; height:400px"></div> </td>
    
          </tr> 
        </tbody>
      </table>
    </div>    
    
      </body>
    </html>
    
    The snippet called [[GoogleFunction]]
    
    <?php
    // Get parameters from URL
    $center_lat = $_GET["lat"];
    $center_lng = $_GET["lng"];
    $radius = $_GET["radius"];
    
    // Start XML file, create parent node
    $dom = new DOMDocument("1.0");
    $node = $dom->createElement("markers");
    $parnode = $dom->appendChild($node);
    
    // Opens a connection to the database and gets the fields.
    // and does some math that throws a error on ‘AS distance’ when you run while manager is open
    
    $modx->db->select('address', 'name', 'type', 'lat', 'lng', ( 3959 * acos( cos( radians('%s') ) * cos( radians( lat ) ) * cos( radians( lng ) - radians('%s') ) + sin( radians('%s') ) * sin( radians( lat ) ) ) ) AS distance FROM markers HAVING distance < '%s' ORDER BY distance LIMIT 0 , 20",
      mysql_real_escape_string($center_lat),
      mysql_real_escape_string($center_lng),
      mysql_real_escape_string($center_lat),
      mysql_real_escape_string($radius));
    $result = mysql_query($query);
    
    $result = mysql_query($query);
    if (!$result) {
      die("Invalid query: " . mysql_error());
    }
    
    header("Content-type: text/xml");
    
    // Iterate through the rows, adding XML nodes for each
    while ($row = @mysql_fetch_assoc($result)){
      $node = $dom->createElement("marker");
      $newnode = $parnode->appendChild($node);
      $newnode->setAttribute("name", $row['name']);
      $newnode->setAttribute("address", $row['address']);
      $newnode->setAttribute("type", $row['type']);
      $newnode->setAttribute("lat", $row['lat']);
      $newnode->setAttribute("lng", $row['lng']);
      $newnode->setAttribute("distance", $row['distance']);
    }
    
    echo $dom->saveXML();
    ?>
    


    The page that displays the map and the search box has the following code. continued on next post
      • 21783
      • 5 Posts
      [!GoogleFunction!] 
       [!eForm? &noemail=`true` &formid=`GoogleMap` &eFormOnBeforeMailSent=`GoogleFunction` &tpl=`GoogleMap`!] 


      I just noticed that I have yet to display the phone number on the map page.
      -on my to do list.
      Please remember that I really don’t know a lot about PHP or code, everything I know I learned from the fourm, so there are probably better ways to do some things.
      Also- you have to go to a geocoder page like batchgeocode dot com put in the address, then paste the latitude and longitude into the table using your php admin, then they will show – if they are within the radius. To center your map, put in the zip code of the center of your area in geocode, then use the first 3 digits include the decimal (play with the zoom level to suit your needs)

      Leave out the Add page, put in your store locations using php admin and you have your own store locator. After all this, I hope it works for you.
        • 4749
        • 623 Posts
        Very cool. Thanks for sharing your work. I’m sure if there are any "holes" in your code they’ll be found and filled very quickly.
          The MODx has you...
          Utah Web Design
          • 10076
          • 1,024 Posts
          Hope someone here can help me out. I seem yet to be lacking time to study PHP.

          Could someone provide me with a few script examples, cause I can not figure it out with the above examples.
          And I have been trying.

          Suppose I have a table named "cars". And I want users to be able to add "car_names" and car_photos". Also I would like them to be able to modify that entry and to delete it. How would I do that? Last but not leats I want those people to see their entrie after they hit "submit". Thanks.
            • 10076
            • 1,024 Posts
            Okay so I ve managed to insert something into the DB. probably will manage to select and delete as well. Currently I amstrugling to translate this bit:

            $idpubdetails = $_GET['idpd']; //id used on the show details page
            $idpubmod     = $_GET['idpmod']; //id used on the modify details page
            $pubchar      = $_GET['pubchar']; //id of first letter of publisher name
            
            //idpub is the primary index of that table
            
            if (strlen($pubchar) > 0)
            {
               $select_subset = true;
               $letter = strtoupper(substr($pubchar,0,1));
               if (is_numeric($letter))
                  $first_is_number = true;
               else $first_is_number = false;
            }
            else $select_subset = false;
            include('connections.php');
            //$result = mysql_query($sql) or die('ERROR: '.$query.' '.mysql_error());
            $letterlinks = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
            echo '<a name="top"></a>';
            echo '<a href="index.php?id=3&pubchar=0">0-9</a> ';
            for ($i = 0; $i < 26; $i++):
            echo '<a href="index.php?id=3&pubchar='.$letterlinks[$i].'">'.$letterlinks[$i].'</a> ';
            endfor;
            
            
            if ($select_subset)
            {
            
            // 001: Database query
            
              if ($first_is_number)
                 $query = "select * from publisher WHERE abbreviation  >= '0%' and abbreviation  <= '9%'";
              else $query = "select * from publisher WHERE abbreviation  like '".$pubchar."%'";
              $query .= " order by abbreviation  ASC";
            
            // 002: Execute query


            A friend of mine created this code and I am slowly trying to use that with the modx dbapi. But it looks as if the code needs to be written differently. Correct? I don t need this code to be re written but a few tips would be welcome.
            Especcially for the alphabetic list this script creates and how I should to use that GET function ($idpubmod = $_GET[’idpmod’]. Or how to get variables to move from one script to the other.

            cheers.
              • 10313
              • 375 Posts
              Quote from: mww at Apr 29, 2008, 04:32 AM

              Call from the page:
              
              [[eForm2db]] 
              
              [!eForm? &formid=`ContactForm` &to=`[email protected]` eFormOnBeforeMailSent=`eForm2db` &tpl=`ContactForm` report=`ContactFormReport`  &thankyou=`ContactFormThanks` &subject=`Web site feedback` &cssStyle=`assets/templates/mystyle.css` !] 
              
              


              Well, you are missing 2 &-signs in your snippet call. It must be [tt]&eFormOnBeforeMailSent[/tt] and [tt]&report[/tt].

              I just read through the last pages of this thread, and I think I don’t exactly know what your problem is. By the way: it IS possible to have eForm send an email and store the date in the database at the same time. I’m doing so.

              Martin
                • 10313
                • 375 Posts
                Now, as my own table in the DB is being filles with form data, I wonder how get the recordsets out of the DBMS into, say Excel or CSV or whatever.

                Is there already a snippet that can export data of tables (not just documents)?

                Martin
                  • 4172
                  • 5,888 Posts
                  wouldn’t be so difficult to modify fast_content_csv for that (imports/exports document-datas from/to csv in the backend) or to create a snippet to do the same in the frontend for your db-datas.

                  Bruno
                    -------------------------------

                    you can buy me a beer, if you like MIGX

                    http://webcmsolutions.de/migx.html

                    Thanks!
                    • 10313
                    • 375 Posts
                    I also think it won’t be too difficult. But I wanted to avoid to code things that already exist smiley

                    Thanks
                      • 18367
                      • 834 Posts
                      Hopefully this is an easy one.

                      Where’s the validation message and how do I change it.

                      I mean the one that gets called by this call

                      [+validationmessage+]

                      Thanks
                        Content Creator and Copywriter