Clearshot Posted June 23, 2010 Share Posted June 23, 2010 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>'; } Quote Link to comment Share on other sites More sharing options...
Psycho Posted June 23, 2010 Share Posted June 23, 2010 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"; } Quote Link to comment Share on other sites More sharing options...
Clearshot Posted June 23, 2010 Author Share Posted June 23, 2010 Works perfect! Thanks alot! Quote Link to comment Share on other sites More sharing options...
Clearshot Posted June 23, 2010 Author Share Posted June 23, 2010 Is there anyway I can put these into a table or frame just to clean it up abit? Quote Link to comment Share on other sites More sharing options...
Psycho Posted June 23, 2010 Share Posted June 23, 2010 Is there anyway I can put these into a table or frame just to clean it up abit? Build youself a static HTML page of how you would like the output to be displayed. Then modify the code to output in that format. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.