foreach ($movies->channel->item as $item) {
echo $item->title.'<br />';
echo $item->link.'<br />';
echo $item->medium_image_url.'<br />';
echo $item->description.'<br />';
echo $item->city;
}
これがあなたがやりたいことだと思うので、それらをエコーしました-コードは反復ごとに変数を再割り当てしますが、それらに対して何もしません。それらは毎回上書きされるため、最後の反復からの値のみが最後に保持されます。
編集
これを簡単に変更して、データベースに行を挿入するには、次のようにします。
foreach ($movies->channel->item as $item) {
echo $item->title.'<br />';
echo $item->link.'<br />';
echo $item->medium_image_url.'<br />';
echo $item->description.'<br />';
echo $item->city.'<br />';
// Build the query - change the names of your table and columns as appropriate
$query = "INSERT INTO `tablename`
(`title`,`link`,`medium_image_url`,`description`,`city`)
VALUES
('$item->title','$item->link','$item->medium_image_url','$item->description','$item->city')";
// Do the query - do NOT show the output of mysql_error() in a production environment!
if (!mysql_query($query)) echo 'Oh no! Something went wrong with the query: '.mysql_error();
}
または、データベース トラフィックを最小限に抑えるために、すべてを 1 つのクエリに変換することもできます。
$rows = array();
foreach ($movies->channel->item as $item) {
echo $item->title.'<br />';
echo $item->link.'<br />';
echo $item->medium_image_url.'<br />';
echo $item->description.'<br />';
echo $item->city.'<br />';
// Add this entry to $rows
$rows[] = "('$item->title','$item->link','$item->medium_image_url','$item->description','$item->city')";
}
// Build the query - change the names of your table and columns as appropriate
$query = "INSERT INTO `tablename`
(`title`,`link`,`medium_image_url`,`description`,`city`)
VALUES ".implode(', ',$rows);
// Do the query - do NOT show the output of mysql_error() in a production environment!
if (!mysql_query($query)) echo 'Oh no! Something went wrong with the query: '.mysql_error();