Ciao
, devo generare un file che ha questa struttura:
Codice PHP:
var data = {"Store": [
{"id": 001, "name": "Ragione Sociale", "address": "Via...", "lat": 41.1227, "lng": 12.4605},
{"id": 002, "name": "Ragione Sociale", "address": "Via...", "lat": 41.1227, "lng": 12.4605},
{"id": 003, "name": "Ragione Sociale", "address": "Via...", "lat": 45.7227, "lng": 12.9605}
]}
dove l'ultimo record non ha la virgola finale
è giusto quello che sto facendo?
Codice PHP:
// Start XML file, echo parent node
echo 'var data = {"Store": [';
// Iterate through the rows, printing XML nodes for each
while ($row = @mysql_fetch_assoc($result)){
// ADD TO XML DOCUMENT NODE
echo '{"id": ' . $row['id'] . ', ';
echo '"name": "' . parseToXML($row['name']) . '", ';
echo '"address": "' . parseToXML($row['address']) . '", ';
echo '"lat": ' . $row['lat'] . ',';
echo '"lng": ' . $row['lng'] . '}';
echo ',';
}
// End XML file
echo ']}';
Se può servire posto tutto il codice della pagina da cui sono partito che generava codice xml e che funziona.
Codice PHP:
<?php
require("phpsqlajax_dbinfo.php");
function parseToXML($htmlStr)
{
$xmlStr=str_replace('<','<',$htmlStr);
$xmlStr=str_replace('>','>',$xmlStr);
$xmlStr=str_replace('"','"',$xmlStr);
$xmlStr=str_replace("'",''',$xmlStr);
$xmlStr=str_replace("&",'&',$xmlStr);
return $xmlStr;
}
// Opens a connection to a MySQL server
$connection=mysql_connect ("mysql.netsons.com", $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 = "SELECT * FROM markers WHERE 1";
$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 'type="' . $row['type'] . '" ';
echo '/>';
}
// End XML file
echo '</markers>';
?>