2011-11-03 4 views
0

저는 PHP를 처음 사용합니다. RSS 피드를 읽고 데이터베이스 테이블에 저장하고 있습니다. 이것에 대한 어떻게 foreach 루프 모든 데이터를 표시 할 수 있습니다for foreach 루프 변경

$num = count($movies->channel->item); 
for ($i=0; $i<=$num-1; $i++){ 
    $tittle= $movies->channel->item[$i]->title."<br/>"; 
    $link=$movies->channel->item[$i]->link."<br/>"; 
    $image=$movies->channel->item[$i]->medium_image_url."<br/>"; 
    $des=$movies->channel->item[$i]->description."<br/>"; 
    $city=$movies->channel->item[$i]->city; 
} 

을 사용하고 있습니다?

답변

1
foreach($movies->channel->item as $opt){ 
    echo $tittle= $opt->title."<br/>"; 
    echo $link=$opt->link."<br/>"; 
    echo $image=$opt->medium_image_url."<br/>"; 
    echo $des=$opt->description."<br/>"; 
    echo $city=$opt->city; 
} 
+0

'$ city' 라인은 편집이 필요합니다 ('-> item [$ i]'를 제거하십시오.) 잠재적으로'$ num' 변수는 더 이상 필요하지 않습니다. – Benjie

+0

흠 .. 당신 말이 맞아 .. –

+0

도와 주셔서 고마워요 – omnath

1

할 수 있습니다 단지 echo 명령문의 결과 (요청에 따라 나는 또한 foreach로 변경했습니다) 아웃 : 나는이 생각하기 때문에

foreach ($movies->channel->item as $item) { 
    $tittle= $item->title."<br/>"; 
    $link=$item->link."<br/>"; 
    $image=$item->medium_image_url."<br/>"; 
    $des=$item->description."<br/>"; 
    $city=$item->city; 

    echo $tittle.$link.$image.$des.$city; 
} 
1
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(); 
} 

을 :

$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(); 
+0

각하. 하지만 지금은 데이터베이스 테이블에 모든 데이터를 저장하려고합니다. – omnath

+0

어떤 데이터베이스/드라이버를 사용하고 있습니까? – DaveRandom

+0

mysql을 사용하고 있습니다 – omnath

1

이 작동합니다 :

foreach ($movies as $movie) 
    $tittle = $movie->title."<br/>"; 
    $link = $movies->link."<br/>"; 
    $image = $movie->medium_image_url."<br/>"; 
    $des = $movie->description."<br/>"; 
    $city = $movie->city; 
}