ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

Help with mySQL query

February 10, 2008 10:58pm

Subscribe [3]
  • #1 / Feb 10, 2008 10:58pm

    David J

    17 posts

    Hi, the below code and SQL query are the contents of an XML template, listing a location marker for each member of our site , which then gets plotted on a member map.

    For some reason this code acts very strange. a) Sometimes the sql file will output kinda properly, but only list about 60 or so members (when our site has 300+) and it lists these 60 in a weird way, eg. jumping from member id 36 to 57 etc. b) A lot of times it will only output the top <markers> tag and nothing else. c) Most ofthen though, it outputs just the first 6 or 7 users and does not inlclude the closing </markers> tag.

    Any help would be much appreciated! This file is driving me crazy.

    <markers>
    <?php
    
    // My Google Maps API key where X's are
    $key = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
    
    global $DB;
    
    $query = "SELECT m.member_id, m.screen_name, m.photo_filename, c.m_field_id_3 AS postcode FROM exp_members m LEFT JOIN exp_member_data c ON m.member_id=c.member_id";
    
    $result = mysql_query($query) or die(mysql_error());
    
    while($row = mysql_fetch_array($result))
    
        {
    
        $postcode = $row['postcode'];
        $postcode = urlencode("$postcode");
        $postcode = substr($postcode,0,(strlen($postcode)-1));
        
        // Desired address
        $url = "http://maps.google.com/maps/geo?q=$postcode&output=xml&key;=$key";
    
        // Retrieve the URL contents
        $page = file_get_contents($url);
    
        // Parse the returned XML file
        $xml = new SimpleXMLElement($page);
        
        // Check for error
        list($code) = explode(",", $xml->Response->Status->code);
        
        if ($code == "200")
            {
            // Parse the coordinate string
            list($longitude, $latitude, $altitude) = explode(",", $xml->Response->Placemark->Point->coordinates);
             
            // Output the coordinates
            echo "<marker ";
            echo "lat=\"$latitude\" lng=\"$longitude\" name=\"";
            echo $row['screen_name'];
            echo "\" photo=\"";
            echo $row['photo_filename'];
            echo "\" memid=\"";
            echo $row['member_id'];
            echo "\" >";
            }
    
        }
    
    ?>
    </markers>
  • #2 / Feb 11, 2008 10:26am

    Robin Sowell

    13255 posts

    I’m going to shift this over to ‘How to’ as it’s more about custom code than EE as far as I can tell.  Are you running this in a standalone php file?  Or is it in an EE template and possibly running into conflicts because of that?

    W/out knowing the outside classes, it’s hard for me to take a guess at what’s going on.  If I wanted to pinpoint if it’s borking in EE, I’d reduce it down to just the query to get the members- echo those out w/out pulling the the xml at all.  See if that’s problematic- which as far as I can tell, it shouldn’t be.

    Make sense as a starting point?

  • #3 / Feb 11, 2008 2:35pm

    David J

    17 posts

    Thanks Robin, i paired things down and ran some tests. I figured out that it doesn’t seem to be an EE problem or a problem with the query itself. I can get it to output every member if i remove the bits where it parses the returned simpleXMLElement.

    Not sure why it’s only returning the first 6 members, i’ll keep testing, i wonder if it might be some sort of limitation on our server, perhaps it can’t send out more than 6 http requests at a time?

  • #4 / Feb 11, 2008 4:34pm

    offsprg01

    78 posts

    you probably need to place a pause between the http requests. not sure how’d you do that but i think that would solve your problem.

  • #5 / Feb 11, 2008 6:02pm

    David J

    17 posts

    offsprg01, I think you may be right. I’ve searched around for some more info and found info (not sure how accuarate) that states “the Google Maps API has a per IP request limit of 1 request per approximately 2 seconds (so batch additions will have to have a built-in pause)”.

    Based on the results i’m receiving i think this approximation is probably closer to 6 or 7 requests. I’m still not a hundred percent sure this is the reason the code is failing though, because i can’t figure out why even it craps out after 6 or 7 requests why its not putting in my closing </markers> tag.

    Can any php gurus give me a hand on how i would go about putting a pause in place between the requests?

  • #6 / Feb 11, 2008 11:36pm

    offsprg01

    78 posts

    don’t think you can do it in php as php is run server side. you need something client side like javascript to make the pause. not sure exactly how that would work though as thats above my head.

    basically you’d pull the entries from your db using php and your msql querry, but then you’d need to store them for the javascript to send out to google 1 at a time. and that would take a long time to load if you have 300 entries with a 1 second or better yet 1.1 second pause between each one.

  • #7 / Feb 13, 2008 2:52am

    David J

    17 posts

    Thanks offsprg, unfortunately that kind of coding is also beyond me. My goal with the above code was to just get the XML file that was spit out and set EE’s cach on it for 24 hrs.

    I wonder if there’s an easier way, if somehow i could get each members postal code (which they currently enter when registering) and have that geocoded some other time and the lat and long stored in the members table? Has anyone else out there had success using EE and a member location map?

  • #8 / Feb 13, 2008 11:30am

    offsprg01

    78 posts

    why don’t you just have each user’s geocode returned from google when the register and store it for later use?

  • #9 / Feb 13, 2008 4:16pm

    David J

    17 posts

    You make it sounds so easy:). The database stuff is not my specialty, but i’ll try and think of how to maybe do just that.

  • #10 / Feb 13, 2008 5:05pm

    offsprg01

    78 posts

    ah but the consept is always easy, the execution is always the hard part. all you need to do is instead of echo our the coords, store them in a field in your data base.

    you’ve already got the code to return the coords as you are echoing them out. well instead of doing that just send their happy little butts back the the database and put them in your users table or whatever the table is you’re storing the post code in.

    try writting a script that uses the poscode entered on registration to do exactly what you have now but instead of echoing it out, store it in the database at the same time you are putting the rest of the users info in the database.

    then all you need to do is call the geocode from your database when every you need instead of trying to get it from google. you website will run a hell of alot faster too.

  • #11 / Feb 13, 2008 5:29pm

    Bruce2005

    536 posts

    Definitely save to database. Very much faster.
    make custom member fields, lat and lng.
    There will be in exp_member_data
    probably m_field_id_1 and 2 if none there now, add and check.

    echo "lat=\"$latitude\" lng=\"$longitude\" name=\"";
            echo $row['screen_name'];
            echo "\" photo=\"";
            echo $row['photo_filename'];
            echo "\" memid=\"";
            echo $row['member_id'];

    So you have the lat, lng there already, so a query to update all rows on member_id using a time delay can do all at once 😉

    Here is an example of geocoding with time delay:

    Geocode multi

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases