I have the following php page, it displays my members, by there Status... Approved, Deleted, On Hold etc... I want to add a 'Delete' next to each member in the table that gets displayed, but only want this 'Delete' button to move the members to the 'Delete' column. Which from a database POV would mean changing the member in the 'MemApp' column from an 'A' to a 'D'... Any help would be greatly appreciated!
PHP Code:
/* set the allowed order by columns */
$default_sort = 'LastName';
$allowed_order = array ('JoinDate', 'FirstName','LastName', 'loginDateTime');
/* if order is not set, or it is not in the allowed
* list, then set it to a default value. Otherwise,
* set it to what was passed in. */
if (!isset ($_GET['order']) ||
!in_array ($_GET['order'], $allowed_order)) {
$order = $default_sort;
} else {
$order = $_GET['order'];
}
/* construct and run our query */
$query = "SELECT * FROM tblmembers WHERE `MemberApproved`='$cat' ORDER BY $order";
$result = mysql_query ($query);
/* make sure data was retrieved */
$numrows = mysql_num_rows($result);
if ($numrows == 0) {
echo "No data to display!";
exit;
}
/* now grab the first row and start the table */
$row = mysql_fetch_assoc ($result);
echo "<TABLE border=1>\n";
echo "<TR>\n";
foreach ($row as $heading=>$column) {
/* check if the heading is in our allowed_order
* array. If it is, hyperlink it so that we can
* order by this column */
echo "<TD><b>";
if (in_array ($heading, $allowed_order)) {
echo "<a href=\"{$_SERVER['PHP_SELF']}?order=$heading&cat=$cat\">$heading</a>";
} else {
echo $heading;
}
echo "</b></TD>\n";
}
echo "</TR>\n";
/* reset the $result set back to the first row and
* display the data */
mysql_data_seek ($result, 0);
while ($row = mysql_fetch_assoc ($result)) {
echo "<TR>\n";
foreach ($row as $column) {
echo "<TD>$column</TD>\n";
}
echo "</TR>\n";
}
echo "</TABLE>\n";
?>
<?php
if (isset($_GET['del']) AND $_GET['del'] <> "") {
/* construct and run our delete query */
$query = "DELETE FROM tblmembers WHERE `MemberID`='" . $_GET['del'] ."' LIMIT 1";
$result = mysql_query ($query);
}
/* set the allowed order by columns */
$default_sort = 'LastName';
$allowed_order = array ('JoinDate', 'FirstName','LastName', 'loginDateTime');
/* if order is not set, or it is not in the allowed
* list, then set it to a default value. Otherwise,
* set it to what was passed in. */
if (!isset($_GET['order']) OR !in_array ($_GET['order'], $allowed_order)) {
$order = $default_sort;
} else {
$order = $_GET['order'];
}
/* construct and run our query */
$query = "SELECT * FROM tblmembers WHERE `MemberApproved`='$cat' ORDER BY $order";
$result = mysql_query ($query);
/* make sure data was retrieved */
$numrows = mysql_num_rows($result);
if ($numrows == 0) {
echo "No data to display!";
} else {
/* now grab the first row and start the table */
$row = mysql_fetch_assoc ($result);
echo "<table border=1>\n";
$table_init = true;
echo "<TR>\n";
while ($row = mysql_fetch_assoc ($result)) {
if ($table_init) {
/* check if the heading is in our allowed_order array. If it is, hyperlink it so that we can order by this column */
echo "<tr>\n";
foreach ($row as $heading => $column) {
echo "<td>";
if (in_array ($heading, $allowed_order)) {
echo "<a href=\"{$_SERVER['PHP_SELF']}?order=$heading&cat=$cat\"><b>$heading</b></a>";
} else {
echo $heading;
}
echo "</td>\n";
}
echo "<td>Del?</td>";
echo "</tr>\n";
} else {
echo "<tr>\n";
foreach ($row as $column) {
echo "<td>$column</td>\n";
}
echo "<a href=\"{$_SERVER['PHP_SELF']}?del=" . $row['MemberID'] . "&order=$heading&cat=$cat\">Delete</a>";
echo "</tr>\n";
}
}
echo "</table>\n";
}
?>
I didn't know the name of the member's ID field and used "MemberID" as a placeholder.
<?php
if (isset($_GET['del']) AND $_GET['del'] <> "") {
/* construct and run our delete query */
$query = "DELETE FROM tblmembers WHERE `MemberID`='" . $_GET['del'] ."' LIMIT 1";
$result = mysql_query ($query);
}
/* set the allowed order by columns */
$default_sort = 'LastName';
$allowed_order = array ('JoinDate', 'FirstName','LastName', 'loginDateTime');
/* if order is not set, or it is not in the allowed
* list, then set it to a default value. Otherwise,
* set it to what was passed in. */
if (!isset($_GET['order']) OR !in_array ($_GET['order'], $allowed_order)) {
$order = $default_sort;
} else {
$order = $_GET['order'];
}
/* construct and run our query */
$query = "SELECT * FROM tblmembers WHERE `MemberApproved`='$cat' ORDER BY $order";
$result = mysql_query ($query);
/* make sure data was retrieved */
$numrows = mysql_num_rows($result);
if ($numrows == 0) {
echo "No data to display!";
} else {
/* now grab the first row and start the table */
$row = mysql_fetch_assoc ($result);
echo "<table border=1>\n";
$table_init = true;
echo "<TR>\n";
while ($row = mysql_fetch_assoc ($result)) {
if ($table_init) {
/* check if the heading is in our allowed_order array. If it is, hyperlink it so that we can order by this column */
echo "<tr>\n";
foreach ($row as $heading => $column) {
echo "<td>";
if (in_array ($heading, $allowed_order)) {
echo "<a href=\"{$_SERVER['PHP_SELF']}?order=$heading&cat=$cat\"><b>$heading</b></a>";
} else {
echo $heading;
}
echo "</td>\n";
}
echo "<td>Del?</td>";
echo "</tr>\n";
} else {
echo "<tr>\n";
foreach ($row as $column) {
echo "<td>$column</td>\n";
}
echo "<a href=\"{$_SERVER['PHP_SELF']}?del=" . $row['MemberID'] . "&order=$heading&cat=$cat\">Delete</a>";
echo "</tr>\n";
}
}
echo "</table>\n";
}
?>
I didn't know the name of the member's ID field and used "MemberID" as a placeholder.
I removed the extra loop.
Hi there, so if I run this is this just going to delete my database? I'm scared...
Hi NightShift, I noticed you delted the exit from here was that deliberate
PHP Code:
echo "No data to display!";
exit;
}
Yes, it shouldn't be used. Use "else" instead.
The reason for not using an exit in the middle of the script is that, if you ever wanted to add additional information at the bottom of the page - for example, a counter, a footer or maybe some debugging information, it would never get that far. I realize that, today, you know there's an exit in there. But in 2 weeks? 2 months? 2 years?
A simple "else" takes care of that and makes your script easier to follow.
Also, use the following, as I had forgotten to reset $table_init:
PHP Code:
<?php
if (isset($_GET['del']) AND $_GET['del'] <> "") {
/* construct and run our "deactivate" query */
$query = "UPDATE tblmembers SET MemberAPP = 'D' WHERE `MemberID`='" . $_GET['del'] ."' LIMIT 1";
$result = mysql_query ($query);
}
/* set the allowed order by columns */
$default_sort = 'LastName';
$allowed_order = array ('JoinDate', 'FirstName','LastName', 'loginDateTime');
/* if order is not set, or it is not in the allowed
* list, then set it to a default value. Otherwise,
* set it to what was passed in. */
if (!isset($_GET['order']) OR !in_array ($_GET['order'], $allowed_order)) {
$order = $default_sort;
} else {
$order = $_GET['order'];
}
/* construct and run our query */
$query = "SELECT * FROM tblmembers WHERE `MemberApproved`='$cat' ORDER BY $order";
$result = mysql_query ($query);
/* make sure data was retrieved */
if (mysql_num_rows($result)) {
echo "No data to display!";
} else {
/* now grab the first row and start the table */
$row = mysql_fetch_assoc ($result);
echo "<table border=1>\n";
$table_init = true;
echo "<TR>\n";
while ($row = mysql_fetch_assoc ($result)) {
if ($table_init) {
$table_init = false;
/* check if the heading is in our allowed_order array. If it is, hyperlink it so that we can order by this column */
echo "<tr>\n";
foreach ($row as $heading => $column) {
echo "<td>";
if (in_array ($heading, $allowed_order)) {
echo "<a href=\"{$_SERVER['PHP_SELF']}?order=$heading&cat=$cat\"><b>$heading</b></a>";
} else {
echo $heading;
}
echo "</td>\n";
}
echo "<td>Del?</td>";
echo "</tr>\n";
} else {
echo "<tr>\n";
foreach ($row as $heading => $column) {
echo "<td>$column</td>\n";
}
echo "<a href=\"{$_SERVER['PHP_SELF']}?del=" . $row['MemberID'] . "&order=$heading&cat=$cat\">Delete</a>";
echo "</tr>\n";
}
}
echo "</table>\n";
}
?>
echo 'There are '.$theCount.' members that are '.$field;
if (isset($_GET['del']) AND $_GET['del'] <> "") {
/* construct and run our "deactivate" query */
$query = "UPDATE tblmembers SET MemberAPP = 'D' WHERE `MemberID`='" . $_GET['del'] ."' LIMIT 1";
$result = mysql_query ($query);
}
/* set the allowed order by columns */
$default_sort = 'LastName';
$allowed_order = array ('JoinDate', 'FirstName','LastName', 'loginDateTime');
/* if order is not set, or it is not in the allowed
* list, then set it to a default value. Otherwise,
* set it to what was passed in. */
if (!isset($_GET['order']) OR !in_array ($_GET['order'], $allowed_order)) {
$order = $default_sort;
} else {
$order = $_GET['order'];
}
/* construct and run our query */
$query = "SELECT * FROM tblmembers WHERE `MemberApproved`='$cat' ORDER BY $order";
$result = mysql_query ($query);
/* make sure data was retrieved */
if (mysql_num_rows($result)) {
echo "No data to display!";
} else {
/* now grab the first row and start the table */
$row = mysql_fetch_assoc ($result);
echo "<table border=1>\n";
$table_init = true;
echo "<TR>\n";
while ($row = mysql_fetch_assoc ($result)) {
if ($table_init) {
$table_init = false;
/* check if the heading is in our allowed_order array. If it is, hyperlink it so that we can order by this column */
echo "<tr>\n";
foreach ($row as $heading => $column) {
echo "<td>";
if (in_array ($heading, $allowed_order)) {
echo "<a href=\"{$_SERVER['PHP_SELF']}?order=$heading&cat=$cat\"><b>$heading</b></a>";
} else {
echo $heading;
}
echo "</td>\n";
}
echo "<td>Del?</td>";
echo "</tr>\n";
} else {
echo "<tr>\n";
foreach ($row as $heading => $column) {
echo "<td>$column</td>\n";
}
echo "<a href=\"{$_SERVER['PHP_SELF']}?del=" . $row['MemberID'] . "&order=$heading&cat=$cat\">Delete</a>";
echo "</tr>\n";
}
}
echo "</table>\n";
}
?>
I had also forgotten to remove a "<TR>", which may account for the table not displaying. Try this:
PHP Code:
<?php
$cat = $_GET['cat'];
/* connect to the mysql database and use a query to get the members info */
include 'library/config.php';
include 'library/opendb.php';
//navigation
include("nav.php");
//approved
$sql = mysql_query("
SELECT 'Approved' as `MemberApp`, count(*) as `cnt` FROM `tblmembers` WHERE `MemberApproved`='A'
UNION SELECT 'Deleted' as `MemberApp`, count(*) as `cnt` FROM `tblmembers` WHERE `MemberApproved`='D'
UNION SELECT 'On Hold' as `MemberApp`, count(*) as `cnt` FROM `tblmembers` WHERE `MemberApproved`='H'
UNION SELECT 'Pending' as `MemberApp`, count(*) as `cnt` FROM `tblmembers` WHERE `MemberApproved`='P'
UNION SELECT 'Not Sure' as `MemberApp`, count(*) as `cnt` FROM `tblmembers` WHERE `MemberApproved`='N'
UNION SELECT 'Rejected' as `MemberApp`, count(*) as `cnt` FROM `tblmembers` WHERE `MemberApproved`='R'
";
$qry = mysql_query($sql) or die("SQL Error: $sql<br>" . mysql_error());
if (isset($_GET['del']) AND $_GET['del'] <> "") {
/* construct and run our "deactivate" query */
$query = "UPDATE tblmembers SET MemberAPP = 'D' WHERE `MemberID`='" . $_GET['del'] ."' LIMIT 1";
$result = mysql_query ($query);
}
/* set the allowed order by columns */
$default_sort = 'LastName';
$allowed_order = array ('JoinDate', 'FirstName','LastName', 'loginDateTime');
/* if order is not set, or it is not in the allowed
* list, then set it to a default value. Otherwise,
* set it to what was passed in. */
if (!isset($_GET['order']) OR !in_array ($_GET['order'], $allowed_order)) {
$order = $default_sort;
} else {
$order = $_GET['order'];
}
/* construct and run our query */
$query = "SELECT * FROM tblmembers WHERE `MemberApproved`='$cat' ORDER BY $order";
$result = mysql_query ($query);
/* make sure data was retrieved */
if (mysql_num_rows($result)) {
echo "No data to display!";
} else {
/* now grab the first row and start the table */
$row = mysql_fetch_assoc ($result);
echo "<table border=1>\n";
$table_init = true;
while ($row = mysql_fetch_assoc ($result)) {
if ($table_init) {
$table_init = false;
/* check if the heading is in our allowed_order array. If it is, hyperlink it so that we can order by this column */
echo "<tr>\n";
foreach ($row as $heading => $column) {
echo "<td>";
if (in_array ($heading, $allowed_order)) {
echo "<a href=\"{$_SERVER['PHP_SELF']}?order=$heading&cat=$cat\"><b>$heading</b></a>";
} else {
echo $heading;
}
echo "</td>\n";
}
echo "<td>Del?</td>";
echo "</tr>\n";
} else {
echo "<tr>\n";
foreach ($row as $heading => $column) {
echo "<td>$column</td>\n";
}
echo "<a href=\"{$_SERVER['PHP_SELF']}?del=" . $row['MemberID'] . "&order=$heading&cat=$cat\">Delete</a>";
echo "</tr>\n";
}
}
echo "</table>\n";
}
?>
Bookmarks