sarahaziz2002 Posted October 11, 2010 Share Posted October 11, 2010 Hello Guys This is the first time i ask a question at php freaks. Hope i could find the answer. I want to create something similar to the image now the problem is that i want to get groups and their users in the same array. Notice something like Array [0] => Group1 [1] => user1 [2] => user2 [3] => Group2 and so on. How could i do that. Thanks in advance. Quote Link to comment https://forums.phpfreaks.com/topic/215615-create-array-of-groups-and-users/ Share on other sites More sharing options...
Spleshmen Posted October 11, 2010 Share Posted October 11, 2010 something like this? <?php $a = array( 'group 1' => array( 0 => 'item g1 0', 1 => 'item g1 1', 2 => 'item g1 3' ), 'group 2' => array( 0 => 'item g2 0', 1 => 'item g2 1', 2 => 'item g2 2' ) ); echo '<pre>'; print_r($a); echo '</pre>'; ?> result Array ( [group 1] => Array ( [0] => item g1 0 [1] => item g1 1 [2] => item g1 3 ) [group 2] => Array ( [0] => item g2 0 [1] => item g2 1 [2] => item g2 2 ) ) Quote Link to comment https://forums.phpfreaks.com/topic/215615-create-array-of-groups-and-users/#findComment-1121040 Share on other sites More sharing options...
sarahaziz2002 Posted October 11, 2010 Author Share Posted October 11, 2010 Thanks for your reply But put into account that i get these results from 2 different queries so how to do it? Also i wish to add them into just one array. Quote Link to comment https://forums.phpfreaks.com/topic/215615-create-array-of-groups-and-users/#findComment-1121069 Share on other sites More sharing options...
AbraCadaver Posted October 11, 2010 Share Posted October 11, 2010 Thanks for your reply But put into account that i get these results from 2 different queries so how to do it? Also i wish to add them into just one array. Show your queries. Quote Link to comment https://forums.phpfreaks.com/topic/215615-create-array-of-groups-and-users/#findComment-1121196 Share on other sites More sharing options...
Psycho Posted October 11, 2010 Share Posted October 11, 2010 ...i get these results from 2 different queries ... You should only be using ONE query with a JOIN statement between the two tables. Then you just need to order by group and the records will be automatically "grouped" by the group value. Quote Link to comment https://forums.phpfreaks.com/topic/215615-create-array-of-groups-and-users/#findComment-1121200 Share on other sites More sharing options...
sarahaziz2002 Posted October 12, 2010 Author Share Posted October 12, 2010 Now even if i get them into one query how do i add them into one array. The queries: $sql = mysql_query('SELECT id,login FROM groups,simulationgroups WHERE Simulation_ID = 5 and id = simulationgroups.Group_ID and kind_of_user NOT IN (1,2) $result = mysql_fetch_array($sql); $sql2 = mysql_query("SELECT * FROM users WHERE Group_ID = ".$result['id']) $result2 = mysql_fetch_array($sql2); how do i add them into one array each group followed by it's users. Quote Link to comment https://forums.phpfreaks.com/topic/215615-create-array-of-groups-and-users/#findComment-1121401 Share on other sites More sharing options...
Psycho Posted October 12, 2010 Share Posted October 12, 2010 Now even if i get them into one query how do i add them into one array. Once you have all the data, why would you need to get them into an array? Just process the db results into the output you want. Here is some sample code. I made some assumptions on a copuple db fields $query = 'SELECT u.name FROM users as u JOIN groups as g ON u.Group_ID = g.id JOIN simulationgroups as sg ON sg.Group_ID = g.id WHERE sg.Simulation_ID = 5 AND g.kind_of_user NOT IN (1,2) ORDER BY g.id'; $result = mysql_query($query); $current_groupID = false; while($row = mysql_fetch_assoc($result)) { if($current_groupID != $row['Group_ID']) { $current_groupID = $row['Group_ID']; echo "<h2>{$current_groupID}</h2>\n"; } echo "{$row['name']}<br />\n"; } Quote Link to comment https://forums.phpfreaks.com/topic/215615-create-array-of-groups-and-users/#findComment-1121477 Share on other sites More sharing options...
sarahaziz2002 Posted October 13, 2010 Author Share Posted October 13, 2010 Hello guys, @mjdamato:OK the way your thinking is fine.But the thing is that i found this function to split the list into 3 or 4 lists.So i need to add all data in one array to split it. here is the function: function split_array($array, $slices) { $perSlice = floor(count($array) / $slices); $sliceExtra = count($array) % $slices; $slicesArray = array(); $offset = 0; for($i = 0; $i < $slices; $i++) { $extra = (($sliceExtra--) > 0) ? 1 : 0; $slicesArray[] = array_slice($array, $offset, $perSlice + $extra); $offset += $perSlice + $extra; } return $slicesArray; } Also one more thing i need to change the group name to bold so any ideas? Any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/215615-create-array-of-groups-and-users/#findComment-1121726 Share on other sites More sharing options...
Psycho Posted October 13, 2010 Share Posted October 13, 2010 You need to stop obsessing over needing an array - at least not in the manner you are thinking. Just because there is an existing piece of code that does something similar to what you want doesn't necessarily mean you have to do it exactly how that code was written. By dumping the results into an array and then processing the array you are making the server do twice as much work as needed. Instead you just need to modify the processing script for the db results - which is very easy using the existing code you have as a model. The example below does use an array - but not how you have it above - it is only a temporary container. By the way, that code you just posted has a flaw if you wanted to use it with a multidimensional array since it wouldn't take into account the headers. I even added logic to add the alternating background colors as shown in the image you first posted. <?php //Run the query $query = 'SELECT u.name FROM users as u JOIN groups as g ON u.Group_ID = g.id JOIN simulationgroups as sg ON sg.Group_ID = g.id WHERE sg.Simulation_ID = 5 AND g.kind_of_user NOT IN (1,2) ORDER BY g.id'; $result = mysql_query($query); //Process the results into table cells $bgColors = array('#00FFFF', '#FF8040', '#FFFF00', '#FF00FF'); $groupCount = 0; $current_groupID = false; $cells = array(); while($row = mysql_fetch_assoc($result)) { if($current_groupID != $row['Group_ID']) { $current_groupID = $row['Group_ID']; $bgColor = $bgColors[$groupCount%count($bgColors)]; $groupCount++; $cells[] = " <th style=\"background-color:{$bgColor};\">Group {$current_groupID}</th>\n"; } $cells[] =" <td style=\"background-color:{$bgColor};\">{$row['name']}</td>\n"; } //Ouput into multi-column table $columns = 4; //Set the number of columns to use $tableOutput = ''; $cellsPerCol = ceil(count($cells)/$columns); for($row=0; $row<$cellsPerCol; $row++) { $tableOutput .= " <tr>\n"; for($col=0; $col<$columns; $col++) { $index = ($col*$cellsPerCol+$row); $tableOutput .= (isset($cells[$index])) ? $cells[$index] : ''; } $tableOutput .= " <tr>\n"; } ?> <html> <body> <table border="1"> <?php echo $tableOutput; ?> </table> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/215615-create-array-of-groups-and-users/#findComment-1121808 Share on other sites More sharing options...
sarahaziz2002 Posted October 14, 2010 Author Share Posted October 14, 2010 I like your answer but i wish to display it as a list and list items. And also if you could tell me what does this step mean? for($row=0; $row<$cellsPerCol; $row++) { $tableOutput .= " <tr>\n"; for($col=0; $col<$columns; $col++) { $index = ($col*$cellsPerCol+$row); $tableOutput .= (isset($cells[$index])) ? $cells[$index] : ''; } $tableOutput .= " <tr>\n"; } Quote Link to comment https://forums.phpfreaks.com/topic/215615-create-array-of-groups-and-users/#findComment-1122080 Share on other sites More sharing options...
Psycho Posted October 14, 2010 Share Posted October 14, 2010 I like your answer but i wish to display it as a list and list items. I've given you plenty of code to use as a base that you can then modify from. And also if you could tell me what does this step mean? for($row=0; $row<$cellsPerCol; $row++) { $tableOutput .= " <tr>\n"; for($col=0; $col<$columns; $col++) { $index = ($col*$cellsPerCol+$row); $tableOutput .= (isset($cells[$index])) ? $cells[$index] : ''; } $tableOutput .= " <tr>\n"; } Which step? There are 10 lines of code there. It is a loop that builds the HTML output to be displayed on the page. Each loop creates a new table row. I wrote it to output the data similar to what you provided, but if you don't like then just change it to what you need. However, if you want to use list items, I'd suggest just using CSS to create columns. Take a look at this tutorial: http://www.communitymx.com/content/article.cfm?cid=27f87 Quote Link to comment https://forums.phpfreaks.com/topic/215615-create-array-of-groups-and-users/#findComment-1122139 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.