• A little PHP table help.
    5 replies, posted
Alright, so my problem is, I have a table in my database that stores every user in a group to the table, and if you are not in the group, you don't have a record at all. So, my issue is, I am trying to make each row in a table either display the user's group, or some text that says "User not in a org." I would just use a while loop, but since there are only two records in the database, it only runs twice. This is probably making no sense, so let me just show you my code. [code] $areResults = false; while ($res = $result->fetch_array()){ $areResults = true; // Edited my code out, but here is all my <tr> and <td>s and shit. It just displays every user's info in a table, except for the org. } if ($areResults == false){ echo('<div class="alert alert-error"><b>Uh oh!</b> There are no results!</div>'); } [/code] That's the code that fetches and displays all the users, but not the row with their org. I had a separate while loop for the org row, and the code for that is: [code] $organizations = true; if ( $organizations == true) { $orgq = "SELECT steamID FROM playerinformation WHERE uid='" . $res['uid'] . "'"; $oresult = $db->query($orgq) or die ($db->error()); while ( $ores = $oresult->fetch_object()->steamID ) { $getorgid = "SELECT orgid FROM players WHERE steamid='" . $ores . "'"; $orgid = $db->query($getorgid) or die ($db->error()); } while ( $org = $orgid->fetch_object()->orgid ) { $getorg = "SELECT Name FROM organisations WHERE ID='" . $org . "'"; $orgname = $db->query($getorg) or die ($db->error()); } if ( $org == false ) { echo("<td>Player not in an org.</td>"); } else { echo("<td>" . $orgname->fetch_object()->Name . "</td>" ); } } [/code] The above is what I was messing with to get it do display "Player not in an org" for those not in one and the name of the org for those who are. However, since every player does not have one, it only runs the loop for the ones who do. So my question is, how do I get it to display something for users without an org and the name of the org for the users with one? Sorry if this is really confusing, let me know how I can clarify if I need to. Thanks!
This is really confusing imo.. Why so many different tables? Anyways, what you could do is just see if player has the organisation set or false or however you handle it, then if not true then he's not in a organisation. If there is a value, check the ID and see if it matches with a organisation from the organisation table and then use that value?
[QUOTE=Killervalon;44679076]This is really confusing imo.. Why so many different tables?[/QUOTE] Two? What's unreasonable about storing player info in one table and group info in another?
[CODE] $query = "SELECT organisations.Name FROM playerinformation LEFT JOIN players ON players.steamid = playerinformation.steamID LEFT JOIN organisations ON organisations.ID = players.orgid WHERE playerinformation.steamID = '".$uid."'"; $result = $db->query($query); while ($row = $result->fetch_assoc()) { var_dump($row); } [/CODE]
[QUOTE=CBastard;44679948][CODE] $query = "SELECT organisations.Name FROM playerinformation LEFT JOIN players ON players.steamid = playerinformation.steamID LEFT JOIN organisations ON organisations.ID = players.orgid WHERE playerinformation.steamID = '".$uid."'"; $result = $db->query($query); while ($row = $result->fetch_assoc()) { var_dump($row); } [/CODE][/QUOTE] Does that just simplify my long way of doing queries? And I think I can achieve what I wanna do with a simple for loop, don't know why I didn't think of that.
Joins do appear what you are looking for. [code] <?php $query = "SELECT organisations.Name AS org_name, players.name AS player_name FROM playerinformation LEFT JOIN players ON players.steamid = playerinformation.steamID LEFT JOIN organisations ON organisations.ID = players.orgid WHERE playerinformation.steamID = '".$uid."'"; $result = $db->query($query); ?> <table> <tr> <th>Name</th> <th>Organisation</th> </tr> <?php while ($row = $result->fetch_assoc()): ?> <tr> <td><?php echo $row['player_name'] ?></td> <td> <?php if(isset($row['org_name']){ echo $row['org_name']; }else{ echo 'Player not in an org.'; } ?> </td> </tr> <?php endwhile; ?> </table> [/code] I guessed where the player's name would be.
Sorry, you need to Log In to post a reply to this thread.