Jump to content

Organize data


Clearshot

Recommended Posts

I want to organize all of our members based on their rank, but im not sure how to do this. I tried to use tables, but it didn't work out to well.

 

                $sql = "SELECT * FROM members ORDER BY id ASC";
	$result = mysql_query($sql);

	while ($row = mysql_fetch_assoc($result)) 
	{
		$rank_id = $row['rank'];
		$sql2 = "SELECT * FROM ranks WHERE id = '$rank_id'";
		$result2 = mysql_query($sql2);
		while ($row2 = mysql_fetch_assoc($result2))
		{
			$rank = $row2['name'];
		}

			echo '<h1>' . $rank . '</h1>';
			echo '<b>Name: </b>' . $row['name'] . '<br>';
			echo '<b>Steam ID: </b>' . $row['steamid'] . '<br>';
			echo '<b>Rank: </b> ' . $rank. ' <br>';

	}

Link to comment
Share on other sites

You need to learn to use JOINs in your queries. I would also advise against duplicating field names between tables (unless they are foreign keys) as it can cause confusion. I had to give aliases for the 'name" fields from the members and ranks tables to be able to query them at the same time.

$query = "SELECT members.name as username, steamid, ranks.name as rankname
          FROM members
          JOIN ranks ON members.rank = ranks.id
          ORDER BY ranks.id, members.name";
$result = mysql_query($query);

$current_rank = '';
while($row = mysql_fetch_assoc($result))
{
    //Show rank header if different from last
    if($current_rank != $row['rankname'])
    {
        $current_rank = $row['rankname'];
        echo "<h1>{$current_rank}</h1>\n";
    };

    //Show player details
    echo "<b>Name:</b> {$row['username']}<br />\n";
    echo "<b>Steam ID:</b> {$row['steamid']}<br />\n";
    echo "<b>Rank:</b> {$row['rankname']}<br /><br />\n";
}

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.