loop but display once

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
firestar_lene
Forum Newbie
Posts: 10
Joined: Mon Mar 29, 2004 2:04 am

loop but display once

Post by firestar_lene »

i have one problem.im suppose to display 10 newest announcements.

when it displays,i want some things to be display once,which is company name and date

currently,it displays as below,first line is company name,second line is the date and third line is the headline.company a is repeated as well as the date

company a
15 july 2004
todays special

company b
15 july 2004
today special recipes

company a
15 july 2004
weather is cold

i want the company a to display once if there is repetition and the date display once if there is repetition in date in same company.for example as below:

company a
15 july 2004
todays special
weather is cold


company b
15 july 2004
today special recipes


how to write all this php coding?
malcolmboston
DevNet Resident
Posts: 1826
Joined: Tue Nov 18, 2003 1:09 pm
Location: Middlesbrough, UK

Post by malcolmboston »

just an idea

Code: Select all

$count = count($array_with_values)
if ($count >= 1)
{
// run it
for ($i = 1; $i <= $count; $i++)
{
     if ($i == 1)
     {
          print "Company Name";
     }
     Print "Other Information";
}
very simple, it only prints if I is 1, which it is only once at the start of the script
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

usually for making repeated info "disappear" we store off the previous values we wish to track, and only print the incoming values if they vary from the stored previous value(s).
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

I just made a reporting system that did something similar. Essentially the logic was as ~feyd mentioned:

Code: Select all

$previous_entries;
//loop through each data row
while($row = $DB->get_row($result))
{
     //in each data row, loop through each field
     foreach($row as $index=>$value)
     {
          //if the value in a field is different
          //from the previous, display and set it.
          if($previous[$index] != $value)
          {
               echo $value;
               $previous[$index] = $value;
          }
     }
}
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply