how would sort a table?

General Chat, Comments
Post Reply
KaL
Posts: 344
Joined: Mon Jan 13, 2014 5:44 am

how would sort a table?

Post 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!
User avatar
Chris
Posts: 1581
Joined: Wed Sep 30, 2009 7:22 pm

Re: how would sort a table?

Post 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
        )

)
Fighting for peace is declaring war on war. If you want peace be peaceful.
KaL
Posts: 344
Joined: Mon Jan 13, 2014 5:44 am

Re: how would sort a table?

Post 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
)

)
KaL
Posts: 344
Joined: Mon Jan 13, 2014 5:44 am

Re: how would sort a table?

Post 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>";
User avatar
a_bertrand
Posts: 1536
Joined: Mon Feb 25, 2013 1:46 pm

Re: how would sort a table?

Post by a_bertrand »

Then you simply need to add a "order by" clause to your SQL statement.
Creator of Dot World Maker
Mad programmer and annoying composer
KaL
Posts: 344
Joined: Mon Jan 13, 2014 5:44 am

Re: how would sort a table?

Post by KaL »

i Swear! i learn new stuff everyday. it works!!! thanks Alain! :D -and thank you everyone for since a great support!
Post Reply

Return to “General”