Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> xml php + google maps v3, problem z generowaniem pliku xml
mglowitz
post 23.08.2012, 12:52:01
Post #1





Grupa: Zarejestrowani
Postów: 5
Pomógł: 0
Dołączył: 23.08.2012

Ostrzeżenie: (0%)
-----


Witam
Jestem nowy na forum więc chciałbym gorąco wszystkich pozdrowić. Chciałbym stworzyć mapę na swojej stronie taką jak https://developers.google.com/maps/articles/phpsqlsearch_v3
Po utworzeniu bazy danych z odpowiednimi kolumnami i danymi niestety przy tworzeniu pliku xml dostaję komunikat
This page contains the following errors:
error on line 2 at column 1: Extra content at the end of the document
Below is a rendering of the page up to the first error.

plik php wygląda tak
<?php
header("Content-type: text/xml");
include ("database.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;
}

// Get parameters from URL
$center_lat = $_GET["lat"];
$center_lng = $_GET["lng"];
$radius = $_GET["radius"];



// Select all the rows in the markers table
$query = sprintf("SELECT address, name, lat, lng, ( 3959 * acos( cos( radians('%s') ) * cos( radians( lat ) ) * cos( radians( lng ) - radians('%s') ) + sin( radians('%s') ) * sin( radians( lat ) ) ) ) AS distance FROM markers HAVING distance < '%s' ORDER BY distance LIMIT 0 , 20",
mysql_real_escape_string($center_lat),
mysql_real_escape_string($center_lng),
mysql_real_escape_string($center_lat),
mysql_real_escape_string($radius));

$result = mysql_query($query);
if (!$result) {
die('Invalid query: ' . mysql_error());
}

// Start XML file, echo parent node
echo "<markers>\n";
// 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 'distance="' . $row['distance'] . '" ';
echo "/>\n";
}

// End XML file
echo "</markers>\n";

?>
Szukałem na google rozwiązania jednak te proponowane niestety nie zdawały egzaminu. Czy ktoś mógłby pomóc? Dodam że korzystam z PHP5
Pozdrawiam

Witam z błędem generowania pliku już sobie poradziłem wstawiłem echo '<'.'?xml version="1.0" encoding="UTF-8"?'.'>'."\n"; ale niestety pojawił się inny problem. Niestety po wstawieniu poniższego kodu nie wyświetla mi nic ani błędu ani danych. Podejrzewam że klamra zamykająca z linii 53 jest nie w tym miejscu (otwierająca w 15) ale nie jestem pewien. Kod aktualny
<?php
echo '<'.'?xml version="1.0" encoding="UTF-8"?'.'>'."\n";
include ("database.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;
}

if (isset($center_lat) && isset($center_lng) && isset($radius)){
$center_lat = $_GET["lat"];
$center_lng = $_GET["lng"];
$radius = $_GET["radius"];



// Select all the rows in the markers table
$query = sprintf("SELECT address, name, lat, lng, ( 3959 * acos( cos( radians('%s') ) * cos( radians( lat ) ) * cos( radians( lng ) - radians('%s') ) + sin( radians('%s') ) * sin( radians( lat ) ) ) ) AS distance FROM markers HAVING distance < '%s' ORDER BY distance LIMIT 0 , 20",
mysql_real_escape_string($center_lat),
mysql_real_escape_string($center_lng),
mysql_real_escape_string($center_lat),
mysql_real_escape_string($radius));
mysql_query('SET character_set_connection=utf8');
mysql_query('SET character_set_client=utf8');
mysql_query('SET character_set_results=utf8');

$result = mysql_query($query);
if (!$result) {
die('Invalid query: ' . mysql_error());
}

// Start XML file, echo parent node
echo "<markers>\n";
// 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 'distance="' . $row['distance'] . '" ';
echo "/>\n";
}

// End XML file
echo "</markers>\n";
}
?>
Go to the top of the page
+Quote Post
Tajgeer
post 23.08.2012, 12:57:40
Post #2





Grupa: Zarejestrowani
Postów: 290
Pomógł: 48
Dołączył: 11.05.2008
Skąd: Kielce

Ostrzeżenie: (0%)
-----


Umieść swój kod w odpowiednim BBCode, bo się tego nie da czytać...
Go to the top of the page
+Quote Post
mglowitz
post 23.08.2012, 13:05:21
Post #3





Grupa: Zarejestrowani
Postów: 5
Pomógł: 0
Dołączył: 23.08.2012

