Jump to content

Array not working


dual_alliance

Recommended Posts

Hello, l have the following code:

[code=php:0]    <?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
    }
    ?>[/code]

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
Link to comment
Share on other sites

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:
[code]    <?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
    }
    ?>[/code]

Ken

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.