473,412 Members | 2,262 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,412 software developers and data experts.

sorting query results into groups

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

Sep 6 '05 #1
8 1889
gu************@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.

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?


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
Sep 6 '05 #2
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.


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

Sep 6 '05 #3
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!

Sep 6 '05 #4
gu************@gmail.com wrote:
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!


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
Sep 6 '05 #5
NC
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

Sep 6 '05 #6
NC,

That worked beautifully, thank you a ton!

Sep 7 '05 #7

gu************@gmail.com a écrit :
NC,

That worked beautifully, thank you a ton!


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 ?

Sep 8 '05 #8
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.

Sep 9 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Guy Erez | last post by:
Hi, I'm running queries with MySql 4.0.17 that return thousands of records. Because I need to present them in GUI, I returieve the results in chunks using LIMIT, for example - get first 100,...
6
by: Brian | last post by:
Hello, Basically, I'm running a query on a form's activation, and I'd like to have the results of the query be placed into other fields on the same form automatically. Does anybody know how...
0
by: Rob | last post by:
I doubt this is the best way to do it, but what I came up with was to hide the XML in an HTML Comment then edit the file deleting the HTML stuff and keep the XML results. If anyone has a better...
9
by: Kelii | last post by:
I've been trying to get this piece to work for a few hours, but have given up. I hope someone out there can help, I think the issue is relatively straightforward, but being a novice, I'm stumped....
6
by: tom_b | last post by:
Hi, I'm using Mysql and PHP,I have a simple table in my DB, looks something like this: id jim bob bill 1 20 30 15 2 20 35 15 etc, what i need to do is get the sums...
2
by: Himmel | last post by:
Hello! The reference database I currently use runs queries that pull data from hundreds of tables in order to create user-friendly form view. The problem is that these queries can take upwards of...
1
by: igor221189 | last post by:
Hello everyone. I have Access 2000 database which holds student records in the school.It stores subject grades for each student.In the 'Student Grade Form', I would like to search student surname...
1
by: mcfly1204 | last post by:
I am generating an XML document using C#, and I have a question on how to populate the value of a particular object with the corresponding value from a SQL query. For example, if I have a query...
1
by: Dave Mallett | last post by:
very new to Access. Trying to export query results via macro and transfertext, but keep getting error message stating "Microsoft Jet Engine cannot find the object 'HRQ-DM_Prd1_qtr.txt'. Make sure...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.