PHP Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPHP Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old December 20th, 2012, 11:59 AM
chrismichaels84 chrismichaels84 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 20 chrismichaels84 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 48 m 11 sec
Reputation Power: 0
PHP5 - Sorting a multi-dimensional array

So, I have a multi-dimensional array (generated on the fly) that looks like this:
PHP Code:
 $users = Array ( 
    [
userid] => Array ( 
        [
0] => 12
        
[1] => 13 
        
[2] => 14
        
...
        ) 
        
    [
username] => Array ( 
        [
0] => Michael 
        
[1] => Jason 
        
[2] => Hannah 
        
...
        ) 
        
    [
role] => Array ( 
        [
0] => director 
        
[1] => member 
        
[2] => reader
        
...
        ) 
    ) 

and I want to be able to sort it by username and role so I can loop through $users and create a sortable table.
The array is actually populated from several different sources and I can't sort it at the SQL query level.
I can change the structure of the array, however, if its easier to sort. So long as I can itenerate through it to echo
a table row for each user.

Any ideas? I'd really appreciate it!

Reply With Quote
  #2  
Old December 20th, 2012, 12:03 PM
gw1500se gw1500se is online now
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Jul 2003
Posts: 2,873 gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level) 
Time spent in forums: 1 Year 1 Week 5 Days 13 h 19 m 39 sec
Reputation Power: 581
Won't array_multisort do the job?
__________________
There are 10 kinds of people in the world. Those that understand binary and those that don't.

Reply With Quote
  #3  
Old December 20th, 2012, 12:16 PM
chrismichaels84 chrismichaels84 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 20 chrismichaels84 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 48 m 11 sec
Reputation Power: 0
close

First, I love your quote as your signature. Second, I'm kinda new to PHP and didn't know about array_multisort so thank you for teaching me something new.

Now, this doesn't do exactly what I need. Here's what's really going on. The pm_projects table in my database holds all the info for each project. Including the ids or members, directors and readers of each project like this
members: 1/username, 2/username, 4/username
directors: 8/username, 7/username
etc.

So, in this script, I pull each of those fields, run it through a foreach loop, explode each one in turn and assign all the info to a new array $users that can then be looped through to display the users. What I want to be able to do is sort that able using ajax.

I'm using wordpress, so the query is done through a $wpdb class that is already setup and outputs to an associative array.

But I feel like my problem may be deeper than sorting the array. I may be going about this hole thing the wrong way.

Here's my abbreviated code:
PHP Code:
 $project $args// the project ID
                
$users = array(); // Will hold the array with all the users for this project
                
global $wpdb;
                
$users_db $wpdb->get_row'SELECT directors, members, readers FROM pm_projects WHERE id=' $projectARRAY_A );

                
// Fill the $users array
                
$i=0;
                foreach ( 
$users_db as $role => $user ) {
                    if ( 
$user != '' ) {
                        
$user explode(','$user);
                        foreach (
$user as $this_user) {
                            
$this_user explode'/'$this_user ); 
                            
$users['userid'][$i] = $this_user[0];
                            
$users['username'][$i] = $this_user[1];
                            
$users['role'][$i] = trim($role's');
                            
$i++;
                        }
                    }
                }

                for(
$i 0$size count$users['userid'] ); $i $size; ++$i) {
        
// Build my table with alternating colors for rows


Reply With Quote
  #4  
Old December 20th, 2012, 12:23 PM
ManiacDan's Avatar
ManiacDan ManiacDan is offline
Likely to be eaten by a grue.
Dev Shed God 10th Plane (9500 - 9999 posts)
 
Join Date: Oct 2006
Location: Pennsylvania, USA
Posts: 9,791 ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)  Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 2 Months 3 Weeks 14 h 53 m 20 sec
Reputation Power: 6112
1) If you're storing multiple pieces of data in the same database column, you're doing it wrong. It seems like that's what you're doing, but I may just have misinterpreted.

2) Use the jquery datatable to display your data and searching/sorting should be taken care of for you without needing a round-trip to the server.
__________________
HEY! YOU! Read the New User Guide and Forum Rules

"They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

"The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.

Reply With Quote
  #5  
Old December 20th, 2012, 12:29 PM
chrismichaels84 chrismichaels84 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 20 chrismichaels84 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 48 m 11 sec
Reputation Power: 0
Okay, so best practice?

Yes, I am storing multiple usernames in one column separated by commas that I then explode() after they've been read. I need to be able to know which users are assigned to each project (just the username and userid) by querying the project table. I'd rather not query the entire users table each time. Seems like a lot of querying.

Is there a better way to get the same effect?

And, jquery datatable is beautiful. I believe it will do the trick.

Thank you! I'm am trying to learn best practices here. I wish I could contribute to the forum more.

Reply With Quote
  #6  
Old December 20th, 2012, 12:35 PM
ManiacDan's Avatar
ManiacDan ManiacDan is offline
Likely to be eaten by a grue.
Dev Shed God 10th Plane (9500 - 9999 posts)
 
