Dynamically update variables in external PHP XML g

2019-08-21 07:29发布

问题:

I have a simple php script, very similar to that demonstrated in the google developers examples, which creates XML data from the results of a MySQL query. I'm then using this XML to drive a map displaying waypoints for a given itinerary.

The problem that I have at present is that whilst the page showing the waypoints works, I don't know how to dynamically update the script below with the said itinerary ID. I would normally use $_GET to pass a variable, especially with a non-sensitive ID, but as this script is a separate file to the page displaying the mapping output, I'm not sure how to dynamically update variables within it.

If someone can explain how I can pass a value to this script so as to update the itineraryID within the query that I have marked as '!!!!' it would be much appreciated.

    <?php

    require("phpsqlajax_dbinfo.php");

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

    // Opens a connection to a mySQL server
    $connection=mysql_connect ($db_host, $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 locations table
    $query = "SELECT itinerary_link.itineraryID, itinerary_link.coursesID, itinerary_courses.coursename, courses.lat, courses.lng FROM itinerary_link LEFT JOIN itinerary_courses ON itinerary_link.coursesID = itinerary_courses.coursesID
 LEFT JOIN courses ON courses.coursename = itinerary_courses.coursename WHERE itineraryID=!!!! ORDER BY coursename";
    $result = mysql_query($query);
    //$ti1 = "U8abKhsdiu";
    //$hashed = $row['coursename'];
    //$bh= sha1($hashed);
    //$tileimage = sha1("$bh$ti1");
    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)){

    // Define variables for infoWindow images   
      // ADD TO XML DOCUMENT NODE
      echo '<marker ';
      echo 'name="' . parseToXML($row['coursename']) . '" ';
      echo 'lat="' . $row['lat'] . '" ';
      echo 'lng="' . $row['lng'] . '" ';
      echo '/>';
    }

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

    ?>

回答1:

I can't comment on posts yet, or I'd just ask for clarification. But I have to make assumptions about how you are using this script:

If you accessing this script through an include in the page that uses it then you can use $_GET and $_POST in the way that you are familiar.

But I suspect that's not the way you're doing it as you said dynamically!

Which means calling the script from the page you want to update using AJAX (asynchronous javascript and xml) or jQuery's simpler ajax functions.

The idea is you call this script with jQuery or AJAX from the page you want updated and then use the results (your XML) to update the page.

These methods allow post GET and POST information to be sent as well. The examples below show their usage, but you'll have to follow the links to see the proper, full, implementation.

Whichever method you choose, at the PHP end you use the same $_GET/$_POST with which you are familiar.

AJAX: ajaxRequest.open("GET", "ajax-example.php" + queryString, true);

full example: http://www.tizag.com/ajaxTutorial/ajax-javascript.php

jQuery: $.get("test.php", { name:"Donald", town:"Ducktown" });

full example: http://www.w3schools.com/jquery/ajax_get.asp



标签: php ajax get