473,387 Members | 1,481 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

How to create array of groups and their users

Hello guys
i want to create something like this image ,consider the fact that Group and users comes from 2 different tables but only one query.
I want to add them into one array.

Oct 10 '10 #1

✓ answered by kovik

That's quite graceful. I was going to suggest the direct echoing method when you order the items by group in the SQL and then output the group name whenever the group changes.

Expand|Select|Wrap|Line Numbers
  1. $result = mysql_query("select * from users order by group asc");
  2. $current_group = null;
  3.  
  4. echo '<ul>';
  5.  
  6. while ($data = mysql_fetch_object($result)) {
  7.     if ($current_group != $data->group) {
  8.         $current_group = $data->group;
  9.         echo "<li><strong>{$data->group}</strong></li>";
  10.     }
  11.  
  12.     echo "<li>{$data->user}</li>"
  13. }
  14.  
  15. echo '</ul>';

4 1660
kovik
1,044 Expert 1GB
So... what are you having difficulty with? Storing the users into an array, or displaying them into this format that you have given?
Oct 10 '10 #2
Since i get all data from only one query the group is repeated with each record i want to add it once in the array and then add users of this group after it in the array.
How is that done?
Oct 11 '10 #3
dlite922
1,584 Expert 1GB
so if you have something like this:
Expand|Select|Wrap|Line Numbers
  1.  
  2. John Doe    grpA 
  3. Jane Smith  grpB
  4. Helen Fake  grpA
  5. Tom Record  grpA
  6. Micky Mouse grpB
  7.  
  8.  
Just use an associative array with the group being the key and each name list will be another array

Expand|Select|Wrap|Line Numbers
  1.  
  2. $data = array(); 
  3.  
  4. while($row = mysql_fetch_assoc($resource))
  5. {
  6.    $group = $row['group_name']; 
  7.    $name = $row['person_name'];
  8.  
  9.    $data[$group][] = $name; 
  10.  
  11. }
  12.  
  13.  
  14.  
That should do it.

Dan
Oct 11 '10 #4
kovik
1,044 Expert 1GB
That's quite graceful. I was going to suggest the direct echoing method when you order the items by group in the SQL and then output the group name whenever the group changes.

Expand|Select|Wrap|Line Numbers
  1. $result = mysql_query("select * from users order by group asc");
  2. $current_group = null;
  3.  
  4. echo '<ul>';
  5.  
  6. while ($data = mysql_fetch_object($result)) {
  7.     if ($current_group != $data->group) {
  8.         $current_group = $data->group;
  9.         echo "<li><strong>{$data->group}</strong></li>";
  10.     }
  11.  
  12.     echo "<li>{$data->user}</li>"
  13. }
  14.  
  15. echo '</ul>';
Oct 11 '10 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Galina | last post by:
Hello I recently updated my database applications from MS Access 97 to Access 2000. I somehow missed that the security file also should have been updated. All went fine and smooth. The...
1
by: Rosy | last post by:
I have a need to set permission levels to a database I have created. I need three to four groups that will have different access to different parts of the database. Each group will have between 3...
0
by: Neo | last post by:
Hi Everyone I am trying to set and retrieve NTFS permssions for fileshares on remote machine using ActiveDs Interop. It works for domain groups and users but when try to retrieve permssions for...
0
by: Raju | last post by:
Hi all i am Rajendran I am working as a asp.net programmer. I am frish to this field so please any of u garify my doubt I Create array of Textbox Dynamically and not passible to retrieve data...
2
by: Daveo | last post by:
Hi there, I've posted this in an excel group, but was wondering if the following may be easier to do in access? I am trying to create a function that will automatically allocate conference...
1
by: dragannis | last post by:
Hello, I realy need help about this, I have database that I want to use in my Vb code, and I need to create array from elements of one field from that mdb. Why : I need to do some calculations...
12
by: =?Utf-8?B?RGFuaWVs?= | last post by:
Hi, I have create a class name employee. Next, i would like to create array for the employee class. The code as below: dim eply(5) as employee when i call eply(0).name="Khrish", an error...
1
by: javasapphire | last post by:
hi , I have a simple application where there are seven groups which four users can access .Now an user can have access to one or more groups ..say user1 has access to group 1 and group 3.Also...
4
by: shashank kadge | last post by:
hi all, i am trying to get local admin users and groups on a windows server. here is the C# code that i am using...
4
by: sara | last post by:
Hi - I can't seem to choose the keywords to find an answer to this query - or maybe it's not in the posts. I have a database where the Buyers enter their Orders and then Accounts Payable posts...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.