Connecting Tech Pros Worldwide Forums | Help | Site Map

sorting query results into groups

guitarromantic@gmail.com
Guest
 
Posts: n/a
#1: Sep 6 '05
Hey,

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.

I've attempted this using a query for each possible status, which I
figure was pretty inefficient (plus it didn't work properly). The best
I could do manually was to just display a list of staff and their
statuses sorted in order of the title ranks.

How can I do this efficiently?

-Matt


Marcin Dobrucki
Guest
 
Posts: n/a
#2: Sep 6 '05

re: sorting query results into groups


guitarromantic@gmail.com wrote:
[color=blue]
> 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.
>
> I've attempted this using a query for each possible status, which I
> figure was pretty inefficient (plus it didn't work properly). The best
> I could do manually was to just display a list of staff and their
> statuses sorted in order of the title ranks.
>
> How can I do this efficiently?[/color]

How about (skipping the fetch from the db, assuming you have a result
already):

while ($row =& $res->fetchRow()) {
$staff[$row['title']][] = $row['name'];
}


This should create something like this:

array ( "Secretary" => array (0 => "Lolli Pop",
1 => "Jonsey Jones"
3 => "Bob Bored"),
"Editor in Chief" => array (0 => "Big Fish",
1 => "Jack Sardine"),
...);

Well, you catch the drift.

/Marcin
Steve
Guest
 
Posts: n/a
#3: Sep 6 '05

re: sorting query results into groups


[color=blue]
> 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.[/color]

You need a query that joins the two tables together. You haven't shown
the table definitions but it would look something like this:

SELECT staff.name, statuses.title
FROM staff, statuses
WHERE staff.statusid = statuses.statusid
ORDER BY statuses.rank

---
Steve

guitarromantic@gmail.com
Guest
 
Posts: n/a
#4: Sep 6 '05

re: sorting query results into groups


Sorry, I should have specified: I already have a join (inner) that does
that. My problem is actually displaying the data in groups.

Marcin: Could you explain a little of what's going on there? I'm pretty
new to all of this. Thanks!

Marcin Dobrucki
Guest
 
Posts: n/a
#5: Sep 6 '05

re: sorting query results into groups


guitarromantic@gmail.com wrote:[color=blue]
> Sorry, I should have specified: I already have a join (inner) that does
> that. My problem is actually displaying the data in groups.
>
> Marcin: Could you explain a little of what's going on there? I'm pretty
> new to all of this. Thanks![/color]

Jeps... you are creating a two dimentional hash. So, whereas in a
one-dimentional hash you would have:

array (hash => value,
hash2 => value2,
...);

you now have:

array (hash => array (hash => value,
hash2 => value2...),
haash => array (hash3 => value3
...

The foo[] construct is a shorthand for pushing onto an array (similar
to array_push()), but without the ill side-effect of renumbering indexes
(if that's important).

So, basically what I suggested is that when you process your joined
query result, you create a hash with key values equal to the 'status',
and the value an array of names in that status. You can then "foreach"
on that, to parse it out into some table, for instance like this (see
HTML_Table on the table genrating stuff):

<?php
require_once ('HTML/Table.php');
$staff = array ('Editors in Chief' =>
array (array('John', 'Doe'),
array('Bob', 'Edams'), array('Iggy', 'Pop')),
'Copywriters' =>
array(array('Joe', 'Little'), array('Billy', 'Jean'),
array('Xena', 'Warrior')));

$t = new HTML_Table(array('border' => 1));
foreach($staff as $status => $folks) {
$t->addRow(array($status),
array('colspan' => 2),
'TH');
foreach ($folks as $name) {
$t->addRow($name);
}
}
$t->display();
?>

This will create the following HTML:
<table border="1">
<tr>
<th colspan="2">Editors in Chief</th>
<!-- span -->
</tr>
<tr>
<td>John</td>
<td>Doe</td>

</tr>
<tr>
<td>Bob</td>
<td>Edams</td>
</tr>
<tr>
<td>Iggy</td>

<td>Pop</td>
</tr>
<tr>
<th colspan="2">Copywriters</th>
<!-- span -->
</tr>
<tr>
<td>Joe</td>

<td>Little</td>
</tr>
<tr>
<td>Billy</td>
<td>Jean</td>
</tr>
<tr>

<td>Xena</td>
<td>Warrior</td>
</tr>
</table>

The benefit here was that you only need to perform one query on the DB
(to fetch the names/status), and you don't really need to sort it by the
"status" field because that will be done during the construction of the
hash.

/Marcin
NC
Guest
 
Posts: n/a
#6: Sep 6 '05

re: sorting query results into groups


guitarroman...@gmail.com wrote:[color=blue]
>
> 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.[/color]
....[color=blue]
> How can I do this efficiently?[/color]

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

guitarromantic@gmail.com
Guest
 
Posts: n/a
#7: Sep 7 '05

re: sorting query results into groups


NC,

That worked beautifully, thank you a ton!

katreid69002@yahoo.fr
Guest
 
Posts: n/a
#8: Sep 8 '05

re: sorting query results into groups



guitarromantic@gmail.com a écrit :
[color=blue]
> NC,
>
> That worked beautifully, thank you a ton![/color]

I'm really interested i have something a bit similar to deal with
(exept i'm a total beginner)
Could you send me a 'create table' instruction that i shall use ?

guitarromantic@gmail.com
Guest
 
Posts: n/a
#9: Sep 9 '05

re: sorting query results into groups


hey,

Mine is pretty specific to my site, but here's what I'm using:

CREATE TABLE `staff` (
`Staff_id` int(11) NOT NULL auto_increment,
`username` text NOT NULL,
`Staff_Email` text NOT NULL,
`password` text NOT NULL,
`status` text NOT NULL,
`profile` text NOT NULL,
PRIMARY KEY (`Staff_id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=75 ;

Password is stored as md5. Status is a number corresponding to the
'statuses' table (contains two columns, 'id' and 'title'). Profile is
just html code for that staff member's personal info.

Closed Thread