Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

loop but display once

Status
Not open for further replies.

lenelene

Programmer
Dec 10, 2002
57
0
0
MY
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?



 
All you need to do is to remember the current company and the date and compare it on each new record that's displayed.
Code:
# initialize the vars to remember company and date
$thisCompany = '';
$thisDate = '';
# iterate the results
while ($row = mysql_fetch_assoc($result)){
   # compare the current company
   if ($row['company']!=$thisCompany){
      writeHeader($row['company']);
     # remember the new company and reset date
     $thisCompany = $row['company'];
     $thisDate = '';
   }
   # compare the date
   if ($row['date']!=$thisDate){
      writeHeader($row['date']);
      $thisDate = $row['date'];
   }
   # print the rest of your event here
   print(...);
}
..etc...

I would write a function writeHeader($content) that outputs whatever HTML you want for the header display fo company and/or date. A second parameter could control the styling.
 
make sure to include an order statement in your $sql statement...i.e.
Code:
$sql = 'select * from tablename order by company_name';
 
but i want retrieve the ten most new announcments(retrieve by the id descending)
 
You assume that a higher ID indicates the announcement is newer - that's an assumption which is not backed up by anything in the MySQL documentation.
Therefore I would put in a field with a timestamp when the announcement was posted and order by that:
Code:
$SQL = "SELECT * FROM table ORDER BY posted_time DESC, company";
 
You do not need to ORDER BY timestamp! You can use the unique ID!

$SQL = "SELECT * FROM table ORDER BY announcement_id DESC, company";

Then you do a LIMIT 0, 10

eg:
Code:
$SQL = "SELECT * FROM `table` ORDER BY `announcement_id` DESC, `company` LIMIT 0, 10";

ps. you can also have a variable limit!
eg. you can use the same query for "list all", or "paged browsing", with variables instead of integers in the query!
 
just a small correction:

variables instead of constants!
the variables need to be integers of course, in the limit :p

*slap myself with a some php*
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top