Jump to content

Archived

This topic is now archived and is closed to further replies.

dual_alliance

Array not working

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

Share this post


Link to post
Share on other sites
$mgroup is set only at the first row and is not changed after that. Move it to the while loop.

Ronald   8)

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

×

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.