Join Date: Oct 2006
Location: Pennsylvania, USA
Posts: 9,791 ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)  Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 2 Months 3 Weeks 14 h 53 m 20 sec
Reputation Power: 6112
If you ever have comma-delimited data in your tables, it's wrong. That's the general rule. Once you know enough to list the 3-4 conditions in which it's not wrong, you won't need general rules anymore anyway.

The correct way to do this is to have a user-projects crossreference table storing userID and productID. Then join all 3 tables in a single query to get the results you want. It's more complex looking but orders of magnitude faster than doing string parsing in PHP for every row of your table.

Reply With Quote
  #7  
Old December 20th, 2012, 12:37 PM
chrismichaels84 chrismichaels84 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 20 chrismichaels84 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 48 m 11 sec
Reputation Power: 0
thank you

thank you. I will google SQL joining. I really appreciate it!

Reply With Quote
  #8  
Old December 20th, 2012, 12:45 PM
E-Oreo's Avatar
E-Oreo E-Oreo is offline
Lost in code
Click here for more information.
 
Join Date: Dec 2004
Posts: 7,931 E-Oreo User rank is General 90th Grade (Above 100000 Reputation Level)E-Oreo User rank is General 90th Grade (Above 100000 Reputation Level)E-Oreo User rank is General 90th Grade (Above 100000 Reputation Level)E-Oreo User rank is General 90th Grade (Above 100000 Reputation Level)E-Oreo User rank is General 90th Grade (Above 100000 Reputation Level)E-Oreo User rank is General 90th Grade (Above 100000 Reputation Level)E-Oreo User rank is General 90th Grade (Above 100000 Reputation Level)E-Oreo User rank is General 90th Grade (Above 100000 Reputation Level)E-Oreo User rank is General 90th Grade (Above 100000 Reputation Level)E-Oreo User rank is General 90th Grade (Above 100000 Reputation Level)E-Oreo User rank is General 90th Grade (Above 100000 Reputation Level)E-Oreo User rank is General 90th Grade (Above 100000 Reputation Level)E-Oreo User rank is General 90th Grade (Above 100000 Reputation Level)E-Oreo User rank is General 90th Grade (Above 100000 Reputation Level)E-Oreo User rank is General 90th Grade (Above 100000 Reputation Level)E-Oreo User rank is General 90th Grade (Above 100000 Reputation Level)  Folding Points: 945 Folding Title: Novice Folder
Time spent in forums: 2 Months 7 h 43 m 47 sec
Reputation Power: 6991
Also since you're new to PHP and working with WordPress, you should be aware that WordPress and most of its plugins are very poor examples of how to structure a database and code in general.

Your array would be easier to sort if you had it indexed by user id first rather than field.
__________________
PHP FAQ
How to program a basic, secure login system using PHP

Quote:
Originally Posted by Spad
Ah USB, the only rectangular connector where you have to make 3 attempts before you get it the right way around

Reply With Quote
  #9  
Old December 20th, 2012, 01:05 PM
ManiacDan's Avatar
ManiacDan ManiacDan is offline
Likely to be eaten by a grue.
Dev Shed God 10th Plane (9500 - 9999 posts)
 
Join Date: Oct 2006
Location: Pennsylvania, USA
Posts: 9,791 ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)  Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 2 Months 3 Weeks 14 h 53 m 20 sec
Reputation Power: 6112
Agreed, wordpress is awful.

Reply With Quote
  #10  
Old December 20th, 2012, 01:38 PM
chrismichaels84 chrismichaels84 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 20 chrismichaels84 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 48 m 11 sec
Reputation Power: 0
Quote:
Originally Posted by E-Oreo
Your array would be easier to sort if you had it indexed by user id first rather than field.


Can you show me what you mean? Just an example of the array structure?

I am actually developing a wordpress plugin. Not one that will be distributed. I own a small media company and we run our website on a wordpress install. We just want a project management system that is integrated into the admin section of our wordpress.

Reply With Quote
  #11  
Old December 20th, 2012, 02:12 PM
ManiacDan's Avatar
ManiacDan ManiacDan is offline
Likely to be eaten by a grue.
Dev Shed God 10th Plane (9500 - 9999 posts)
 
Join Date: Oct 2006
Location: Pennsylvania, USA
Posts: 9,791 ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)  Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 2 Months 3 Weeks 14 h 53 m 20 sec
Reputation Power: 6112
He means this:
PHP Code:
 $this_user explode'/'$this_user );  
                            
$users['userid'][$i] = $this_user[0]; 
                            
$users['username'][$i] = $this_user[1]; 
                            
$users['role'][$i] = trim($role's'); 
                            
$i++; 
Should be something like:
PHP Code:
 $this_user explode'/'$this_user );  
$users[$this_user[0]] = array(
    
'userid' => $this_user[0],
    
'username' => $this_user[1],
    
'role' => trim($role's')); 

Reply With Quote
  #12  
Old December 20th, 2012, 02:15 PM
chrismichaels84 chrismichaels84 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 20 chrismichaels84 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 48 m 11 sec
Reputation Power: 0
Perfect, thank you

That is great, thank you. I think I've also decided to reorganize my database. I think I'm doing more work than I have to. I wish I could give something back to the forum, help answer some questions but I'm just not qualified yet.

Thanks for all the help.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPHP Development > PHP5 - Sorting a multi-dimensional array

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap