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?
loop but display once
Moderator: General Moderators
-
malcolmboston
- DevNet Resident
- Posts: 1826
- Joined: Tue Nov 18, 2003 1:09 pm
- Location: Middlesbrough, UK
just an idea
very simple, it only prints if I is 1, which it is only once at the start of the script
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";
}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.