0

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>';

    ?>
hakre
  • 193,403
  • 52
  • 435
  • 836
Welly
  • 305
  • 6
  • 21
  • Hint #1: [Generating XML document in PHP (escape characters)](http://stackoverflow.com/q/3957360/367456) - Hint #2: [How to convert array to SimpleXML](http://stackoverflow.com/q/1397036/367456) – hakre Feb 18 '13 at 20:32
  • @hakre - Thanks for the info. I'm sure it's me not getting for a second what you're hinting at, but after looking at those pages it seems both relate to the xml itself. My issue is passing the php variable from another page to this script - The data being produced works fine it just relies on me entering an ID number manually in order to test the mapping page rather than this being dynamic as desired. As I explained above the reason I don't see a use for $_GET is that the user will never navigate to this page. Thanks again – Welly Feb 18 '13 at 20:49
  • That depends on your needs you didn't outline. I don't see any problem in using `$_GET` anyway to be honest, but I don't know if that feedback is helpful for you. – hakre Feb 18 '13 at 20:51
  • @hakre Sorry I always think I explain myself better than I must be. The reason I can't see get working is that I am linking from page1.php to page2.php, with page2.php being the mapping page calling the script above (xmlgen.php). I thought that I could only use $_GET to create a link of something like 'page2.php?id=5', I don't see how I can then use this to update a variable inside xmlgen.php . If I'm still not making sense I'll have to go away and try figure it out, thanks thus far! – Welly Feb 18 '13 at 21:00
  • Yes it works with AJAX request. Yes, it's much faster to write a small test-script that process $_GET and returns the input in three lines of code and then call it with another three lines of javascript code in a standard HTML page you might already have. So yes, you're asking a lot for what you could just test within less than 10 minutes ;) – hakre Feb 19 '13 at 06:44

1 Answers1

0

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

abase
  • 220
  • 1
  • 8
  • Thanks for your input. To clarify, there is already an AJAX call on the mapping page that calls my XML gen file, which works fine. The problem is trying dynamically update part of the query which generates the XML before it is called from the page displaying the map and processed data. I can't have the script as then when you try to load the main page a 'XML Parsing Error: junk after document element' error message is produced (script tries to parse the page it's been included by). – Welly Feb 18 '13 at 21:38
  • Further to my last, all sorted, I don't know why I didn't think to use $_GET in my call straight away! I've now got: downloadUrl("xmlgenv1.php?id="+''+"", function(data) { which works perfectly. Cheers @abase – Welly Feb 18 '13 at 22:01
  • `downloadUrl("xmlgenv1.php?id=" + ` – hakre Feb 19 '13 at 06:46
  • So glad that it worked out! I think you'll find the strategy you employed super useful on future projects. – abase Feb 19 '13 at 14:25