Previous Next

Thread: convert PHP TO ASP

Last post 07-31-2008 11:55 AM by havanajoe26. 6 replies.

Average Rating Rate It (5)

RSS

Page 1 of 1 (7 items)

Sort Posts:

  • 07-24-2008, 5:49 PM

    • havanajoe26
    • Not Ranked
    • Joined on 07-24-2008, 5:21 PM
    • Pasadena, CA
    • Posts 4

    convert PHP TO ASP

     

    Hello,

     I am working on a project but I am only able to find PHP versions of the code. can someone help me convert this to classic ASP (vbscript)Thanks in advance!!

    <?php 
    require("phpsqlsearch_dbinfo.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 a mySQL server
    $connection=mysql_connect (localhost, $username, $password);
    if (!$connection) {
      die("Not connected : " . mysql_error());
    }

    // Set the active mySQL database
    $db_selected = mysql_select_db($database, $connection);
    if (!$db_selected) {
      die ("Can\'t use db : " . mysql_error());
    }

    // Search the rows in the markers table
    $query = sprintf("SELECT address, name, 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);

    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("lat", $row['lat']);
      $newnode->setAttribute("lng", $row['lng']);
      $newnode->setAttribute("distance", $row['distance']);
    }

    echo $dom->saveXML();
    ?>
  • 07-25-2008, 10:39 AM In reply to

    • havanajoe26
    • Not Ranked
    • Joined on 07-24-2008, 5:21 PM
    • Pasadena, CA
    • Posts 4

    Re: convert PHP TO ASP

    Thanks for your help with the link. The above code is actually Google's PHP way to implement a store locator using google maps API.

    I searched their forum's but nobody seems to use ASP. I was able to create the page below. I have a sql server DB with about 150 records, if i make my strSQL a "select * from markers" it will create a xml file right on the page with all 150 records, the sql query in the php seems to do plug in the querystring values to pull a dynamically created distance and generate a new sql query. Thats is where I am stuck.

    The page that post to this page sends 3 query strings values like this

    genxml.asp?lat=' + center.lat() + '&lng=' + center.lng() + '&radius=' + radius


    <%
    <!--# include file="dbconnect.asp"-->

    Dim adoCon, rs, strSQL, conn, center_lat, center_lng, radius        

    center_lat = Request.QueryString("lat")
    center_lng = Request.QueryString("lng")
    radius = Request.QueryString("radius")


    ' this is where i am stuck. How do I  plugging in my querystring requests, the below obviously doesnt work

    strSQL="SELECT address, name, 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"_
    & center_lat _
    & center_lng _
    & center_lat _
    & radius _
    result = strSQL



    rs.Open strSQL, adoCon

    if rs.eof then

    response.write "No matches.<br><br>"
            
    else

    response.write("<?xml version='1.0' encoding='UTF-8'?>")
    'response.Write objXmlDoc.xml
    response.write("<markers>")

    Do While not rs.EOF
      response.write("<marker>")
      response.write("<lat>" & rs("lat") & "</lat>")
      response.write("<lng>" & rs("lng") & "</lng>")
      response.write("<name>" & rs("name") & "</name>")
      response.write("<address>" & rs("address") & "</address>")
      response.write("<distance>" & rs("distance") & "</distance>")
      response.write("</marker>")
      rs.MoveNext()

    rs.MoveNext
    Loop

    end if
    response.write("</markers>")



    rs.Close
    Set rs = Nothing
    Set adoCon = Nothing
    %>

  • 07-30-2008, 11:13 PM In reply to

    • Gaver
    • Not Ranked
    • Joined on 07-31-2008, 2:55 AM
    • Posts 2

    Re: convert PHP TO ASP

    Joe,

    Although I can't help with the line by line conversion... I can point you in the right direction to understanding the problem you are experiencing.

    Some background first... I'm trying to do exactly the same thing you are, except I'm converting it to Cold Fusion. I have already figured out how to get the geocode routine to batch convert my addresses to lat / lng and store them in a db, and I've got a map working correctly - but I want to be able to create some additional functionality that I'm not comfortable in attempting using PHP. Kind of a stick with what you know problem for me... anyway back to your question...

    These lines:
      mysql_real_escape_string($center_lat),
      mysql_real_escape_string($center_lng),
      mysql_real_escape_string($center_lat),
      mysql_real_escape_string($radius));

    are variables that are being substituted in the query definition above it. For each %s you see, one of the lines is being substituted as a value. They are substituted in the same order they are shown on the page. They are also a call to a PHP function that will encode the strings such that they help to prevent a malicious attack on your MySQL server.

    The Fix...I'm not certain about the actual coding for your query in ASP / ADO - but here's a sample of what I'm going to end up doing in CF to accomplish the same task.

    Ref.
    SELECT address, name, lat, lng, ( 3959 * acos( cos( radians(#LAT_VAR#) ) * cos( radians( lat ) ) * cos( radians( lng ) - radians('#LNG_VAR#') ) + sin( radians('#LAT_VAR#') ) * sin( radians( lat ) ) ) ) AS distance FROM markers HAVING distance < '#RADIUS#' ORDER BY distance LIMIT 0 , 20

    If you substitute your values for #LAT_VAR#; #LNG_VAR# & #RADIUS# the query should work for you.

    Further Reading: Lookup the PHP sprintf() function and the mysql_real_escape_string() function. It will begin to make a lot more sense after you see how and why they are using those functions.

    Gaver Powers
    Tropic Signs & Shirts
    "forgiveness is easier than permission"

    ps. You can test your code by calling the page and putting the lat;lng and radius values on the url e.g. http://www.foo.biz/dbtest.asp?lat=22&lng=-82&radius=25 of course you will need to use lat/lng numbers that are more representative of the numbers in your db if you want to see any results.

  • 07-31-2008, 12:18 AM In reply to

    • havanajoe26
    • Not Ranked
    • Joined on 07-24-2008, 5:21 PM
    • Pasadena, CA
    • Posts 4

    Re: convert PHP TO ASP

     Thanks Gaver, I was on a tight deadline and couldnt afford to spend any more time on this so i ended up installing php and mysql on my windows 2003 server. works beautifully now. i have my asp page post to my php page. a bit of a work around, but hey it works. i am not using my work machine right now but will gladly post my code tomorrow.

     

     

  • 07-31-2008, 5:18 AM In reply to

    • Gaver
    • Not Ranked
    • Joined on 07-31-2008, 2:55 AM
    • Posts 2

    Re: convert PHP TO ASP

    Great Joe, Glad to hear you got it working. I look forward to seeing your code - it may help me with my transition as well. I'm interested in seeing how you form your XML marker segments.

     GP

  • 07-31-2008, 11:55 AM In reply to

    • havanajoe26
    • Not Ranked
    • Joined on 07-24-2008, 5:21 PM
    • Pasadena, CA
    • Posts 4

    Re: convert PHP TO ASP

    Here's the PHP. I had to modify the XML markers portion some because the store locator code sample didnt not work verbatim for me. Google did provide an alternate way depending on your PHP configuration. Find that here: http://code.google.com/support/bin/answer.py?answer=65622&topic=11364#outputxml

    Here's a link to the html/asp page that sends info to this page. look at the source code, you can use it as it. Sorry it doesnt work, my db is set up on my work server. But the code below is what makes it happen.

    http://daverosewebworks.com/gmaps/finder.html

     

    <?php

    $username="myusername";
    $password="mypw";
    $database="mydb";

    // Get parameters from URL
    $center_lat = $_GET["lat"];
    $center_lng = $_GET["lng"];
    $radius = $_GET["radius"];

    function parseToXML($htmlStr)
    {
    $xmlStr=str_replace('<','&lt;',$htmlStr);
    $xmlStr=str_replace('>','&gt;',$xmlStr);
    $xmlStr=str_replace('"','&quot;',$xmlStr);
    $xmlStr=str_replace("'",'&#39;',$xmlStr);
    $xmlStr=str_replace("&",'&amp;',$xmlStr);
    return $xmlStr;
    }

    // Opens a connection to a MySQL server
    $connection=mysql_connect (localhost, $username, $password);
    if (!$connection) {
      die('Not connected : ' . mysql_error());
    }

    // Set the active MySQL database
    $db_selected = mysql_select_db($database, $connection);
    if (!$db_selected) {
      die ('Can\'t use db : ' . mysql_error());
    }

    // Select all the rows in the markers table
    $query = sprintf("SELECT address, name, icontype, 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);

    if (!$result) {
      die("Invalid query: " . mysql_error());
    }

    header("Content-type: text/xml");

    // Start XML file, echo parent node
    echo '<markers>';

    // Iterate through the rows, printing XML nodes for each
    while ($row = @mysql_fetch_assoc($result)){
      // ADD TO XML DOCUMENT NODE
      echo '<marker ';
      echo 'name="' . parseToXML($row['name']) . '" ';
      echo 'address="' . parseToXML($row['address']) . '" ';
      echo 'lat="' . $row['lat'] . '" ';
      echo 'lng="' . $row['lng'] . '" ';
      echo 'distance="' . $row['distance'] . '" ';
      echo 'icontype="' . $row['icontype'] . '" ';

      echo '/>';
    }

    // End XML file
    echo '</markers>';

    ?>

Page 1 of 1 (7 items)
Page view counter