You are here: Re: sorting query results into groups « PHP Programming Language « IT news, forums, messages
Re: sorting query results into groups

Posted by NC on 09/06/05 20:58

guitarroman...@gmail.com wrote:
>
> I have a 'staff' table with a column 'status', corresponding to a
> 'statuses' table with the appropriate titles (eg 1 | Editor in Chief).
> I want to display on the page the headings (the titles from statuses)
> along with all of the staff members with that status ID.
....
> How can I do this efficiently?

Something like this should work (since I don't know your field
naming scheme, I had to invent my own field names):

$query = <<<EOQ
SELECT
staff.firstname AS firstname,
staff.lastname AS lastname,
staff.status AS status,
statuses.title AS title
FROM staff LEFT JOIN statuses ON staff.status = statuses.id
ORDER BY status
EOQ;
$result = mysql_query($query)
or die('Could not execute query: ' . mysql_error());
$status = null;
while ($record = mysql_fetch_array($result, MYSQL_ASSOC)) {
if ($record['status'] <> $status) {
$title = $record['title'];
echo "<p>Status: $title</p>\r\n";
$status = $record['status'];
}
$name = $record['firstname'] . ' ' . $record['lastname'];
echo "<p style='padding-right:10px'>$name</p>\r\n";
}

Cheers,
NC

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация