I am fairly new to PHP. I’ve only created a few PHP/MySQL scripts so far, so what I am trying to accomplish is a little out of my level of expertise.
Here is what I have:
In essence, I have 6 dynamically populated drop down menus, which are populated by a MySQL database. The end user selects an option from each of the 6 drop down menus. The end user then clicks the submit button, which collects the data from the drop down menus.
Here is what I want to accomplish:
Upon the user clicking the submit button, I want to sort the end user’s choices, based on a hierarchy that I’ve defined, and then echo the results back to the browser in the correct order. So, how would I:
[LIST=1][*]
Define the Hierarchy?
Sort the Data
Arrange the Data (based on the hierarchy); and
Echo the results to the browser out
For instance, is it possible to define my hierarchy like this?
[CODE]
$a = 110A;
$b = 130B;
$c = 9000;
$a > $b > $c;
[code=php]
/**
* @param array $codes
* @return array
* @throws Exception
*/
function sortCodes(array $codes)
{
$hierarchy = array(
'090A',
'090C',
'110A',
'110C',
'1200',
'130A',
'130D'
);
$result = array();
foreach($codes as $code) {
$key = array_search($code, $hierarchy);
if($key === false) {
throw new Exception("'$code' not found in hierarchy");
}
$result[$key] = $code;
}
ksort($result);
return array_values($result);
}
// test it:
$codes = array(
'090A',
'090C',
'110A',
'110C',
'1200',
'130A',
'130D'
);
for($i=0; $i<20; $i++) {
$test = $codes;
shuffle($test);
$test = array_slice($test, 0, 3);
echo "Test data:".PHP_EOL;
print_r($test);
$sorted = sortCodes($test);
echo "Sorted data:".PHP_EOL;
print_r($sorted);
echo PHP_EOL;
}
[/code]
<i>
</i>[~]$ php sort.php
Test data:
Array
(
[0] => 130D
[1] => 130A
[2] => 110C
)
Sorted data:
Array
(
[0] => 110C
[1] => 130A
[2] => 130D
)
Test data:
Array
(
[0] => 130D
[1] => 090A
[2] => 090C
)
Sorted data:
Array
(
[0] => 090A
[1] => 090C
[2] => 130D
)
Test data:
Array
(
[0] => 090C
[1] => 130D
[2] => 090A
)
Sorted data:
Array
(
[0] => 090A
[1] => 090C
[2] => 130D
)
Test data:
Array
(
[0] => 130D
[1] => 090A
[2] => 110A
)
Sorted data:
Array
(
[0] => 090A
[1] => 110A
[2] => 130D
)
Test data:
Array
(
[0] => 110C
[1] => 1200
[2] => 130D
)
Sorted data:
Array
(
[0] => 110C
[1] => 1200
[2] => 130D
)
Test data:
Array
(
[0] => 110C
[1] => 090C
[2] => 110A
)
Sorted data:
Array
(
[0] => 090C
[1] => 110A
[2] => 110C
)
Test data:
Array
(
[0] => 090C
[1] => 1200
[2] => 130A
)
Sorted data:
Array
(
[0] => 090C
[1] => 1200
[2] => 130A
)
Test data:
Array
(
[0] => 130D
[1] => 110A
[2] => 090A
)
Sorted data:
Array
(
[0] => 090A
[1] => 110A
[2] => 130D
)
Test data:
Array
(
[0] => 1200
[1] => 110C
[2] => 130D
)
Sorted data:
Array
(
[0] => 110C
[1] => 1200
[2] => 130D
)
Test data:
Array
(
[0] => 110A
[1] => 110C
[2] => 1200
)
Sorted data:
Array
(
[0] => 110A
[1] => 110C
[2] => 1200
)
Test data:
Array
(
[0] => 110C
[1] => 130A
[2] => 090C
)
Sorted data:
Array
(
[0] => 090C
[1] => 110C
[2] => 130A
)
Test data:
Array
(
[0] => 110C
[1] => 130A
[2] => 090C
)
Sorted data:
Array
(
[0] => 090C
[1] => 110C
[2] => 130A
)
Test data:
Array
(
[0] => 130A
[1] => 1200
[2] => 130D
)
Sorted data:
Array
(
[0] => 1200
[1] => 130A
[2] => 130D
)
Test data:
Array
(
[0] => 130D
[1] => 090A
[2] => 130A
)
Sorted data:
Array
(
[0] => 090A
[1] => 130A
[2] => 130D
)
Test data:
Array
(
[0] => 1200
[1] => 090C
[2] => 110C
)
Sorted data:
Array
(
[0] => 090C
[1] => 110C
[2] => 1200
)
Test data:
Array
(
[0] => 090A
[1] => 110C
[2] => 1200
)
Sorted data:
Array
(
[0] => 090A
[1] => 110C
[2] => 1200
)
Test data:
Array
(
[0] => 090A
[1] => 1200
[2] => 110C
)
Sorted data:
Array
(
[0] => 090A
[1] => 110C
[2] => 1200
)
Test data:
Array
(
[0] => 090A
[1] => 130A
[2] => 130D
)
Sorted data:
Array
(
[0] => 090A
[1] => 130A
[2] => 130D
)
Test data:
Array
(
[0] => 090A
[1] => 130D
[2] => 090C
)
Sorted data:
Array
(
[0] => 090A
[1] => 090C
[2] => 130D
)
Test data:
Array
(
[0] => 090A
[1] => 130A
[2] => 090C
)
Sorted data:
Array
(
[0] => 090A
[1] => 090C
[2] => 130A
)
0.1.9 — BETA 6.16