FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » Group sort syntax?
Show: Today's Messages :: Polls :: Message Navigator
Return to the default flat view Create a new topic Submit Reply
Re: Group sort syntax? [message #169796 is a reply to message #169795] Mon, 27 September 2010 05:50 Go to previous messageGo to previous message
Denis McMahon is currently offline  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
[Message index]
 
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: Stats comp.lang.php (last 7 days)
Next Topic: mysql_fetch_array
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Mon Oct 21 04:28:33 GMT 2024

Total time taken to generate the page: 0.04597 seconds