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

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]


Удаленная работа для программистов  •  Как заработать на 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

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