|
Posted by Marcin Dobrucki on 09/06/05 18:27
guitarromantic@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
Navigation:
[Reply to this message]
|