Jump to content

Create array of groups and users


sarahaziz2002

Recommended Posts

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.

acizt.png

Link to comment
Share on other sites

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
        )

)

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

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";
}

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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";
}

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.