Link to home
Start Free TrialLog in
Avatar of billy21
billy21

asked on

PHP code is dislaying as literal string at runtime

Please see the attached PHP code snipet.

Scroll down to the bottom to the section labelled //THIS IS WHERE THE TROUBLE BEGINS

I am attempting to build an xml string.
Look at this line...
 $xml  = '<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>'."\n";

When I run this, everything after the last single quote (ie the ."\n"; and all the code after that) displays literally displays on screen in my browser.

When I remove this line it still happens but starts from the next line.

Anyone happen to know why this would be happening?

<?php
 	
 	  // Database connection settings
 	  define("PG_DB"  , "test");
 	  define("PG_HOST", "localhost");
 	  define("PG_USER", "postgres");
 	  define("PG_PORT", "5432");
 	  define("TABLE",   "ways");
 	  
 
 	
 	  $counter = $pathlength = 0;
 	
 	  // Retrieve start point
 	 $start = split(' ',$_REQUEST['startpoint']);
 	
 	  $startPoint = array($start[0], $start[1]);
 	 
 	 
 	 
 	 
 	 
 	  // Retrieve end point
 	  $end = split(' ',$_REQUEST['finalpoint']);
 	
 	 $endPoint = array($end[0], $end[1]);
 	 
 	 
 	
 	  // Find the nearest edge
 	  $startEdge = findNearestEdge($startPoint);
 	 
 	
 	  $endEdge   = findNearestEdge($endPoint);
 	
 	  // FUNCTION findNearestEdge
 	  function findNearestEdge($lonlat) {
 	    
 	    // Connect to database
 	    $con = pg_connect("dbname=".PG_DB." host=".PG_HOST." password=".PG_PASS." user=".PG_USER);
 	    
 	    $sql = "SELECT gid, source, target, the_geom,
 	             distance(the_geom, GeometryFromText(
 	                  'POINT(".$lonlat[0]." ".$lonlat[1].")', 900913)) AS dist
 	            FROM ".TABLE." 
 	            WHERE the_geom && setsrid(
 	                  'BOX3D(".($lonlat[0]-200)."
 	                         ".($lonlat[1]-200).",
 	                         ".($lonlat[0]+200)."
 	                         ".($lonlat[1]+200).")'::box3d, 900913)
 	            ORDER BY dist LIMIT 1";
 	    
 	 
 	
 	
 	    $query = pg_query($con,$sql); 
 	    
 	    $edge['gid']      = pg_fetch_result($query, 0, 0); 
 	    $edge['source']   = pg_fetch_result($query, 0, 1); 
 	    $edge['target']   = pg_fetch_result($query, 0, 2); 
 	    $edge['the_geom'] = pg_fetch_result($query, 0, 3); 
 	
 	    // Close database connection
 	    pg_close($con);
 	
 	    return $edge;
 	  }
 	
 	  // Select the routing algorithm
 	   switch($_REQUEST['method']) {
 	
 	
 	
 	
 	
 	    case 'SPD' : // Shortest Path Dijkstra
 	      $sql = "SELECT rt.gid, AsText(rt.the_geom) AS wkt,
 	                   length(rt.the_geom) AS length, ".TABLE.".id
 	                FROM ".TABLE.",
 	                    (SELECT gid, the_geom
 	                        FROM dijkstra_sp_delta(
 	                            '".TABLE."',
 	                            ".$startEdge['source'].",
 	                            ".$endEdge['target'].",
 	                            3000)
 	                     ) as rt
 	                WHERE ".TABLE.".gid=rt.gid;";
 	                
 	
 	                
 	      break;
 	
 	    case 'SPA' : // Shortest Path A*
 	
 	      $sql = "SELECT rt.gid, AsText(rt.the_geom) AS wkt,
 	                     length(rt.the_geom) AS length, ".TABLE.".id
 	                  FROM ".TABLE.",
 	                      (SELECT gid, the_geom
 	                          FROM astar_sp_delta(
 	                              '".TABLE."',
 	                              ".$startEdge['source'].",
 	                              ".$endEdge['target'].",
 	                              3000)
 	                       ) as rt
 	                  WHERE ".TABLE.".gid=rt.gid;"; 
	      break;
 	
 	    case 'SPS' : // Shortest Path Shooting*
 	
 	      $sql = "SELECT rt.gid, AsText(rt.the_geom) AS wkt,
 	                     length(rt.the_geom) AS length, ".TABLE.".id
 	                  FROM ".TABLE.",
 	                      (SELECT gid, the_geom
 	                          FROM shootingstar_sp(
 	                              '".TABLE."',
 	                              ".$startEdge['gid'].",
 	                              ".$endEdge['gid'].",
 	                              3000, 'length', false, false)
 	                       ) as rt
 	                  WHERE ".TABLE.".gid=rt.gid;";
 	      break; 
 	
 	  } // close switch
 	
 	
 	  //  echo $sql;
 	  // Database connection and query
 	  $dbcon = pg_connect("dbname=".PG_DB." host=".PG_HOST." password=".PG_PASS." user=".PG_USER);
 	
 	  $query = pg_query($dbcon,$sql);
 	 
 	  // Return route as XML
	//THIS IS WHERE THE TROUBLE BEGINS...
 	  $xml  = '<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>'."\n";
 	  $xml .= "<route>\n";
 	 
 	  // Add edges to XML file
 	  while($edge=pg_fetch_assoc($query)) { 
 	
 	    $pathlength += $edge['length'];
 	
 	    $xml .= "\t<edge id='".++$counter."'>\n";
 	    $xml .= "\t\t<id>".$edge['id']."</id>\n";
 	    $xml .= "\t\t<wkt>".$edge['wkt']."</wkt>\n";
 	    $xml .= "\t\t<length>".round(($pathlength/1000),3)."</length>\n";
 	    $xml .= "\t</edge>\n";
 	  }
 	 
 	  $xml .= "</route>\n";
 	        
 	  // Close database connection
 	  pg_close($dbcon);
 	  // Return routing result
 	  header('Content-type: text/xml',true);
 	  echo $xml;
 	 
	?>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of hielo
