Jump to content

[SOLVED] Array problem


penguin0

Recommended Posts

I am trying to use the array $groups which has the group tables information to check a users group (in the user table) and choose the appropriate color from the group table.

 

$result2 = mysql_query( "SELECT id, color, name, level FROM groups ORDER BY level" );
$num_rows = mysql_num_rows( $result2 );

$groups = array();
while ( $b_row = mysql_fetch_array( $result2 ) ) {
$gid = $b_row['id'];
$gname = $b_row['name'];
        $color = $b_row['color'];
$level = $b_row['level'];
$Info = array(
"id" => $gid,
        "color" => $color,
        "level" => $level
);
$groups = array_merge($groups, array($gname => $Info));
}


$sql = "SELECT * FROM users WHERE status = 'perm' ORDER BY id";
$result = mysql_query($sql, $link) or die(mysql_error());
$num_rows = mysql_num_rows( $result );

while ( $a_row = mysql_fetch_array( $result ) ) {
$group = $a_row['group'];
foreach(array_keys($groups) AS $key => $groupname){
	if(isset($groups[$groupname])){ 
                if($group = $groups[$groupname]['id']){
$colors = $groups[$groupname]['color'];
}
}


$id = $a_row['id'];
$name = $a_row['name'];
$position = $a_row['position'];
$charactern = $a_row['charactern'];
$displayname = $a_row['displayname'];
$realm = $a_row['realm'];
$email = $a_row['email'];
$online = $a_row['online'];
$created = $a_row['created'];
$newcreated = date('m/d/Y h:i:s A', $created);
if ($online == 1) {
$onlinepic = "<img src=\"images/online.gif\">";

} else {
$onlinepic = "";
}

}

 

Here is an example of how all that code will be output:

$table_block .= "<tr><td>$onlinepic <font color=\"".$groups[$groupname]['color']."\">$displayname</font></td><td>$position</td><td class=ac>$charactern</td></td><td><a href=\"mailto:$email\" class=\"links\">$email</a></td><td>$newcreated</td></tr>";

 

*in the code $group would be the same as one of these "$groups[$groupname]['id']".  As you can see I am checking to see if $group equals one of the groups while pulling the user table.  My problem is it only uses the color of the last group and every user gets that color, and not the color they are assigned by group.

Link to comment
Share on other sites

If that was the case then $colors wouldn't be working at all.  It does pull the last value of color out of the array.  I am just trying to pull the color that corresponds to the group ID that is stored for each user.  Please take a look at the array and maybe that will help:

 

Array ( [Administrators] => Array ( [id] => admins32 => #90000 [level] => 0 ) [Officers] => Array ( [id] => officers6642 => #0066CC [level] => 1 ) [Moderators] => Array ( [id] => mods2234 => Yellow [level] => 2 ) [Members] => Array ( [id] => members6543 => #FFFFFF [level] => 3 ) [New Recruits] => Array ( [id] => newreq2321 => pink [level] => 4 ) )

 

 

Link to comment
Share on other sites

The array isnt the problem

U dont show the definition of groupname

 

if it shows only the last entry added, than it makes sense that groupname isnt updated to the current users group.

 

so check yer definition of groupname, as the snippet you show only shows the usage of groupname not the definition of it

Link to comment
Share on other sites

assuming $groups actually gets filled..

<?php
// do whatever to get $groups filled...
$formatted = '';
foreach ($groups as $type => $members) {
	foreach ($members as $member) {
		list($name,$color,$level) = $member;
		$formatted .= "<span style='color: {$color};'>{$name}</span>";
	}
}
?>

Link to comment
Share on other sites

$groupname is assigned to the arrays keys which coincidently are the group names:

Array ( [0] => Administrators [1] => Officers [2] => Moderators [3] => Members [4] => New Recruits )

 

I don't have the following varables $member, $members

 

What I am ultimatly trying to do is get $displayname colored by the variable $color

 

Also I do not know which loop that should go in.  Could you please take a look at the code posted and show me where that goes and what would have to be changed to my variables?

 

This is where it ultimately will be displayed:

$table_block .= "<tr><td>$onlinepic <font color=\"".$groups[$groupname]['color']."\">$displayname</font></td><td>$position</td><td class=ac>$charactern</td></td><td><a href=\"mailto:$email\" class=\"links\">$email</a></td><td>$newcreated</td></tr>";

 

 

Link to comment
Share on other sites

Yes, But Groupname shudn be an array as u are showing.

 

groupname should be one of those titles, not an array of those titles

 

groupname should be updated after loading the groups.

 

and thats why u see it as failing.

My problem is it only uses the color of the last group and every user gets that color, and not the color they are assigned by group.

 

so load $groupname with the users group before the display function.

 

 

 

 

 

Link to comment
Share on other sites

I fixed this myself:

$result2 = mysql_query( "SELECT id, color, name, level FROM groups ORDER BY level" );
$num_rows = mysql_num_rows( $result2 );

$groups = array();
while ( $b_row = mysql_fetch_array( $result2 ) ) {
$gid = $b_row['id'];
$gname = $b_row['name'];
        $color = $b_row['color'];
$level = $b_row['level'];
$Info = array(
"id" => $gid,
        "color" => $color,
        "level" => $level
);
$groups = array_merge($groups, array($gname => $Info));
}
$sql = "SELECT * FROM users WHERE status = 'perm' ORDER BY id";
$result = mysql_query($sql, $link) or die(mysql_error());
$num_rows = mysql_num_rows( $result );

while ( $a_row = mysql_fetch_array( $result ) ) {
$group = $a_row['group'];
foreach(array_keys($groups) AS $key => $groupname){
                if($group == $groups[$groupname]['id']){
$colors = $groups[$groupname]['color'];
}
}

 

 

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.