动态更新外部PHP XML生成脚本中的变量 [英] Dynamically update variables in external PHP XML generation script

查看:129
本文介绍了动态更新外部PHP XML生成脚本中的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的php脚本,与google开发人员的例子非常相似,它可以从MySQL查询的结果中创建XML数据。然后我使用这个XML来驱动一个地图,显示给定行程的航点。



我目前遇到的问题是,虽然显示航点的页面有效,我不知道如何用上述行程ID动态更新下面的脚本。我通常会使用$ _GET传递一个变量,尤其是使用非敏感的ID,但由于此脚本是显示映射输出的页面的单独文件,因此我不确定如何动态更新其中的变量。

如果有人可以解释如何将值传递给此脚本以更新查询中我标记为'!!!!'的itineraryID,它会请注意。

 <?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(','&'; $ xmlStr);
$ xmlStr = str_replace(&,'& amp;',$ xmlStr);
return $ xmlStr;
}

//打开一个连接到一个mySQL服务器
$ connection = mysql_connect($ db_host,$ username,$ password);
if(!$ connection){
die('Not connected:'。mysql_error());


//设置活动的mySQL数据库
$ db_selected = mysql_select_db($ database,$ connection);
if(!$ db_selected){$ b $ (无法使用db:'。mysql_error());
}

//选择locations表中的所有行
$ 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 cou rses.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('无效查询:'。mysql_error());
}

header(Content-type:text / xml);

//启动XML文件, echo $ {$ b $ echo $< markers>';

//遍历行,为每个
打印XML节点while($ row = @mysql_fetch_assoc($ result) ){

//为infoWindow映像定义变量
//添加到XML文档节点
echo'< marker';
echo'name =''。 parseToXML($ row ['coursename'])。 ''';
echo'lat ='。 $ row ['lat']。 ''';
echo'lng ='。 $ row ['lng']。 ''';
echo'/>';
}

//结束XML文件
echo'< / markers>';

?>


解决方案

对帖子发表评论,或者我只是要求澄清,但我必须假设你是如何使用这个脚本的:



如果你通过在使用它的页面中包含,那么您可以以您熟悉的方式使用$ _GET和$ _POST。



<



这意味着你不是这样做的,使用AJAX(异步JavaScript和xml)或jQuery更简单的ajax函数从您想要更新的页面调用脚本。



jQuery或AJAX从您想要更新的页面中获取,然后使用结果(您的XML)更新页面。



<这些方法允许发送post和GET信息。以下示例显示了它们的用法,但您必须按照链接查看正确的完整实施。



无论您选择哪种方法,PHP端都会使用您熟悉的相同$ _GET / $ _ POST。


$ b

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



完整示例: http:// www。 tizag.com/ajaxTutorial/ajax-javascript.php

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



完整示例: http://www.w3schools.com/jquery/ajax_get.asp


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

    ?>

解决方案

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 XML生成脚本中的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