hielo
Flag of Wallis and Futuna image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of billy21
billy21

ASKER

Hi hielo.  Thanks but that didn't change anything.  I've tried it with short_open_tag on, with it off and also changed the xml string as you suggested both with short_open_tag on and off and it make no difference in any case.
>>When I remove this line it still happens but starts from the next line.
are you saying that the "literal" output is the PHP code?
Avatar of billy21

ASKER


Just as a test I tried removing the ? characters from the xml header tag.  I also removed the xml building lines underneath it just for clarity.

When I run the page this displays in my browser...

'."\n"; // Close database connection pg_close($dbcon); // Return routing result header('Content-type: text/xml',true); echo $xml; ?>

Note: The only ? characters in the system now are for the opening and closing php tag.  So what is displaying at the end is the closing php tag.

Avatar of billy21

ASKER

"are you saying that the "literal" output is the PHP code?"

Yes and even commented out sections are displayed.  This is the full text that I get when I run the original code posted...

'."\n"; $xml .= "\n"; // Add edges to XML file while($edge=pg_fetch_assoc($query)) { $pathlength += $edge['length']; $xml .= "\t\n"; $xml .= "\t\t".$edge['id']."\n"; $xml .= "\t\t".$edge['wkt']."\n"; $xml .= "\t\t".round(($pathlength/1000),3)."\n"; $xml .= "\t\n"; } $xml .= "\n"; // Close database connection pg_close($dbcon); // Return routing result header('Content-type: text/xml',true); echo $xml; ?>
Avatar of billy21

ASKER

The attached code snipet is what I'm currently working with.  And the text below is what is displayed in the browser when I run it.  Maybe I've not implemented your solution correctly?

