Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

PHP/XML problem

Options
  • 13-04-2010 7:13pm
    #1
    Registered Users Posts: 86 ✭✭


    I am currently trying to use php to access a postgres database, return a resultset which is then used to create an example file. However, when I run my php file I get this error: Parse error: syntax error, unexpected T_DNUMBER in C:\xampp\xampp\htdocs\routing.php on line 133 I don't know how to fix this. Any help? Cheers!

    <?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 = array($start[0], $start[1]);





    // Retrieve end point
    $end = split(' ',$_REQUEST);

    $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 = pg_fetch_result($query, 0, 0);
    $edge = pg_fetch_result($query, 0, 1);
    $edge = pg_fetch_result($query, 0, 2);
    $edge = pg_fetch_result($query, 0, 3);

    // Close database connection
    pg_close($con);

    return $edge;
    }

    // Select the routing algorithm
    switch($_REQUEST) {





    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.",
    ".$endEdge.",
    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.",
    ".$endEdge.",
    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.",
    ".$endEdge.",
    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;

    $xml .= "\t<edge id='".++$counter."'>\n";
    $xml .= "\t\t<id>".$edge."</id>\n";
    $xml .= "\t\t<wkt>".$edge."</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;

    ?>


Comments

  • Closed Accounts Posts: 7,145 ✭✭✭DonkeyStyle \o/


    You need to escape those double quotes if they're supposed to be quotes inside a double-quote-enclosed string.

    Like so...
    $xml = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\" ?>"."\n";
    Otherwise you're just opening and closing the string every time you use a double quote.
    Or better yet use single quotes, so php doesn't try to parse variables in your string.
    $xml = '<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>'.'\n';


  • Registered Users Posts: 86 ✭✭NerfNerder


    Thanks, that sorted it out nicely.


  • Registered Users Posts: 6,509 ✭✭✭daymobrew


    You need to escape those double quotes if they're supposed to be quotes inside a double-quote-enclosed string.

    Like so...

    Otherwise you're just opening and closing the string every time you use a double quote.
    Or better yet use single quotes, so php doesn't try to parse variables in your string.
    I've been using sprintf a lot recently. Here is an overly complicated and untested example:
    [PHP]
    $version = '1.0';
    $encoding = 'UTF-8';
    $standalone = 'yes';
    $xml = sprintf( '<?xml version="%s" encoding="%s" standalone="%s" ?>%s", $version, $encoding, $standalone, "\n");
    [/PHP] (I know that PHP might render $version as '1'; using %.1f would fix that.)


  • Registered Users Posts: 86 ✭✭NerfNerder


    Thanks guys I got it working just about. However I have one more question if anyone might be able to answer. It all works fine, except for this being printed when I run the code in my browser: This XML file does not appear to have any style information associated with it. The document tree is shown below.
    I then see my xml printed on the screen. Any ideas why this is happening. Here is my code just incase you need to see:

    <?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");

    //http://localhost:1980/routing2/ol_osm_routing/routing.php?startpoint=-720023.40608569%207055317.3361107&finalpoint=%22-722698.7020752%207058909.8764395%22

    $counter = $pathlength = 0;

    // Retrieve start point
    $start = split(' ',$_REQUEST);

    $startPoint = array($start[0], $start[1]);



    // Retrieve end point
    $end = split(' ',$_REQUEST);

    $endPoint = array($end[0], $end[1]);



    // Find the nearest edge
    $startEdge = findNearestEdge($startPoint);


    $endEdge = findNearestEdge($endPoint);

    // FUNCTION findNearestEdge
    function findNearestEdge($lonlat) {

    // Connect to database
    $dbcon = pg_connect("dbname=".PG_DB." host=".PG_HOST." password=".PG_PASS." user=".PG_USER);

    $sql = "SELECT gid, source, target, the_geom_4326,
    distance(the_geom_4326, GeometryFromText(
    'POINT(".$lonlat[0]." ".$lonlat[1].")', 4326)) AS dist
    FROM ".TABLE."
    WHERE the_geom_4326 && setsrid(
    'BOX3D(".($lonlat[0]-200)."
    ".($lonlat[1]-200).",
    ".($lonlat[0]+200)."
    ".($lonlat[1]+200).")'::box3d, 4326)
    ORDER BY dist LIMIT 1;";


    //echo $sql;

    $query = pg_query($dbcon,$sql);

    $edge = pg_fetch_result($query, 0, 0);
    $edge = pg_fetch_result($query, 0, 1);
    $edge = pg_fetch_result($query, 0, 2);
    $edge = pg_fetch_result($query, 0, 3);

    // Close database connection
    pg_close($dbcon);

    return $edge;
    }

    // Select the routing algorithm
    switch($_REQUEST) {

    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.",
    ".$endEdge.",
    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.",
    ".$endEdge.",
    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.",
    ".$endEdge.",
    3000, 'length', false, false)
    ) as rt
    WHERE ".TABLE.".gid=rt.gid;";
    break;

    } // close switch

    // http://localhost:1980/routing2/ol_osm_routing/routing.php?startpoint=-6.45419%2053.40087&finalpoint=-6.44833%2053.40066


    // 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\"?>";
    $xml .= "<route>\n";

    // Add edges to XML file
    while($edge=pg_fetch_assoc($query)) {

    $pathlength += $edge;

    $xml .= "\t<edge id='".++$counter."'>\n";
    $xml .= "\t\t<id>".$edge."</id>\n";
    $xml .= "\t\t<wkt>".$edge."</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 id=\"thisId\">";
    echo $xml;
    ?>


Advertisement