Solved

Remove White Space

Posted on 2010-08-16
11
447 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
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
 
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 143

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
PHP strings vs array errors 13 33
Wordpress French and English Site 6 78
Add to XML (Powershell) 1 30
Inserting results of While loop to sql server 5 20
SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
This article discusses how to create an extensible mechanism for linked drop downs.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…

828 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