'."\n"; $xml .= "\n"; // Add edges to XML file while($edge=pg_fetch_assoc($query)) { $pathlength += $edge['length']; $xml .= "\t\n"; $xml .= "\t\t".$edge['id']."\n"; $xml .= "\t\t".$edge['wkt']."\n"; $xml .= "\t\t".round(($pathlength/1000),3)."\n"; $xml .= "\t\n"; } $xml .= "\n"; // Close database connection pg_close($dbcon); // Return routing result header('Content-type: text/xml',true); echo $xml; ?>
<?php
 	ini_set('short_open_tag','Off');
		
 	  // Database connection settings
 	  define("PG_DB"  , "routing");
 	  define("PG_HOST", "localhost");
	  define("PG_PASS", "postgres");
 	  define("PG_USER", "postgres");
 	  define("PG_PORT", "5432");
 	  define("TABLE",   "ways");
 	   	
 	  $counter = $pathlength = 0;
 	
 	  // Retrieve start point
 	 $start = split(' ',$_REQUEST['startpoint']);
 	
 	  $startPoint = array($start[0], $start[1]);
 	  	 
 	  // Retrieve end point
 	  $end = split(' ',$_REQUEST['finalpoint']);
 	
 	 $endPoint = array($end[0], $end[1]);
 	 
 	  // Find the nearest edge
 	  $startEdge = findNearestEdge($startPoint);
 	 
 	
 	  $endEdge   = findNearestEdge($endPoint);
 	
 	  // FUNCTION findNearestEdge
 	  function findNearestEdge($lonlat) {
 	    
 	    // Connect to database
 	    $con = pg_connect("dbname=".PG_DB." host=".PG_HOST." password=".PG_PASS." user=".PG_USER);
 	    
 	    $sql = "SELECT gid, source, target, the_geom,
 	             distance(the_geom, GeometryFromText(
 	                  'POINT(".$lonlat[0]." ".$lonlat[1].")', 900913)) AS dist
 	            FROM ".TABLE." 
 	            WHERE the_geom && setsrid(
 	                  'BOX3D(".($lonlat[0]-200)."
 	                         ".($lonlat[1]-200).",
 	                         ".($lonlat[0]+200)."
 	                         ".($lonlat[1]+200).")'::box3d, 900913)
 	            ORDER BY dist LIMIT 1";
 	     	
 	    $query = pg_query($con,$sql); 
 	    
 	    $edge['gid']      = pg_fetch_result($query, 0, 0); 
 	    $edge['source']   = pg_fetch_result($query, 0, 1); 
 	    $edge['target']   = pg_fetch_result($query, 0, 2); 
 	    $edge['the_geom'] = pg_fetch_result($query, 0, 3); 
 	
 	    // Close database connection
 	    pg_close($con);
 	
 	    return $edge;
 	  }
 	
 	  // Select the routing algorithm
 	   switch($_REQUEST['method']) {
 	
 	 	
 	    case 'SPD' : // Shortest Path Dijkstra
 	      $sql = "SELECT rt.gid, AsText(rt.the_geom) AS wkt,
 	                   length(rt.the_geom) AS length, ".TABLE.".id
 	                FROM ".TABLE.",
 	                    (SELECT gid, the_geom
 	                        FROM dijkstra_sp_delta(
 	                            '".TABLE."',
 	                            ".$startEdge['source'].",
 	                            ".$endEdge['target'].",
 	                            3000)
 	                     ) as rt
 	                WHERE ".TABLE.".gid=rt.gid;";
 	                
 	
 	                
 	      break;
 	
 	    case 'SPA' : // Shortest Path A*
 	
 	      $sql = "SELECT rt.gid, AsText(rt.the_geom) AS wkt,
 	                     length(rt.the_geom) AS length, ".TABLE.".id
 	                  FROM ".TABLE.",
 	                      (SELECT gid, the_geom
 	                          FROM astar_sp_delta(
 	                              '".TABLE."',
 	                              ".$startEdge['source'].",
 	                              ".$endEdge['target'].",
 	                              3000)
 	                       ) as rt
 	                  WHERE ".TABLE.".gid=rt.gid;"; 
	      break;
 	
 	    case 'SPS' : // Shortest Path Shooting*
 	
 	      $sql = "SELECT rt.gid, AsText(rt.the_geom) AS wkt,
 	                     length(rt.the_geom) AS length, ".TABLE.".id
 	                  FROM ".TABLE.",
 	                      (SELECT gid, the_geom
 	                          FROM shootingstar_sp(
 	                              '".TABLE."',
 	                              ".$startEdge['gid'].",
 	                              ".$endEdge['gid'].",
 	                              3000, 'length', false, false)
 	                       ) as rt
 	                  WHERE ".TABLE.".gid=rt.gid;";
 	      break; 
 	
 	  } // close switch
 	
 	   //  echo $sql;
 	  // Database connection and query
 	  $dbcon = pg_connect("dbname=".PG_DB." host=".PG_HOST." password=".PG_PASS." user=".PG_USER);
 	
 	  $query = pg_query($dbcon,$sql);
 	 
 	  // Return route as XML
	 
 	  $xml  = '<' . '?xml version="1.0" encoding="UTF-8" standalone="yes" ?'.'>'."\n";
 	  $xml .= "<route>\n";
 	 
 	  // Add edges to XML file
 	  while($edge=pg_fetch_assoc($query)) { 
 	
 	    $pathlength += $edge['length'];
 	
 	    $xml .= "\t<edge id='".++$counter."'>\n";
 	    $xml .= "\t\t<id>".$edge['id']."</id>\n";
 	    $xml .= "\t\t<wkt>".$edge['wkt']."</wkt>\n";
 	    $xml .= "\t\t<length>".round(($pathlength/1000),3)."</length>\n";
 	    $xml .= "\t</edge>\n";
 	  }
 	 
 	  $xml .= "</route>\n";
 	        
 	  // Close database connection
 	  pg_close($dbcon);
 	  // Return routing result
 	  header('Content-type: text/xml',true);
 	  echo $xml;
 	 
	?>

