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!
how would sort a table?
Re: how would sort a table?
That depends on how you get that information:
This could be done with an SQL query:
Or sorting a PHP array with uasort (http://www.php.net/manual/en/function.uasort.php):
Result:
This could be done with an SQL query:
Code: Select all
SELECT * FROM `company` ORDER BY `wage` DESCCode: 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); 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
)
)
Fighting for peace is declaring war on war. If you want peace be peaceful.
Re: how would sort a table?
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
)
)
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?
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>";
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>";
- a_bertrand
- Posts: 1536
- Joined: Mon Feb 25, 2013 1:46 pm
Re: how would sort a table?
Then you simply need to add a "order by" clause to your SQL statement.
Creator of Dot World Maker
Mad programmer and annoying composer
Mad programmer and annoying composer
Re: how would sort a table?
i Swear! i learn new stuff everyday. it works!!! thanks Alain!
-and thank you everyone for since a great support!