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 #169803 is a reply to message #169796] Mon, 27 September 2010 12:08 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 06:50, Denis McMahon wrote:

If he wants to use ul / li for these items:

This code:

<?php

// -- data and usort cmp for testing starts here --
function cmp($a, $b)
{
if ($a['ItemNumber'] == $b['ItemNumber'] &&
$a['ItemName'] == $b['ItemName']) return 0;
if ($a['ItemNumber'] < $b['ItemNumber']) return -1;
if ($a['ItemNumber'] == $b['ItemNumber'] &&
$a['ItemName'] < $b['ItemName']) return -1;
return 1;
}

// data
$data[] = array('ItemNumber'=> 349, 'ItemName' => 'bat');
$data[] = array('ItemNumber'=> 346, 'ItemName' => 'bat');
$data[] = array('ItemNumber'=> 346, 'ItemName' => 'bun');
$data[] = array('ItemNumber'=> 349, 'ItemName' => 'ball');
$data[] = array('ItemNumber'=> 417, 'ItemName' => 'hutch');
$data[] = array('ItemNumber'=> 350, 'ItemName' => 'jib');
$data[] = array('ItemNumber'=> 349, 'ItemName' => 'bridge');
$data[] = array('ItemNumber'=> 350, 'ItemName' => 'jig');
$data[] = array('ItemNumber'=> 346, 'ItemName' => 'ball');
$data[] = array('ItemNumber'=> 350, 'ItemName' => 'poll');
$data[] = array('ItemNumber'=> 350, 'ItemName' => 'numb');

usort($data, "cmp");

// -- data and usort cmp for testing ends here --

// -- removed sql below for test --
/*
$sql = 'SELECT * FROM resmal
WHERE ItemNumber IN (' . implode(', ', $items) . ')
ORDER BY ItemNumber,ItemName';
$test=mysql_query($sql);
*/
// -- removed sql above for test --
$ItemNumber = -1; // assumes that this will never be -1 in database
echo "<ul>\n";
// while ($row = mysql_fetch_assoc($test)) { // -- removed while for testing
foreach ($data as $row) { // -- added foreach for testing
if ($ItemNumber != $row['ItemNumber']) { // new number grouping
if ($ItemNumber != -1) echo " </ul>\n </li>\n"; // not on
// first group, close last group sublist and containing li
$ItemNumber = $row['ItemNumber'];
echo " <li>ITEM NUMBER {$row['ItemNumber']}\n <ul>\n"; // new
// group and sublist
}
echo " <li>{$row['ItemName']}</li>\n"; // sublist member
}
echo " </li>\n</ul>\n"; // after last group
?>

generated this html:

<ul>
<li>ITEM NUMBER 346
<ul>
<li>ball</li>
<li>bat</li>
<li>bun</li>
</ul>
</li>
<li>ITEM NUMBER 349
<ul>
<li>ball</li>
<li>bat</li>
<li>bridge</li>
</ul>
</li>
<li>ITEM NUMBER 350
<ul>
<li>jib</li>
<li>jig</li>
<li>numb</li>
<li>poll</li>
</ul>
</li>
<li>ITEM NUMBER 417
<ul>
<li>hutch</li>
</li>
</ul>

To use this for your application, you probably need to:

(a) remove or comment all code between

// -- data and usort cmp for testing starts here --

and

// -- data and usort cmp for testing ends here --

(b) uncomment the code between

// -- removed sql below for test --

and

// -- removed sql above for test --

(c) remove the comment at the start of the line that has

// -- removed while for testing

(d) remove or comment the line that has

// -- added foreach for testing

(I couldn't be bothered to create and populate a database for the test,
so I loaded the data table into a 2d array and sorted it with usirt to
emulate the sql collation and sort.)

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: Sat Nov 23 17:51:13 GMT 2024

Total time taken to generate the page: 0.04603 seconds