Open in new window

strange. Try:
<?php
 	ini_set('short_open_tag','Off');
		
 	  // Database connection settings
 	  define("PG_DB"  , "routing");
 	  define("PG_HOST", "localhost");
	  define("PG_PASS", "postgres");
 	  define("PG_USER", "postgres");
 	  define("PG_PORT", "5432");
 	  define("TABLE",   "ways");
 	   	
 	  $counter = $pathlength = 0;
 	
 	  // Retrieve start point
 	 $start = split(' ',$_REQUEST['startpoint']);
 	
 	  $startPoint = array($start[0], $start[1]);
 	  	 
 	  // Retrieve end point
 	  $end = split(' ',$_REQUEST['finalpoint']);
 	
 	 $endPoint = array($end[0], $end[1]);
 	 
 	  // Find the nearest edge
 	  $startEdge = findNearestEdge($startPoint);
 	 
 	
 	  $endEdge   = findNearestEdge($endPoint);
 	
 	  // FUNCTION findNearestEdge
 	  function findNearestEdge($lonlat) {
 	    
 	    // Connect to database
 	    $con = pg_connect("dbname=".PG_DB." host=".PG_HOST." password=".PG_PASS." user=".PG_USER);
 	    
 	    $sql = "SELECT gid, source, target, the_geom,
 	             distance(the_geom, GeometryFromText(
 	                  'POINT(".$lonlat[0]." ".$lonlat[1].")', 900913)) AS dist
 	            FROM ".TABLE." 
 	            WHERE the_geom && setsrid(
 	                  'BOX3D(".($lonlat[0]-200)."
 	                         ".($lonlat[1]-200).",
 	                         ".($lonlat[0]+200)."
 	                         ".($lonlat[1]+200).")'::box3d, 900913)
 	            ORDER BY dist LIMIT 1";
 	     	
 	    $query = pg_query($con,$sql); 
 	    
 	    $edge['gid']      = pg_fetch_result($query, 0, 0); 
 	    $edge['source']   = pg_fetch_result($query, 0, 1); 
 	    $edge['target']   = pg_fetch_result($query, 0, 2); 
 	    $edge['the_geom'] = pg_fetch_result($query, 0, 3); 
 	
 	    // Close database connection
 	    pg_close($con);
 	
 	    return $edge;
 	  }
 	
 	  // Select the routing algorithm
 	   switch($_REQUEST['method']) {
 	
 	 	
 	    case 'SPD' : // Shortest Path Dijkstra
 	      $sql = "SELECT rt.gid, AsText(rt.the_geom) AS wkt,
 	                   length(rt.the_geom) AS length, ".TABLE.".id
 	                FROM ".TABLE.",
 	                    (SELECT gid, the_geom
 	                        FROM dijkstra_sp_delta(
 	                            '".TABLE."',
 	                            ".$startEdge['source'].",
 	                            ".$endEdge['target'].",
 	                            3000)
 	                     ) as rt
 	                WHERE ".TABLE.".gid=rt.gid;";
 	                
 	
 	                
 	      break;
 	
 	    case 'SPA' : // Shortest Path A*
 	
 	      $sql = "SELECT rt.gid, AsText(rt.the_geom) AS wkt,
 	                     length(rt.the_geom) AS length, ".TABLE.".id
 	                  FROM ".TABLE.",
 	                      (SELECT gid, the_geom
 	                          FROM astar_sp_delta(
 	                              '".TABLE."',
 	                              ".$startEdge['source'].",
 	                              ".$endEdge['target'].",
 	                              3000)
 	                       ) as rt
 	                  WHERE ".TABLE.".gid=rt.gid;"; 
	      break;
 	
 	    case 'SPS' : // Shortest Path Shooting*
 	
 	      $sql = "SELECT rt.gid, AsText(rt.the_geom) AS wkt,
 	                     length(rt.the_geom) AS length, ".TABLE.".id
 	                  FROM ".TABLE.",
 	                      (SELECT gid, the_geom
 	                          FROM shootingstar_sp(
 	                              '".TABLE."',
 	                              ".$startEdge['gid'].",
 	                              ".$endEdge['gid'].",
 	                              3000, 'length', false, false)
 	                       ) as rt
 	                  WHERE ".TABLE.".gid=rt.gid;";
 	      break; 
 	
 	  } // close switch
 	
 	   //  echo $sql;
 	  // Database connection and query
 	  $dbcon = pg_connect("dbname=".PG_DB." host=".PG_HOST." password=".PG_PASS." user=".PG_USER);
 	
 	  $query = pg_query($dbcon,$sql);
 	 
 	  // Return route as XML

 	  $xml = "<route>\n";
 	 
 	  // Add edges to XML file
 	  while($edge=pg_fetch_assoc($query)) { 
 	
 	    $pathlength += $edge['length'];
 	
 	    $xml .= "\t<edge id='".++$counter."'>\n";
 	    $xml .= "\t\t<id>".$edge['id']."</id>\n";
 	    $xml .= "\t\t<wkt>".$edge['wkt']."</wkt>\n";
 	    $xml .= "\t\t<length>".round(($pathlength/1000),3)."</length>\n";
 	    $xml .= "\t</edge>\n";
 	  }
 	 
 	  $xml .= "</route>\n";
 	        
 	  // Close database connection
 	  pg_close($dbcon);
 	  // Return routing result
 	  header('Content-type: text/xml',true);
 	  echo  '<' . '?xml version="1.0" encoding="UTF-8" standalone="yes" ?'.'>'."\n".$xml;
 	 exit;
	?>

