Solved

Remove White Space

Posted on 2010-08-16
11
443 Views
Last Modified: 2013-12-13
Looks like your PHP is putting out a space before the <?xml ...?> declaration, which is technically invalid XML

How do I remove that? This is for Google Maps
<?
require('/fmicdev/geo_search_connect.php');
$center_lat = $_GET["latitude"];
$center_lng = $_GET["longitude"];
$radius = $_GET["radius"];
$connection=mysql_connect (localhost, $username, $password);
if (!$connection) {
  die("Not connected : " . mysql_error());
}
$dom =new DOMDocument("1.0");
$node =$dom->createElement("markers");
$parnode =$dom->appendChild($node);
$db_selected = mysql_select_db($database, $connection);
if (!$db_selected) {
  die ("Can\'t use db : " . mysql_error());
}
$query = sprintf("SELECT concat(street,', ',city,', ',stateprov,', ',zip) AS address, custname, latitude, longitude, ( 3959 * acos( cos( radians('%s') ) * cos( radians( latitude ) ) * cos( radians( longitude ) - radians('%s') ) + sin( radians('%s') ) * sin( radians( latitude ) ) ) ) AS distance FROM dealer 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);
$result = mysql_query($query);
if (!$result) {
  die("Invalid query: " . mysql_error());
}
header("Content-type: application/xml");
while ($row = @mysql_fetch_assoc($result)){
  $node = $dom->createElement("marker");
  $newnode = $parnode->appendChild($node);
  $newnode->setAttribute("name", $row['custname']);
  $newnode->setAttribute("address", $row['address']);
  $newnode->setAttribute("latitude", $row['latitude']);
  $newnode->setAttribute("longitude", $row['longitude']);
  $newnode->setAttribute("distance", $row['distance']);
}
echo $dom->saveXML();
?>

Open in new window


0
Comment
Question by:rgranlund
11 Comments
 
LVL 58

Accepted Solution

by:
cyberkiwi earned 125 total points
ID: 33451214
$dom->saveXML() should never have leading or trailing spaces.
What you need to make sure of, is that there is NO space before the first <? tag (line one of your code) and no space, linebreak or anything after the last ?> of your code.
Because PHP will treat any source code not encased in <? ?> as normal html data and output it to the browser/stream.
0
 
LVL 7

Author Comment

by:rgranlund
ID: 33451290
There are no spaces. Do you have any other thoughts?
0
 
LVL 18

Assisted Solution

by:Jerry Miller
Jerry Miller earned 125 total points
ID: 33451334
It looks like you may have something after the ?>. You have a line 35 that appears blank, so there was most likely something there in your copy / paste.
0
 
LVL 7

Author Comment

by:rgranlund
ID: 33451406
Please have a look again, cause I'm still getting this error:
[code]
<?php
require('/fmicdev/geo_search_connect.php');
$center_lat = $_GET["latitude"];
$center_lng = $_GET["longitude"];
$radius = $_GET["radius"];
$connection=mysql_connect (localhost, $username, $password);
if (!$connection) {
  die("Not connected : " . mysql_error());
}
$dom =new DOMDocument("1.0");
$node =$dom->createElement("markers");
$parnode =$dom->appendChild($node);
$db_selected = mysql_select_db($database, $connection);
if (!$db_selected) {
  die ("Can\'t use db : " . mysql_error());
}
$query = sprintf("SELECT concat(street,', ',city,', ',stateprov,', ',zip) AS address, custname, latitude, longitude, ( 3959 * acos( cos( radians('%s') ) * cos( radians( latitude ) ) * cos( radians( longitude ) - radians('%s') ) + sin( radians('%s') ) * sin( radians( latitude ) ) ) ) AS distance FROM dealer 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);
$result = mysql_query($query);
if (!$result) {
  die("Invalid query: " . mysql_error());
}
header("Content: application/xml");
while ($row = @mysql_fetch_assoc($result)){
  $node = $dom->createElement("marker");
  $newnode = $parnode->appendChild($node);
  $newnode->setAttribute("name", $row['custname']);
  $newnode->setAttribute("address", $row['address']);
  $newnode->setAttribute("latitude", $row['latitude']);
  $newnode->setAttribute("longitude", $row['longitude']);
  $newnode->setAttribute("distance", $row['distance']);
}
echo $dom->saveXML();
?>
[/code]

It appears to be commented out.
<?php
require('/fmicdev/geo_search_connect.php');
$center_lat = $_GET["latitude"];
$center_lng = $_GET["longitude"];
$radius = $_GET["radius"];
$connection=mysql_connect (localhost, $username, $password);
if (!$connection) {
  die("Not connected : " . mysql_error());
}
$dom =new DOMDocument("1.0");
$node =$dom->createElement("markers");
$parnode =$dom->appendChild($node);
$db_selected = mysql_select_db($database, $connection);
if (!$db_selected) {
  die ("Can\'t use db : " . mysql_error());
}
$query = sprintf("SELECT concat(street,', ',city,', ',stateprov,', ',zip) AS address, custname, latitude, longitude, ( 3959 * acos( cos( radians('%s') ) * cos( radians( latitude ) ) * cos( radians( longitude ) - radians('%s') ) + sin( radians('%s') ) * sin( radians( latitude ) ) ) ) AS distance FROM dealer 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);
$result = mysql_query($query);
if (!$result) {
  die("Invalid query: " . mysql_error());
}
header("Content: application/xml");
while ($row = @mysql_fetch_assoc($result)){
  $node = $dom->createElement("marker");
  $newnode = $parnode->appendChild($node);
  $newnode->setAttribute("name", $row['custname']);
  $newnode->setAttribute("address", $row['address']);
  $newnode->setAttribute("latitude", $row['latitude']);
  $newnode->setAttribute("longitude", $row['longitude']);
  $newnode->setAttribute("distance", $row['distance']);
}
echo $dom->saveXML();
?>

Open in new window

0
 
LVL 7

Author Comment

by:rgranlund
ID: 33451414
Also, it works fine in IE but not in Firefox.
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 2

Expert Comment

by:Tiller79188231
ID: 33467206
can you post the xml output from firebug?
0
 
LVL 7

Author Comment

by:rgranlund
ID: 33467286
white space problem solved
0
 
LVL 2

Expert Comment

by:Tiller79188231
ID: 33467336
what was it?
0
 
LVL 7

Author Comment

by:rgranlund
ID: 33468659
There was a space after the ?> in the include file.  It only took me about 8 hours to find it.  Geez.
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 34881964
This question has been classified as abandoned and is being closed as part of the Cleanup Program. See my comment at the end of the question for more details.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Many times as a report developer I've been asked to display normalized data such as three rows with values Jack, Joe, and Bob as a single comma-separated string such as 'Jack, Joe, Bob', and vice versa.  Here's how to do it. 
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now