Ostrzeżenie: (0%)
-----



  1. <?php
  2. echo '<'.'?xml version="1.0" encoding="UTF-8"?'.'>'."\n";
  3. include ("database.php");
  4.  
  5. function parseToXML($htmlStr)
  6. {
  7. $xmlStr=str_replace('<','&lt;',$htmlStr);
  8. $xmlStr=str_replace('>','&gt;',$xmlStr);
  9. $xmlStr=str_replace('"','&quot;',$xmlStr);
  10. $xmlStr=str_replace("'",''',$xmlStr);
  11. $xmlStr=str_replace("&",'&amp;',$xmlStr);
  12. return $xmlStr;
  13. }
  14.  
  15. if (!isset($center_lat)){
  16. $center_lat = $_GET["lat"];
  17. }
  18.  
  19. if (!isset($center_lng)){
  20. $center_lng = $_GET["lng"];
  21.  
  22. }
  23. if (!isset($radius)){
  24. $radius = $_GET["radius"];
  25.  
  26. }
  27.  
  28. // Select all the rows in the markers table
  29. $query = sprintf("SELECT address, name, lat, lng, ( 3959 * acos( cos( radians('%s') ) * cos( radians( lat ) ) * cos( radians( lng ) - radians('%s') ) + sin( radians('%s') ) * sin( radians( lat ) ) ) ) AS distance FROM markers HAVING distance < '%s' ORDER BY distance LIMIT 0 , 20",$center_lat, $center_lng, $radius);
  30.  
  31.  
  32.  
  33. $result = mysql_query($query);
  34. if (!$result) {
  35. die('Invalid query: ' . mysql_error());
  36. }
  37.  
  38. // Start XML file, echo parent node
  39. echo "<markers>\n";
  40. // Iterate through the rows, printing XML nodes for each
  41. while ($row = @mysql_fetch_assoc($result)){
  42. // ADD TO XML DOCUMENT NODE
  43. echo '<marker ';
  44. echo 'name="' . parseToXML($row['name']) . '" ';
  45. echo 'address="' . parseToXML($row['address']) . '" ';
  46. echo 'lat="' . $row['lat'] . '" ';
  47. echo 'lng="' . $row['lng'] . '" ';
  48. echo 'distance="' . $row['distance'] . '" ';
  49. echo "/>\n";
  50. }
  51.  
  52. // End XML file
  53. echo "</markers>\n";
  54.  
  55. ?>

Wywala mi błąd
Notice: Undefined index: lat in C:\xampp\htdocs\test2.php on line 16

Notice: Undefined index: lng in C:\xampp\htdocs\test2.php on line 20

Notice: Undefined index: radius in C:\xampp\htdocs\test2.php on line 24

Warning: sprintf() [function.sprintf]: Too few arguments in C:\xampp\htdocs\test2.php on line 29
Invalid query: Query was empty
Go to the top of the page
+Quote Post
Tajgeer
post 23.08.2012, 13:10:09
Post #4





Grupa: Zarejestrowani
Postów: 290
Pomógł: 48
Dołączył: 11.05.2008
Skąd: Kielce

Ostrzeżenie: (0%)
-----


I co jest niezrozumiałego w tych błędach? smile.gif

Próbujesz przypisywać do zmiennych wartości za tablicy $_GET mimo, że ich nie ma.
Go to the top of the page
+Quote Post
mglowitz
post 23.08.2012, 13:17:55
Post #5





Grupa: Zarejestrowani
Postów: 5
Pomógł: 0
Dołączył: 23.08.2012

Ostrzeżenie: (0%)
-----


Ale jak w linku dodam
http://localhost/test2.php?lat=49&lng=19&radius=40
to niestety wywala mi nadal błąd
Warning: sprintf() [function.sprintf]: Too few arguments in C:\xampp\htdocs\test2.php on line 29
Invalid query: Query was empty
Go to the top of the page
+Quote Post
Tajgeer
post 23.08.2012, 13:39:57
Post #6





Grupa: Zarejestrowani
Postów: 290
Pomógł: 48
Dołączył: 11.05.2008
Skąd: Kielce

Ostrzeżenie: (0%)
-----


Zauważ, że sprintf w Twoim przypadku oczekuje 4 dodatkowych argumentów - Ty podajesz tylko 3.
Go to the top of the page
+Quote Post
mglowitz
post 23.08.2012, 13:56:29
Post #7





Grupa: Zarejestrowani
Postów: 5
Pomógł: 0
Dołączył: 23.08.2012

Ostrzeżenie: (0%)
-----


Zrobiłem trochę inaczej i jest ok dzięki za pomoc
  1. <?php
  2. include ("database.php");
  3. $result = mysql_query("SELECT *, ACOS( SIN( RADIANS( `lat` ) ) * SIN( RADIANS( 49 ) ) + COS( RADIANS( `lat` ) )
  4. * COS( RADIANS( 49 )) * COS( RADIANS( `lng` ) - RADIANS( 23 )) ) * 6380 AS `distance`
  5. FROM `markers` ORDER BY distance ASC LIMIT 0,20");
  6.  
  7. $doc = new DomDocument('1.0');
  8. $root = $doc->createElement('markers');
  9. $root = $doc->appendChild($root);
  10. while($row = mysql_fetch_assoc($result))
  11. {
  12. $node = $doc->createElement('marker');
  13.  
  14. $node = $root->appendChild($node);
  15. $node->setAttribute('lat', $row['lat']);
  16. $node->setAttribute('lng', $row['lng']);
  17. $node->setAttribute('symbol', $row['symbol']);
  18. $node->setAttribute('distance', $row['distance']);
  19. }
  20. $doc->save("categories.xml");
  21. ?>
Go to the top of the page
+Quote Post

Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Wersja Lo-Fi Aktualny czas: 23.04.2024 - 13:19