Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Sorting multidimensional array
Old 05-23-2005, 05:49 AM Sorting multidimensional array
Marvin Le Rouge's Avatar
Skilled Talker

Posts: 52
Trades: 0
Hi,

I took the content of a query on my database, and threw it into an array, this way
Code:
$tabData = array ();
while ($lineData = mysql_fetch_assoc ($resultSql))
{
  array_push ($tabData, $lineData);
}
So, each line is an associative array containing several columns.
I want to sort this array line by line, depending on the content of columns (example : sort lines on column title, ascending then on column date descending), but array_multisort doesn't seem to be acting this way.

Could someone help, please ?
Marvin Le Rouge is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 05-23-2005, 07:16 AM
mtishetsky's Avatar
King Spam Talker

Posts: 1,226
Name: Mike
Location: Mataro, Spain
Trades: 0
Decision #1: Use mysql sorting. E.g. select * from my_table order by title asc, date desc
Decision #2: Use usort(). This function is well described in PHP manual. The "cmp" function will look like this:
PHP Code:
    function sortByCountryPlant($a$b) {
        
$a->country $a->countries[0]->country;
        
$a->plant $a->countries[0]->plant;

        
$b->country $b->countries[0]->country;
        
$b->plant $b->countries[0]->plant;

        if (
$a->cid == $b->cid) {
            if (
$a->sid == $b->sid) {
                if (
$a->country == $b->country) {
                    if (
$a->plant == $b->plant) {
                        if (
$a->name == $b->name) {
                            return 
0;
                        }
                        else
                            return (
$a->name $b->name ? -1);
                    }
                    else
                        return (
$a->plant $b->plant ? -1);
                }
                else
                    return (
$a->country $b->country ? -1);
            }
            else
                return (
$a->sid $b->sid ? -1);
        }
        else
            return (
$a->cid $b->cid ? -1);
    } 
It is just an example of how to sort array of objects by multiple fields, but in your case you may redesidn the function to sort array of arrays (which is in fact much similar to array of objects).
__________________

Please login or register to view this content. Registration is FREE
-
Please login or register to view this content. Registration is FREE
-
Please login or register to view this content. Registration is FREE

And don't forget to give me talkupation!
mtishetsky is offline
Reply With Quote
View Public Profile Visit mtishetsky's homepage!
 
Old 05-23-2005, 07:24 AM
mtishetsky's Avatar
King Spam Talker

Posts: 1,226
Name: Mike
Location: Mataro, Spain
Trades: 0
Also please note that while sorting on multiple fields the (N+1)'th sorting field is only used to sort the subset of records when values of the N'th sorting field is are the same for some records. In your case records will be sorted by title first and if some titles are same then they will be sorted by date. Only so, not less, not more.
__________________

Please login or register to view this content. Registration is FREE
-
Please login or register to view this content. Registration is FREE
-
Please login or register to view this content. Registration is FREE

And don't forget to give me talkupation!
mtishetsky is offline
Reply With Quote
View Public Profile Visit mtishetsky's homepage!
 
Old 05-23-2005, 11:36 AM
Marvin Le Rouge's Avatar
Skilled Talker

Posts: 52
Trades: 0
Thanks a lot
I'm gonna have a look at usort, 'cause mysql sorting here is ... irrelevant
Marvin Le Rouge is offline
Reply With Quote
View Public Profile
 
Old 05-23-2005, 01:35 PM
Marvin Le Rouge's Avatar
Skilled Talker

Posts: 52
Trades: 0
Aaaaaaaaaaaarrrghhhh !!!
It seems usort doesn't work properly if the array is an object attribute.
Marvin Le Rouge is offline
Reply With Quote
View Public Profile
 
Old 05-24-2005, 01:00 AM
mtishetsky's Avatar
King Spam Talker

Posts: 1,226
Name: Mike
Location: Mataro, Spain
Trades: 0
Why don't you want to use mysql_fetch_object() instead of mysql_fetch_assoc() ?
__________________

Please login or register to view this content. Registration is FREE
-
Please login or register to view this content. Registration is FREE
-
Please login or register to view this content. Registration is FREE

And don't forget to give me talkupation!
mtishetsky is offline
Reply With Quote
View Public Profile Visit mtishetsky's homepage!
 
Old 05-24-2005, 06:45 AM
celticbrue's Avatar
Extreme Talker

Posts: 175
Location: Wiltshire, England
Trades: 0
If you only want to produce one table of data, with each dataset presented only once and ordered as you specify, I don't see why you cannot use mysql sort as mtishetsky suggests in his first reply.

A little more information on your table structure, your query string and what you actually want to achieve would be helpful.

Regards

Ian
__________________
Found this useful? - HIT MY TALKUPATION!


Please login or register to view this content. Registration is FREE
celticbrue is offline
Reply With Quote
View Public Profile
 
Old 05-24-2005, 09:02 AM
Marvin Le Rouge's Avatar
Skilled Talker

Posts: 52
Trades: 0
I sort in in the query, but I want to be able to sort it in php too, 'cause i' have to sort the same resultset different ways.
So several queries would be absurd.

NB : the mysql_fetch_anything method is not the problem here
Marvin Le Rouge is offline
Reply With Quote
View Public Profile
 
Old 05-25-2005, 12:23 AM
mtishetsky's Avatar
King Spam Talker

Posts: 1,226
Name: Mike
Location: Mataro, Spain
Trades: 0
When you display something you use one query. If a user clicks some button or link labelled like "Show me the same items but ordered by another field" then you have to reload a page anyway and call another query. Write a function like following and just call it with neccessary parameter:
PHP Code:
function getTitles($orderby "") {

   
$sql "select * from table ";
      if (
$orderby != "")
         
$sql .= " order by $orderby ";
   
$result mysql_query($sql);
   ...
   return 
$records;

I hope you don't display the same set of records twice on the same page, first ordered by title and second ordered by date.
__________________

Please login or register to view this content. Registration is FREE
-
Please login or register to view this content. Registration is FREE
-
Please login or register to view this content. Registration is FREE

And don't forget to give me talkupation!
mtishetsky is offline
Reply With Quote
View Public Profile Visit mtishetsky's homepage!
 
Old 05-25-2005, 02:29 PM
Marvin Le Rouge's Avatar
Skilled Talker

Posts: 52
Trades: 0
Quote:
Originally Posted by mtishetsky
I hope you don't display the same set of records twice on the same page, first ordered by title and second ordered by date.
No, but i re-use the same set of data, using it differently.
I think i'm gonna have two queries : that will be far more simple, and on this size of data, after all, it's not so bad.
Perhaps i'm too purist sometimes
Marvin Le Rouge is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Sorting multidimensional array
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 3.05047 seconds with 12 queries