每个日期仅显示一次

发布于 2024-08-07 01:27:32 字数 1804 浏览 2 评论 0原文

好吧,这肯定是一个绝对简单的问题,对此我深表歉意。

如果我只是未能找到正确的搜索词来自行得出答案,我也深表歉意。我确实尝试过,但我对 PHP 的不熟练让我在搜索方面很糟糕。

我正在寻找一种简单的方法来在 foreach 循环中仅显示每个日期一次。我正在像这样循环数据:

<?php
  echo "<ul>";
  foreach($rss_items as $i){
   if($i->get_feed()->get_title() == 'Twitter (no @ replies)'){
   echo "<li>";
   echo $i->get_date();
   echo "<a href='" .$i->get_link()."'>Twitter</a>";
   echo $i->get_title();
   echo "</li>";
   }
   elseif($i->get_feed()->get_title() == 'Pinboard (jpcody)'){
   echo "<li>";
   echo $i->get_date();
   echo "<a href='" .$i->get_link()."'>Pinboard</a>";
   echo $i->get_title();
   echo "</li>";
   }
   elseif($i->get_feed()->get_title() == 'Entries at Church Marketing Sucks by Joshua Cody'){
   echo "<li>";
   echo $i->get_date();
   echo "<a href='" .$i->get_link()."'>Church Marketing Sucks</a>";
   echo "<a href='" .$i->get_link()."'>" . $i->get_title() . "</a>";
   echo $i->get_description();
   echo "</li>";
   }
   elseif($i->get_feed()->get_title() == 'Flickr remove first paragraph'){
   echo "<li>";
   echo $i->get_date();
   echo "<a href='" .$i->get_link()."'>Flickr</a>";
   echo "<a href='" .$i->get_link()."'>" . $i->get_title() . "</a>";
   echo $i->get_description();
   echo "</li>";
   }
  }
  echo "</ul>";
 ?>

并且每个项目都包含日期,因此我多次获得相同的日期。我只想让每个日期显示一次,例如 http://daringfireball.net

我正在使用 CodeIgniter 和 SimplePie 库,因此所有数据都是直接提取的,而不是存储在数据库中。我想一种方法可以包括第二个 if 语句来检查日期是否已被使用,但我不知道如何执行它。

我对 PHP 还很陌生,我真的希望了解更多,而不仅仅是给出解决方案。

您能提供的任何帮助都会很棒!

All right, this must be an absolutely easy question, and I apologize for that.

I also apologize if I simply failed in finding the right search terms to use to come to an answer on my own. I did try, but my lack of fluency in PHP kind of makes me suck at searching.

I'm looking for a simple way to show each date only once within a foreach loop. I'm looping through data like so:

<?php
  echo "<ul>";
  foreach($rss_items as $i){
   if($i->get_feed()->get_title() == 'Twitter (no @ replies)'){
   echo "<li>";
   echo $i->get_date();
   echo "<a href='" .$i->get_link()."'>Twitter</a>";
   echo $i->get_title();
   echo "</li>";
   }
   elseif($i->get_feed()->get_title() == 'Pinboard (jpcody)'){
   echo "<li>";
   echo $i->get_date();
   echo "<a href='" .$i->get_link()."'>Pinboard</a>";
   echo $i->get_title();
   echo "</li>";
   }
   elseif($i->get_feed()->get_title() == 'Entries at Church Marketing Sucks by Joshua Cody'){
   echo "<li>";
   echo $i->get_date();
   echo "<a href='" .$i->get_link()."'>Church Marketing Sucks</a>";
   echo "<a href='" .$i->get_link()."'>" . $i->get_title() . "</a>";
   echo $i->get_description();
   echo "</li>";
   }
   elseif($i->get_feed()->get_title() == 'Flickr remove first paragraph'){
   echo "<li>";
   echo $i->get_date();
   echo "<a href='" .$i->get_link()."'>Flickr</a>";
   echo "<a href='" .$i->get_link()."'>" . $i->get_title() . "</a>";
   echo $i->get_description();
   echo "</li>";
   }
  }
  echo "</ul>";
 ?>

And each item contains the date, so I'm getting the same date multiple times. I'd like to only have each date shown once, a la http://daringfireball.net.

I'm using CodeIgniter and the SimplePie library, so all of the data is being pulled directly instead of being stored in a db. I imagine a way to do it could be including a second if statement to check if the date has already been used, but I don't know how to execute this.

I'm pretty new to PHP, and I'm really looking to learn more than just have a solution given.

Any help you could give would be great!

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

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

发布评论

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

评论(2

掩耳倾听 2024-08-14 01:27:32

您需要记住上次使用的日期是什么,只有在不同时才打印它。您可以尝试以下操作:(

$previous_date = null;
foreach ($rss_items as $item) {
    if ($item->get_date() != $previous_date) {
        $previous_date = $item->get_date();
        echo '<li>' . $previous_date . '</li>';
    }
    ...
}

并且不要忘记使用 htmlspecialchars 对标题和链接进行 HTML 编码。)

You need to remember what was the date you used last, and print it only if it differs. You can try something like:

$previous_date = null;
foreach ($rss_items as $item) {
    if ($item->get_date() != $previous_date) {
        $previous_date = $item->get_date();
        echo '<li>' . $previous_date . '</li>';
    }
    ...
}

(And don't forget to HTML-encode the titles and links using htmlspecialchars.)

羁拥 2024-08-14 01:27:32

您的意思是您只想在循环之前或每个循环中显示一次日期,以便它看起来像:

日期 1
- 第 1 项
- 第 2 项
- 等等...

日期 2
- 第 1 项
- 第 2 项
- 等等...

你能澄清一下格式吗?因为目前每个有效的 $i 都应该显示一次日期。

Do you mean you only want the date to be shown once, before the loop, or once per loop so that it looks like:

Date 1
- item 1
- item 2
- etc...

Date 2
- item 1
- item 2
- etc...

Could you clarify the format? Cause at the moment the date should be shown once for each $i that is valid.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文