penguin0 Posted April 23, 2009 Share Posted April 23, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/155290-solved-array-problem/ Share on other sites More sharing options...
laffin Posted April 23, 2009 Share Posted April 23, 2009 u dun have a definition for $groupname $group = $a_row['group']; I think ya got yer variable names mixed up Quote Link to comment https://forums.phpfreaks.com/topic/155290-solved-array-problem/#findComment-817118 Share on other sites More sharing options...
penguin0 Posted April 24, 2009 Author Share Posted April 24, 2009 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 ) ) Quote Link to comment https://forums.phpfreaks.com/topic/155290-solved-array-problem/#findComment-817948 Share on other sites More sharing options...
laffin Posted April 24, 2009 Share Posted April 24, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/155290-solved-array-problem/#findComment-818059 Share on other sites More sharing options...
penguin0 Posted April 25, 2009 Author Share Posted April 25, 2009 I echoed $groupname and I get all the group names: AdministratorsOfficersModeratorsMembersNew Recruits Any other ideas? Quote Link to comment https://forums.phpfreaks.com/topic/155290-solved-array-problem/#findComment-818781 Share on other sites More sharing options...
RussellReal Posted April 25, 2009 Share Posted April 25, 2009 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>"; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/155290-solved-array-problem/#findComment-818785 Share on other sites More sharing options...
penguin0 Posted April 25, 2009 Author Share Posted April 25, 2009 $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>"; Quote Link to comment https://forums.phpfreaks.com/topic/155290-solved-array-problem/#findComment-818862 Share on other sites More sharing options...
laffin Posted April 25, 2009 Share Posted April 25, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/155290-solved-array-problem/#findComment-818865 Share on other sites More sharing options...
penguin0 Posted April 25, 2009 Author Share Posted April 25, 2009 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']; } } Quote Link to comment https://forums.phpfreaks.com/topic/155290-solved-array-problem/#findComment-818875 Share on other sites More sharing options...
penguin0 Posted April 25, 2009 Author Share Posted April 25, 2009 Just so everyone's clear: The only error with my script is that I was using if(isset($groups[$groupname])){ } and that will only work with radios and check boxes so it was omitted. Quote Link to comment https://forums.phpfreaks.com/topic/155290-solved-array-problem/#findComment-818894 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.