0

I am writing a Javascript which shows latitude and longitude on google maps. It takes input data from an xml file. I have the latitude and longitude values stored in my MySql database. Now, all I need to do is populate 3 fields --- Name, Lat and Lon on an xml file.

I have gathered and written the following code but it seems to give an error

Parse error: syntax error, unexpected T_ELSE .... on line 52

The xml file I need should be in this order...

<?xml version="1.0"?>

<markers>
<marker>
<name>Yaz01</name>
<lat>52.5914943</lat>
<lng>-2.1328675</lng>
</marker>

<marker>
<name>Yaz02</name>
<lat>52.45041265041157</lat>
<lng>-1.9247746467590332</lng>
</marker>

<marker>
<name>Yaz03</name>
<lat>52.584720758388734</lat>
<lng>-2.125253677368164</lng>
</marker>

</markers>

Here is the code I've written for your reference ...

 <!DOCTYPE html>
<html>
<head>
<title>Google Maps</title>
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
<script type="text/javascript" src="scripts/jquery-1.4.1.min.js"></script>
<script type="text/javascript" src="scripts/markers.js">

</script>
</head>
<body>
<?php
include 'datalogin.php';

$table_name = 'draininfo';

$db = mysql_select_db($dbName, $link);
$query = "select DRAINNAME, LATITUDE, LONGITUDE from " . $table_name;

$result = mysql_query($query, $connection) or die("Could not complete database query");
$num = mysql_num_rows($result);

if ($num != 0) {

 $file= fopen("results.xml", "w");

 $_xml =<<<XML
<?xml version="1.0" encoding="UTF-8" ?>\r\n
XML;

 $_xml .="<markers>\r\n";

 while ($row = mysql_fetch_array($result)) {



 $_xml .="\t<marker>\r\n";
  $_xml .="\t<name>" . $row["DRAINNAME"] . "<\"name>\r\n";
   $_xml .="\t<lat>" . $row["LATITUDE"] . "<\"lat>\r\n";
    $_xml .="\t<lon>" . $row["LONGITUDE"] . "<\"lon>\r\n";


$_xml .="\t</marker>\r\n";


 $_xml .="</markers>";

 fwrite($file, $_xml);

 fclose($file);

 echo "XML has been written.  <a href=\"results.xml\">View the XML.</a>";

 } 
 else {

 echo "No Records found";

 } 

 }
 ?>

<div id="map"></div>
<div ><input type="button" id="showmarkers" value="Show Markers" /></div>
</body>
</html>
  • As far as the error goes, the code you've provided is not 52 lines long nor is there a T_ELSE in it, and it's saying the error is occurring on line 52. Please check the *file* (most likely in datalogin.php) and line number and maybe there is a T_ELSE there that shouldn't be, something wasn't closed correctly, etc. – animuson Aug 10 '11 at 19:12
  • [A similar question](http://stackoverflow.com/questions/486757/how-to-generate-xml-file-dynamically-using-php) – Nabil Kadimi Aug 10 '11 at 19:12
  • Sorry !! I pasted the wrong code previously .. Ive updated it now .. – Yasar Abdullah Aug 10 '11 at 19:17
  • 1
    You're missing the closing brace for your `while` statement. Where you have `} else {`, you need an extra `}` to close both the `while` and `if` blocks. – Michael Mior Aug 10 '11 at 19:18
  • I've updated the code with } but still, its giving an error saying -- – Yasar Abdullah Aug 10 '11 at 19:29
  • Warning: mysql_select_db(): supplied argument is not a valid MySQL-Link resource in /home/content/85/7851385/html/yazapp/markers.php on line 17 Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in /home/content/85/7851385/html/yazapp/markers.php on line 20 Could not complete database query – Yasar Abdullah Aug 10 '11 at 19:30
  • add `or die(mysql_error())` after `mysql_query()`. Don't just say "query failed", when mysql can tell you exactly WHY it failed. – Marc B Aug 10 '11 at 19:50

2 Answers2

0

There's no Javascript in your code samples, so I don't know where that language figures in this question.

There's also no line 52 in your PHP script, so you've not pasted in all of your code.

However, the general cause of that error is having too many else clauses in an if statement, or you're missing an if() altogether:

if ($somecondition) {
   blah blah
} else {
   blah blah 
} else {   <--- unexpected, because you can only have 1 "else" clause.
   blah blah
}
Marc B
  • 356,200
  • 43
  • 426
  • 500
0

PHP does not accept this

$_xml ="<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\r\n";

change it to

$_xml =<<<XML
<?xml version="1.0" encoding="UTF-8" ?>\r\n
XML;
ajreal
  • 46,720
  • 11
  • 89
  • 119