Open in new window

Avatar of billy21

ASKER

That results in this...

\n"; // Add edges to XML file while($edge=pg_fetch_assoc($query)) { $pathlength += $edge['length']; $xml .= "\t\n"; $xml .= "\t\t".$edge['id']."\n"; $xml .= "\t\t".$edge['wkt']."\n"; $xml .= "\t\t".round(($pathlength/1000),3)."\n"; $xml .= "\t\n"; } $xml .= "\n"; // Close database connection pg_close($dbcon); // Return routing result header('Content-type: text/xml',true); echo '<' . '?xml version="1.0" encoding="UTF-8" standalone="yes" ?'.'>'."\n".$xml; exit; ?>
on the following lines:
         $dbcon = pg_connect("dbname=".PG_DB." host=".PG_HOST." password=".PG_PASS." user=".PG_USER);
       
         $query = pg_query($dbcon,$sql);

how do you know the queries succeeded?

Try:
$dbcon = pg_connect("dbname=".PG_DB." host=".PG_HOST." password=".PG_PASS." user=".PG_USER) or die("Error connection to db");
       
$query = pg_query($dbcon,$sql) or die("Error executing query");
Avatar of billy21

ASKER

Hi Hielo,

The problem turned out to be an issue with my web server setup.  It wasn't recognising the php code as anything other than text and thus was displaying it to screen.

Had I not had this issue it still would not have worked due to the issues you have highlighted.  Thanks for your help on this and sorry for the delay it just took some time for me to figure out the web server issues.