I'm new to php.I'm reading a RSS feed and store in my database table. for this I'm using

$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;
}

how can display all data with foreach loop?

有帮助吗?

解决方案

 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;
 }

其他提示

You can just echo out the result of your statements (I've also changed it to a foreach as requested):

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;
}

This should work:

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;
}
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;
}

I have echoed them because I think this is what you are wanting to do - your code reassigns variables on each iteration but does nothing with them. They are overwritten each time, so they will only hold the values from the last iteration at the end.

EDIT

You can easily modify this to insert rows into your database by doing something like this:

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();
}

Alternatively, you can turn it all into one query, to minimise database traffic:

$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();
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top