2012-03-05 4 views
1

分解された緯度と経度の値を繰り返し表示できません。データベース内に1組の(x、y)値のみを返します。分解された値を反復する

以下のコードを参照してください。

<?php 
$feed = file_get_contents("http://api.geograph.org.uk/syndicator.php?key=hfALmNnpQ&i=14102530&page=1&format=media&expand=1&perpage=100"); 
$xml = new SimpleXmlElement($feed); 
foreach ($xml->item as $entry){ 
echo $entry->title; 
echo $entry->description; 
echo $entry->link; 

// use that namespace 
$dc = $entry->children('http://purl.org/dc/elements/1.1/'); 
$georss = $entry->children('http://www.georss.org/georss'); 
echo $entry->children('http://purl.org/dc/elements/1.1/')->creator; 
echo $entry->children('http://www.georss.org/georss')->point 
list($lat,$lng) = explode(' ', $entry->children('http://www.georss.org/georss')->point); 
} 
?> 

次のコードは、データベース操作を示しています

<?php 
require 'table.php'; 

// Opens a connection to a PostgresSQL server 
$connection = pg_connect("dbname=postgis user=postgres password=****"); 


// Execute query   

foreach ($xml->item as $entry) { 

$query = "INSERT INTO geognew(title, link, author, latitude, longitude) VALUES ('" . $entry->title . "', '" . $entry->link . "', '" . $entry->children('http://purl.org/dc/elements/1.1/')->creator . "', '" . $lat . "', '" . $lng . "')"; 

$result = pg_query($query); 
printf ("These values are inserted into the database - %s %s %s", $entry->title, $entry->link, $entry->children('http://purl.org/dc/elements/1.1/')->creator, $lat, $lng); 
} 

pg_close(); 

?> 

おかげ Yemi

+0

ループ内にデータベース操作がないと仮定すると、ループによって処理されたLASTペアであるループ終了後に** 1 **緯度/経度のペアが得られます。 –

+2

'echo'文の出力を表示します。 –

+0

* APIキーを削除する* –

答えて

0

良く、そのあなたは、アレイ内の緯度/経度を保存:いやは意味SimpleXmlElement::children?

を意味しますか。 xmlを何度も何度も反復処理することは過度のものです。

// define this $points outside the foreach loop 
$points = array(); 

foreach(...){ 
.... 
    list($lat,$lng) = explode(' ' 
      , (string)($entry->children('http://www.georss.org/georss')->point)); 
    $points[] = array('lat'=> $lat, 'long' => $lng); 
} 

ここでは、緯度と経度でのみ繰り返す完全なコードです。

$xml = simplexml_load_string($feed); 
$namespaces = $xml->getNameSpaces(true); 
foreach ($xml->item as $entry){ 
    $georss = $entry->children($namespaces['georss']); 
    list($lat,$lng) = explode(' ', (string)$georss->point); 
    echo "$lat, $lng\n"; 
} 
+0

あなたの提案が値を反復しないのではないかと恐れています。同じlat値、lng値のペアを返します。 – akinboj

+0

ここで '$ points'はすべてのlatとlongを保持します。 –

+0

@akinboj私は動作する私の完全なコードを追加しました。 –

0

を私はSimpleXmlElement::item()は、クラスで定義されて表示されていません。

foreach ($xml->children() as $entry) { ... } 
+0

私は完全に理解しているとは思わない – akinboj

+0

$ xml-> itemはSimpleXmlElementクラスの有効な関数ではありません。 – Churk

関連する問題