Jump to content


Photo

Array not working


  • Please log in to reply
4 replies to this topic

#1 dual_alliance

dual_alliance
  • Members
  • PipPipPip
  • Advanced Member
  • 140 posts
  • LocationNSW, Australia

Posted 22 September 2006 - 03:39 PM

Hello, l have the following code:

    <?php
    // Read information from MySQL database	
    $query = "SELECT `userid`, `username`, `m.group` FROM `users` ORDER by `userid` ASC ";
	$result = mysql_query($query) or die("Problem with the query: $query<br>" . mysql_error());
	$rows = mysql_fetch_assoc($result);
	$mgroup = $rows['m.group'];
	$group = array(
		1=>"Banned",
		2=>"Member",
                3=>"Donator",
                4=>"Game Moderator",
                5=>"Forum Moderator",
                6=>"Global Moderator",
                7=>"Graphic's Admin",
                8=>"General Admin",
                9=>"Root Admin"
                );
	while ($row = mysql_fetch_array($result)){
	?>
    <tr>
		<td><p class="description"> <?php echo $row['userid']; ?></p></td>
		<td><p class="description"> <?php echo $row['username']; ?></p></td>
		<td><p class="description"> <?php echo $group[$mgroup]; ?></p></td>
		<td><p class="description">View Profile</p></td>
	</tr>
	<?php
    }
    ?>

When l view this it prints out that their all Root Admin ???

If you could shed some light on this l would appreciate it.

Thanks

#2 ronverdonk

ronverdonk
  • Members
  • PipPipPip
  • Advanced Member
  • 277 posts
  • LocationNetherlands

Posted 22 September 2006 - 03:56 PM

$mgroup is set only at the first row and is not changed after that. Move it to the while loop.

Ronald   8)
RTFM is an almost extinct art form, it should be subsidized.

#3 dual_alliance

dual_alliance
  • Members
  • PipPipPip
  • Advanced Member
  • 140 posts
  • LocationNSW, Australia

Posted 22 September 2006 - 03:58 PM

Thanks for finding the problem :), but how would l do that?

#4 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 22 September 2006 - 03:59 PM

You have the loop constructed incorrectly, such that the variable $mgroup is only set once to the value of the first record. If that record was an "Root Admin", you would show that for all of the records.

Change you code to something like:
    <?php
$group = array(1=>"Banned",
                2=>"Member",
                3=>"Donator",
                4=>"Game Moderator",
                5=>"Forum Moderator",
                6=>"Global Moderator",
                7=>"Graphic's Admin",
                8=>"General Admin",
                9=>"Root Admin"
                );

    // Read information from MySQL database
    $query = "SELECT `userid`, `username`, `m.group` FROM `users` ORDER by `userid` ASC ";
    $result = mysql_query($query) or die("Problem with the query: $query<br>" . mysql_error());
    while($rows = mysql_fetch_assoc($result)) {
            $mgroup = $rows['m.group'];
?>
    <tr>
         <td><p class="description"> <?php echo $row['userid']; ?></p></td>
         <td><p class="description"> <?php echo $row['username']; ?></p></td>
         <td><p class="description"> <?php echo $group[$mgroup]; ?></p></td>
         <td><p class="description">View Profile</p></td>
    </tr>
<?php
    }
    ?>

Ken



#5 dual_alliance

dual_alliance
  • Members
  • PipPipPip
  • Advanced Member
  • 140 posts
  • LocationNSW, Australia

Posted 22 September 2006 - 04:11 PM

Thankyou both for you help, kenrbnsn that worked prefectly thanks once again.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users