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


}

?>
Member Avatar for diafol

Issue 1: use a LEFT JOIN to link the two tables BEFORE running the while loop (see mYSQL manual).

Issue 2: set a session variable to deal with ASC and DESC, e.g.

session_start();
if(!isset($_SESSION['sort_counter']))$_SESSION['sort_counter'] = 1;

if($_SESSION['sort_counter']%2) == 0){ //test even value
  $sort = " DESC";
}else{ //odd value
  $sort = "";
}

...

$rs = mysql_query("SELECT fields... FROM table1 ... ORDER BY field1{$sort}");
...
$_SESSION['sort_counter'] = $_SESSION['sort_counter'] + 1; //increment after every run

Issue 3:

HEAD SCRIPT:

<script type="text/javascript">
<!--
function confirm_delete(id) {
	var choose = confirm("Delete user " + id + "?")
	if (choose){
		//do your delete function here, e.g. ajax call
	}
	else{
		alert("Delete cancelled")
	}
}
//-->
</script>

...

<a href="javascript: confirm_delete(6);">Delete User #6</a>

The links are usually created dynamically by php (from mysql data).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.