Jump to content

MySQL query and foreach loop not acting as wanted


TRobs

Recommended Posts

First off, I would just like to say "hi" as this is my first post here.

Here is the code
[code]
// GET GUILD INFORMATION
$guild_info_query = mysql_query ("
                        SELECT *
                        FROM `guilds`
                        WHERE `name` = '$guild'
                        LIMIT 0,1") or die (mysql_error ());
$guild_info = mysql_fetch_array ($guild_info_query);

$guild_id = $guild_info['id'];

$member_info_query = mysql_query ("
                        SELECT `id`, `username`, `level`
                        FROM `users`
                        WHERE `guild`  = '$guild'
                        ORDER BY `username`") or die (mysql_error ());
$member_info = mysql_fetch_array ($member_info_query);
print_r ($member_info);
?>

<table>
    <tr>
        <td>Guild Name:</td>
        <td><?php print $guild; ?></td>
    </tr>
    <tr>
        <td>Guild Leader:</td>
        <td><?php foreach ($member_info as $leader) { if ($leader['id'] == $guild_id) { print $leader['username']; } } ?></td>
    </tr>
    <tr>
        <td>Description:</td>
        <td><?php print $guild_info['description']; ?></td>
    </tr>
    <tr>
        <td colspan="2">Members</td>
    </tr>
<?php
    foreach ($member_info as $member)
    {
        print "<tr><td>   <a href=\"viewcharacter.php?view=".$member['id']."\">".$member['username']."</a></td><td>".$member['level']."</td></tr>";
    }
?>[/code]

The first problem I am having has to do with the second MySQL query. While testing I have 3 people in the guild. [i]$guild[/i] is defined in an included file. When I run the query through phpMyAdmin it works the way i want it too, but in this file it returns only one of the guild members and it adds new things to the array from which im not sure where they come.
Here is what the print_r function returns:
Array ( [0] => 2 [id] => 2 [1] => Drew [username] => Drew [2] => 1 [level] => 1 )


My other Problem takes place in both of the foreach loops. In the first loop I want it to print the username of the person in the guild whose user_id == the guild_id. Instead it prints all the numbers in the array the equal the guild_id.

Second foreach loop should print the user name and their level, but instead it goes through each variable in the array and just prints the first letter or number and goes onto the next.

Thanks for the help in advanced. I am willing to try any alternatives to the foreach loops
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.