views:

106

answers:

2

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!

A: 

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.

Zolomon
My apologies on this. You're correct in your understanding. I would like the date to display once, and each item for that day underneath without a date repeat.
Joshua Cody
Try Lukas's answer, if that doesn't work you can try running two foreach loops, one that sets the appropriate date first and then the second one that fills in the respective content. (Not very pretty, but it should work) Hope it works out!
Zolomon
+1  A: 

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.)

Lukáš Lalinský
Thanks so much Lukas, I'm off to bed now, but I'll try this tomorrow and be sure to give appropriate credit if it works for me or ask a follow-up if I'm too dense to get it still. Thanks again!
Joshua Cody
Hey Lukas, this worked! Am I understanding how it works correctly: (1) Defines empty variable so it can be used and the first date will be displayed. (2) If the list item's date doesn't equal the var, then we set previous_date to the current date and echo the current date. (3) The next item checks to see if its own get_date equals the recently stored variable, and if so, it does not echo its date. Is previous_date a bit of a misnomer as the current item's date gets stored to this variable?
Joshua Cody
Depends on how you look at it. :) Yes, the current date is being stored to the variable, but every time you compare to it, it contains the value from a previous iteration of the loop.
Lukáš Lalinský