Ciao, devo generare un file che ha questa struttura:
dove l'ultimo record non ha la virgola finaleCodice 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}
]}
è 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>';
?>![]()

, devo generare un file che ha questa struttura:
Rispondi quotando
susbtr al posto di substr
che ho corretto e adesso funge alla grande!!!!
Si dovrebbe convertire questo script in json ? oppure c'è una soluzione diversa più semplice? Grazie ancora!!!
lasciando tutto come ho descritto, mi sono accorto che nell'output il valore di Latitudine del quartultimo record era formalmente sbagliato!!!
rimosso dal database l'intoppo lo script ha funzionato!!
l'aiuto che mi avete dato!!!!