Re: Group sort syntax? [message #169796 is a reply to message #169795] |
Mon, 27 September 2010 05:50 |
Denis McMahon
Messages: 634 Registered: September 2010
Karma:
|
Senior Member |
|
|
On 27/09/10 05:27, GarryJones wrote:
> I have the following table from which I am reading with php.
>
> ItemNumber, ItemName
> 349, bat
> 346, bat
> 346, bun
> 349, ball
> 417, hutch
> 350, jib
> 349, bridge
> 350, jig
> 346, ball
> 350, poll
> 350, numb
>
> The Item numbers that someone wants to see are read into an array
> called items.
>
> If the array "items" contains is 346,349,350 (ie someone wants to see
> everything except for item number 417
>
> I need to be able to extract this data and group the items into a
> table with table headings based on each value in the array
>
> I think this gets the data for me
>
> $sql = 'SELECT * FROM resmal
> WHERE ItemNumber IN (' . implode(', ', $items) . ')';
> $test=mysql_query($sql);
>
> But how do I obtain this following result? I am unsure of the syntax.
> I know how to write the html data but I need to be able to fetch the
> data for each different item number in turn and group them,
perhaps add an 'ORDER BY ItemNumber,ItemName' clause to your select?
then, in your php, I think this (not tested):
<?php
$ItemNumber = -1; // assumes that this will never be -1 in database
$firsta = true;
$firstb = true;
echo "<p>";
while ($row = mysql_fetch_assoc($test)) {
if ($firsta) { // only want '<br>'s before elements
// other than the first one
$firsta = false;
}
else {
echo "<br>";
}
if ($ItemNumber != $row['ItemNumber']) // new number grouping
if ($firstb) { // only want '<br>'s before elements
// other than the first one
$firstb = false;
}
else {
echo "<br>";
}
$ItemNumber = $row['ItemNumber'];
echo "ITEM NUMBER {$row['ItemNumber']}<br>";
}
echo "{$row['ItemName']}";
}
echo </p>
?>
Rgds
Denis McMahon
|
|
|