Page 1 of 1

loop but display once

Posted: Tue Oct 12, 2004 4:41 am
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?

Posted: Tue Oct 12, 2004 6:02 am
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

Posted: Sun Oct 17, 2004 6:25 pm
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).

Posted: Mon Oct 18, 2004 10:47 am
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;
          }
     }
}