Page 1 of 1
how would sort a table?
Posted: Tue Mar 25, 2014 7:00 am
by KaL
For example i want to sort the "works module" table after i create it:
Wood Company
wage: 5 Gold
Mine Company
wage: 10 Gold
Cotton Company
wage: 3 Gold
---------------------------------------------------------------------------------------------------------------------------------------
But instead i want them to sort it by wages, from highest wage on top and lowest wage on bottom.
Mine Company
wage: 10 Gold
Wood Company
wage: 5 Gold
Cotton Company
wage: 3 Gold
how would i do this? example would be great!
Re: how would sort a table?
Posted: Tue Mar 25, 2014 8:16 am
by Chris
That depends on how you get that information:
This could be done with an SQL query:
Code: Select all
SELECT * FROM `company` ORDER BY `wage` DESC
Or sorting a PHP array with uasort (
http://www.php.net/manual/en/function.uasort.php):
Code: Select all
<?php
function companyWageComparer($a, $b) {
return $a['wage'] == $b['wage'] ? 0 : $a['wage'] > $b['wage'] ? -1 : 1;
}
$companyList = array(
array('name'=>'Cotton Company','wage'=>3),
array('name'=>'Wood Company','wage'=>5),
array('name'=>'Mine Company','wage'=>10)
);
uasort($companyList, 'companyWageComparer');
print_r($companyList);
Result:
Code: Select all
Array
(
[2] => Array
(
[name] => Mine Company
[wage] => 10
)
[1] => Array
(
[name] => Wood Company
[wage] => 5
)
[0] => Array
(
[name] => Cotton Company
[wage] => 3
)
)
Re: how would sort a table?
Posted: Tue Mar 25, 2014 2:35 pm
by KaL
ok but how would you remove: "array", "[]" , "=>"
i just want it to print or echo only the content and placing it in order.
Just like this:
-------------------------------------------------------------------
Mine Company
wage: 10 Gold
Wood Company
wage: 5 Gold
Cotton Company
wage: 3 Gold
---------------------------------------------------------------------
not like this:
Array
(
[2] => Array
(
[name] => Mine Company
[wage] => 10
)
[1] => Array
(
[name] => Wood Company
[wage] => 5
)
[0] => Array
(
[name] => Cotton Company
[wage] => 3
)
)
Re: how would sort a table?
Posted: Tue Mar 25, 2014 3:34 pm
by KaL
yes! i'm getting the value from the database and its php.
SO its looks like this:
TableHeader(Companies");
$result = $db->Execute("select name,wage,description from company where id=? ",$userId);
while(!$result->EOF)
$name = $result->fields[0];
$wage = $result->fields[1];
$description = $result->fields[2];
$result->MoveNext();
echo "<form method='post' name=$name>";
echo $name;
echo $description;
echo $wage;
echo "<input type='hidden' value='ok' name=$name>";
SubmitButton("work it!","$name");
echo "</form>";
Re: how would sort a table?
Posted: Tue Mar 25, 2014 6:23 pm
by a_bertrand
Then you simply need to add a "order by" clause to your SQL statement.
Re: how would sort a table?
Posted: Wed Mar 26, 2014 5:40 am
by KaL
i Swear! i learn new stuff everyday. it works!!! thanks Alain!

-and thank you everyone for since a great support!