Smudly Posted July 16, 2010 Share Posted July 16, 2010 I am currently working on an admin only page that allows the admin to view all of the websites members. On this page, the admin can edit any of the information for the users (and change it in the database), delete the users, ban the users, and sort the users (depending on which link they press, it will sort it accordingly). I've got the basic layout complete, however I have run into some dead ends. First Issue: I have the users information split into two different tables in my database. The first one "users" contains their basic information (name, username, email), while the second "userstats" contains their stats (credits, level, etc). Right now, when the page is loaded, it goes through the "users" table and displays each row exactly how it should. But I need it to do the same thing on these same rows for the other table. I'm not sure how I should change my While loop to get it to do this without causing problems. Second Issue: The sort feature allows the admin to sort through all the information by the username, id, email, etc. It works perfectly in an Ascending order, but I want it to go the opposite direction (Descending) once it is clicked again. I tried setting variables, and adding increment each time the link was clicked, and determining if the variable contained an even or odd number, but this didn't seem to work (The variable stayed at 1, so I obviously did it wrong). Third Issue: I need there to be a Delete button which when clicked, will popup a javascript confirm window. Once the admin confirms they want to delete the user, it will erase their information. Any feedback on any of these issues would be greatly appreciated. Here is my code: <?php session_start(); include_once('../inc/connect.php'); $result = mysql_query("SELECT * FROM users ORDER BY id"); $today = date("Y-m-d"); $sort = $_GET['sort']; $delete = $_GET['delete']; $submit = $_POST['submit']; if ($sort==id){ $result = mysql_query("SELECT * FROM users ORDER BY id"); } if ($sort==username){ $result = mysql_query("SELECT * FROM users ORDER BY username"); } if ($sort==email){ $result = mysql_query("SELECT * FROM users ORDER BY email"); } if ($sort==type){ $result = mysql_query("SELECT * FROM users ORDER BY member"); } if ($sort==referrer){ $result = mysql_query("SELECT * FROM users ORDER BY referrer"); } if ($sort==level){ $result = mysql_query("SELECT * FROM userstats ORDER BY level"); } if ($sort==exp){ $result = mysql_query("SELECT * FROM userstats ORDER BY exp"); } if ($sort==credits){ $result = mysql_query("SELECT * FROM userstats ORDER BY credits"); } if ($delete==true){ mysql_query("DELETE * from exampletable"); } if ($ban==true){ } // head echo " <html> <head> <title>Users</title> <style> a:link{ text-decoration: none; color: #519904; } a:visited{ text-decoration: none; color: #519904; } a:hover{ text-decoration: none; color: #4296ce; } #joined{ position: absolute; width: 200px; top: 35px; left: 465px; } </style> </head> <body> "; echo "<h2 align='center'>Members</h2><br /><table border='1' align='center'> <tr> <th bgcolor='#cccccc'><a href='users.php?sort=id'>ID</a></th> <th bgcolor='#cccccc'><a href='users.php?sort=username'>Username</a></th> <th bgcolor='#cccccc'><a href='users.php?sort=email'>Email</a></th> <th bgcolor='#cccccc'><a href='users.php?sort=type'>Type</a></th> <th bgcolor='#cccccc'><a href='users.php?sort=referrer'>Referrer</a></th> <!-- Level, Exp, and Credits are in the table called userstats --> <th bgcolor='#cccccc'><a href='users.php?sort=level'>Level</a></th> <th bgcolor='#cccccc'><a href='users.php?sort=exp'>Exp</a></th> <th bgcolor='#cccccc'><a href='users.php?sort=credits'>Credits</a></th> </tr><form>"; $recentmembers = 0; while($row = mysql_fetch_array($result)) { $joined = $row['joindate']; if ($joined==$today){ $recentmembers += 1; } echo "<tr>"; echo "<td align='center' width='40'>" .$row['id']. "</td>"; echo "<td align='center' width='130'><input type='text' name='username' value='" .$row['username']. "'></td>"; echo "<td align='center' width='230'><input type='text' name='email' value='" .$row['email']. "' size='35'></td>"; echo "<td align='center' width='10'><input type='text' name='member' value='" .$row['member']. "' size='2'></td>"; echo "<td align='center' width='175'><input type='text' name='referrer' value='" .$row['referrer']. "' size='25'></td>"; echo "<td align='center' width='10'><input type='text' name='level' value='" .$row['level']. "' size='2'></td>"; echo "<td align='center' width='10'><input type='text' name='exp' value='" .$row['exp']. "' size='10'></td>"; echo "<td align='center' width='10'><input type='text' name='credits' value='" .$row['credits']. "' size='20'></td>"; echo "<td align='center' width='10'><a href='users.php?delete=true&id=" .$row['id']. "'>Delete</a></td>"; echo "</tr>"; } echo "</table><br /><center><input type='submit' name='submit' value='Submit Changes'><input type='reset' name='reset' value='Reset'></form></center>"; echo "<br /><div id='joined'>Joined Today: ".$recentmembers."</div>"; // Footer echo " </body> </html> "; // Change User's Information if (isset($submit)){ // UPDATE USERS INFORMATION FOR ONLY THE ROWS THAT HAVE BEEN MODIFIED } ?> Quote Link to comment https://forums.phpfreaks.com/topic/207971-admin-view-members-page/ Share on other sites More sharing options...
jcbones Posted July 16, 2010 Share Posted July 16, 2010 I assumed (that gets you in trouble), that your users table, and the userstats table is tied together with "id". I tried to incorporate all of your request in this script. It may or may not work as I didn't test it. But, that is all you can expect for free <?php session_start(); include_once('../inc/connect.php'); $today = date("Y-m-d"); $sort = $_GET['sort']; $delete = $_GET['delete']; $dir = $_GET['dir']; $id = (isset($_GET['id']) && preg_match('~^[0-9]+$~',$_GET['id'])) ? $_GET['id'] : false; $submit = $_POST['submit']; switch($dir) { case 'ASC': $dir = 'DESC'; break; case 'DESC'; $dir = 'ASC'; break; default: $dir = 'ASC'; } switch($sort) { case 'username': $sorted = 'u.username'; break; case 'email': $sorted = 'u.email'; break; case 'type': $sorted = 'u.type'; break; case 'referrer': $sorted = 'u.referrer'; break; case 'level': $sorted = 's.level'; break; case 'exp': $sorted = 's.exp'; break; case 'credits': $sorted = 's.credits'; break; default: $sorted = 'u.id'; } //Assuming the tables user and userstats are tied together with the id. $sql = "SELECT u.*,s.level,s.exp,s.credits FROM users as u, userstats as s WHERE u.id = s.id ORDER BY $sorted $dir"; if ($delete==true && $id !== false){ //Assuming that users and userstats are tied together with the id. mysql_query("DELETE FROM users WHERE id='$id' LIMIT 1"); mysql_query("DELETE FROM userstats WHERE id='$id' LIMIT 1"); } if ($ban==true){ } // head echo " <html> <head> <title>Users</title> <style> a:link{ text-decoration: none; color: #519904; } a:visited{ text-decoration: none; color: #519904; } a:hover{ text-decoration: none; color: #4296ce; } #joined{ position: absolute; width: 200px; top: 35px; left: 465px; } </style> </head> <body> "; echo "<h2 align='center'>Members</h2><br /><table border='1' align='center'> <tr> <th bgcolor='#cccccc'><a href='users.php?sort=id&dir=" . $dir . "'>ID</a></th> <th bgcolor='#cccccc'><a href='users.php?sort=username&dir=" . $dir . "'>Username</a></th> <th bgcolor='#cccccc'><a href='users.php?sort=email&dir=" . $dir . "'>Email</a></th> <th bgcolor='#cccccc'><a href='users.php?sort=type&dir=" . $dir . "'>Type</a></th> <th bgcolor='#cccccc'><a href='users.php?sort=referrer&dir=" . $dir . "'>Referrer</a></th> <!-- Level, Exp, and Credits are in the table called userstats --> <th bgcolor='#cccccc'><a href='users.php?sort=level&dir=" . $dir . "'>Level</a></th> <th bgcolor='#cccccc'><a href='users.php?sort=exp&dir=" . $dir . "'>Exp</a></th> <th bgcolor='#cccccc'><a href='users.php?sort=credits&dir=" . $dir . "'>Credits</a></th> </tr><form>"; $result = mysql_query($sql); $recentmembers = 0; while($row = mysql_fetch_array($result)) { $joined = $row['joindate']; if ($joined==$today){ $recentmembers += 1; } echo "<tr>"; echo "<td align='center' width='40'>" .$row['id']. "</td>"; echo "<td align='center' width='130'><input type='text' name='username' value='" .$row['username']. "'></td>"; echo "<td align='center' width='230'><input type='text' name='email' value='" .$row['email']. "' size='35'></td>"; echo "<td align='center' width='10'><input type='text' name='member' value='" .$row['member']. "' size='2'></td>"; echo "<td align='center' width='175'><input type='text' name='referrer' value='" .$row['referrer']. "' size='25'></td>"; echo "<td align='center' width='10'><input type='text' name='level' value='" .$row['level']. "' size='2'></td>"; echo "<td align='center' width='10'><input type='text' name='exp' value='" .$row['exp']. "' size='10'></td>"; echo "<td align='center' width='10'><input type='text' name='credits' value='" .$row['credits']. "' size='20'></td>"; echo "<td align='center' width='10'><a href='users.php?delete=true&id=" .$row['id']. "' onclick=\"return confirm('Are you sure you wish to Delete this user?');\">Delete</a></td>"; echo "</tr>"; } echo "</table><br /><center><input type='submit' name='submit' value='Submit Changes'><input type='reset' name='reset' value='Reset'></form></center>"; echo "<br /><div id='joined'>Joined Today: ".$recentmembers."</div>"; // Footer echo " </body> </html> "; // Change User's Information if (isset($submit)){ // UPDATE USERS INFORMATION FOR ONLY THE ROWS THAT HAVE BEEN MODIFIED } ?> Quote Link to comment https://forums.phpfreaks.com/topic/207971-admin-view-members-page/#findComment-1087210 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.