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
https://forums.phpfreaks.com/topic/205641-organize-data/
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
https://forums.phpfreaks.com/topic/205641-organize-data/#findComment-1076132
Share on other sites

Archived

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

×
×
  • 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.