Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/251.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP while循环生成KML文件_Php_Mysql_Xml_Kml - Fatal编程技术网

PHP while循环生成KML文件

PHP while循环生成KML文件,php,mysql,xml,kml,Php,Mysql,Xml,Kml,给定一个MySQL房地产数据表,我想生成一个具有以下输出的KML文件: <?xml version="1.0" encoding="UTF-8"?> <kml xmlns="http://www.opengis.net/kml/2.2"> <Document> <Placemark> <name>Property Address Pulled from Address Field</name>

给定一个MySQL房地产数据表,我想生成一个具有以下输出的KML文件:

<?xml version="1.0" encoding="UTF-8"?>
 <kml xmlns="http://www.opengis.net/kml/2.2">
  <Document>
    <Placemark>
      <name>Property Address Pulled from Address Field</name>
      <description>
      Some descriptive data pulled from table inserted here.
      </description>
      <Point>
        <coordinates>Latitude FROM Lat,Long FROM Lng</coordinates>
      </Point>
    </Placemark>
  </Document>
</kml>

从地址字段中提取的属性地址
从此处插入的表格中提取一些描述性数据。
纬度与纬度、距离液化天然气的距离
这是我目前掌握的代码。正如您所看到的,我在编写一个循环来构造我的KML时遇到了问题,如上所示。非常感谢您的帮助

<?php 
require("phpsqlgeocode_dbinfo.php");

// Start XML file, create parent node
$dom = new DOMDocument('1.0', 'UTF-8');
$dom->formatOutput = true; //This was added from the PHP Doc. Nice output.

// Creates the root KML element and appends it to the root document.
$node = $dom->createElementNS('http://www.opengis.net/kml/2.2', 'kml');
$parNode = $dom->appendChild($node);

// Creates a KML Document element and append it to the KML element.
$dnode = $dom->createElement('Document');
$docNode = $parNode->appendChild($dnode);

// 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 * FROM markers");
$result = mysql_query($query);
if (!$result) {
  die("Invalid query: " . mysql_error());
}

header("Content-type: application/vnd.google-earth.kml+xml");

// Iterate through the rows, adding KML nodes for each
while ($row = @mysql_fetch_assoc($result)){
  $dnode = $dom->createElement("Placemark");
  $newnode = $docNode->appendChild($dnode);
  $newnode = $newnode->createElement("Name");
  $newnode = $newnode->createElement("Description");  
  $newnode = $newnode->createElement("Coordinates");
  }
echo $dom->saveXML() . "\n";
?>
formatOutput=true//这是从PHP文档中添加的。输出很好。
//创建根KML元素并将其附加到根文档。
$node=$dom->createElements('http://www.opengis.net/kml/2.2","kml",;
$parNode=$dom->appendChild($node);
//创建KML文档元素并将其附加到KML元素。
$dnode=$dom->createElement('Document');
$docNode=$parNode->appendChild($dnode);
//打开到mySQL服务器的连接
$connection=mysql\u connect(localhost,$username,$password);
如果(!$connection){
die(“未连接:”.mysql_error());
}
//设置活动mySQL数据库
$db\u selected=mysql\u select\u db($database,$connection);
如果(!$db_选中){
die(“无法使用db:.mysql_error());
}
//搜索标记表中的行
$query=sprintf(“从标记中选择*);
$result=mysql\u query($query);
如果(!$result){
die(“无效查询:”.mysql_error());
}
标题(“内容类型:application/vnd.googleearth.kml+xml”);
//遍历行,为每个行添加KML节点
while($row=@mysql\u fetch\u assoc($result)){
$dnode=$dom->createElement(“Placemark”);
$newnode=$docNode->appendChild($dnode);
$newnode=$newnode->createElement(“名称”);
$newnode=$newnode->createElement(“说明”);
$newnode=$newnode->createElement(“坐标”);
}
echo$dom->saveXML()。“\n”;
?>
createElement()
DOMDocument
类(根文档
$dom
)的一种方法,据我所知(并基于我对

您已经创建了3个新元素,但没有将它们作为
$dnode
的子元素追加。请对每个元素使用
$dom->createElement()
,并将其追加到正确的
$dnode(Placemark)

要强制使用.kml文件名,请使用
内容处置
标题:

header("Content-type: application/vnd.google-earth.kml+xml");
header("Content-disposition: inline; filename=$somefilename.kml");

好的,这会产生什么结果呢?谢谢Michael,你的解释很有意义,代码现在似乎可以运行了。你能解释一下我如何将MySQL表中的数据插入每个节点吗?例如,Name节点(插入地址)、Coordinates节点(插入Lat/Long坐标),等等。谢谢!最后一个问题,代码当前返回一个.PHP文件。我如何修改代码以返回一个.KML文件?谢谢!@AME添加一个内容处置头,就像我上面所做的那样。在参考KML文档后,我看到坐标节点必须是“点”节点的子节点,这样KML文件才能正确结构。@AME在上面创建一个点节点,并将坐标作为它的子节点。
header("Content-type: application/vnd.google-earth.kml+xml");
header("Content-disposition: inline; filename=$somefilename.kml");