更改 foreach 中的 for 循环

发布于 2024-12-13 14:30:19 字数 532 浏览 0 评论 0原文

我是 php 新手。我正在阅读 RSS 提要并将其存储在我的数据库表中。 为此,我正在使用

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

如何使用 foreach 循环显示所有数据?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

逆流 2024-12-20 14:30:19
 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;
 }
 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;
 }
好菇凉咱不稀罕他 2024-12-20 14:30:19

您可以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;
}

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;
}
锦上情书 2024-12-20 14:30:19

这应该有效:

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

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;
}
属性 2024-12-20 14:30:19
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();
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();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文