I played around with your code a bit and just couldn’t get it to work like I wanted. Decided to make my own version, going off the theme of creating Meetings and a list of items to toggle the status on. This page covers pretty much all of it even commented out mysqli connection. You will notice an array where you can adjust button names and colors near the top of the page. Also there are two button styles. I will also upload the sql files for the little testing I did.
if(isset($status) && array_key_exists($status,$status_types['sname'])){
NOTE: Small change to line 238 that is NOT done in attached copy. Used Posted Copy below.
<?php
session_start();
include ("config.php");
//////// config.php ///////
//$host = "localhost";
/*Database user name.*/
//$login = "";
/*Database Password.*/
//$dbpass = "";
/*Database name.*/
//$dbname = "";
//$db = new mysqli("$host", "$login", "$dbpass", "$dbname");
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
/*Assumimg "Meeting Mgr" will have an ID set to session as Mgr_id when they log in.
For testing I will force this variable below. Remove this section in live login mode.*/
$_SESSION['Mgr_id'] = "124";
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
/* Make sure manager is logged in.
Change ['Mgr_id'] session key if needed, e.g. ['user_id'] or whatever you are using. */
if(!isset($_SESSION['Mgr_id'])){
header("location: login.php");
exit;
}
//////////////////////////////////////////////////////////////////////////////
$Mgr_id = $_SESSION['Mgr_id'];
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
/////////////// Set Name, Background Color and Text Color ////////////////////
//////////////////////////////////////////////////////////////////////////////
$status_types = array(
"sname" => array("Not Defined","Normal","Marginal","Problem"),
"bgcolor" => array("#FFFFFF","#006600","#FFFF00","#FF0000"),
"txtcolor" => array("#000000","#FFFFFF","#000000","#FFFFFF")
);
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
// Two button styles. Round = blank, full rectangle = 2
$button_type = "";
//$button_type = "2";
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
////////////////////////////// Add New Meeting //////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
if(isset($_POST['AddMeeting']) && !empty($_POST['newmeeting'])){
$newmeeting = mysqli_real_escape_string($db, $_POST['newmeeting']);
$mgr_id = mysqli_real_escape_string($db, $Mgr_id);
$mod_date = date('Y-m-d H:i:s');
$sql = "INSERT INTO `meetings` (`meeting_name`, `mgr_id`, `date_created`, `date_modified`)
VALUES('$newmeeting', '$mgr_id', '$mod_date', '$mod_date')";
$db->query($sql) or die(mysqli_error($db));
}
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
////////////////////////////// Add New Item //////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
if(isset($_POST['newitem']) && !empty($_POST['newitem'])){
$newitem = mysqli_real_escape_string($db, $_POST['newitem']);
$Meeting_id = mysqli_real_escape_string($db, $_POST['Meeting_id']);
$mgr_id = mysqli_real_escape_string($db, $Mgr_id);
$mod_date = date('Y-m-d H:i:s');
$sql = "INSERT INTO `items` (`item_name`, `meeting_id`, `mgr_id`, `date_modified`)
VALUES('$newitem', '$Meeting_id', '$mgr_id', '$mod_date')";
$db->query($sql) or die(mysqli_error($db));
$sqlu = "UPDATE `meetings` SET `date_modified` = '$mod_date'";
$db->query($sqlu) or die(mysqli_error($db));
}
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
////////////////////// Update Item Status and Name ///////////////////////////
//////////////////////////////////////////////////////////////////////////////
if(isset($_POST['Save'])){
$Meeting_id = mysqli_real_escape_string($db, $_POST['Meeting_id']);
$mgr_id = mysqli_real_escape_string($db, $_SESSION['Mgr_id']);
$mod_date = date('Y-m-d H:i:s');
foreach($_POST['item_name'] as $item_id => $item_name){
$item_name = mysqli_real_escape_string($db, $item_name);
$status_name = trim($_POST['item_status'][$item_id]);
$status = array_search($status_name,$status_types['sname']);
$sql = "UPDATE `items` SET `item_name` = '$item_name', `status` = '$status', `date_modified` = '$mod_date'
WHERE `item_id` = $item_id AND `meeting_id` = '$Meeting_id'";
$db->query($sql) or die(mysqli_error($db));
}
$sqlu = "UPDATE `meetings` SET `date_modified` = '$mod_date' WHERE `meeting_id` = '$Meeting_id'";
$db->query($sqlu) or die(mysqli_error($db));
}
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
////////////////////////// Meeting Table Cells ///////////////////////////////
//////////////////////////////////////////////////////////////////////////////
$meeting_rows = "";
if(isset($Mgr_id) && !empty($Mgr_id)){
$meeting_rows .= "<tr>
<th colspan=\\"4\\">Meetings</th>
</tr>
<tr>
<td colspan=\\"4\\">
<form method=\\"post\\" action=\\"\\">
<div style=\\"width:75%; float:left; text-align:center;\\">
<input type=\\"text\\" name=\\"newmeeting\\" value=\\"\\" class=\\"name\\" placeholder=\\"Enter NEW Meeting Name Here\\" />
</div>
<div style=\\"width:25%; float:left; text-align:center;\\">
<input type=\\"submit\\" name=\\"AddMeeting\\" value=\\"Add Meeting\\" />
</div>
</form>
</td>
</tr>
<tr class=\\"subheading\\">
<td style=\\"text-align:center;\\">Meeting Name</td>
<td style=\\"text-align:center;\\">Date Created</td>
<td style=\\"text-align:center;\\">Date Modified</td>
<td style=\\"text-align:center;\\">View</td>
</tr>\\r";
$sql = "SELECT
m.meeting_id,
m.meeting_name,
m.date_created,
m.date_modified
FROM meetings as m
WHERE m.mgr_id = '$Mgr_id' ORDER BY m.meeting_id DESC";
$result = $db->query($sql) or die(mysqli_error($db));
while($row = $result->fetch_array()) {
$meeting_id = $row['meeting_id'];
$meeting_name = $row['meeting_name'];
$date_created = $row['date_created'];
$date_modified = $row['date_modified'];
$meeting_rows .= "<tr>
<td style=\\"text-align:center; font-weight:bold;\\">$meeting_name</td>
<td style=\\"text-align:center; font-weight:bold;\\">$date_created</td>
<td style=\\"text-align:center; font-weight:bold;\\">$date_modified</td>
<td style=\\"text-align:center;\\">
<form method=\\"post\\" action=\\"\\">
<input type=\\"hidden\\" name=\\"Meeting_id\\" value=\\"" . $meeting_id . "\\" />
<input type=\\"submit\\" name=\\"view_meeting\\" value=\\"View\\" />
</form>
</td>
</tr>\\r";
}
}
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
if(isset($_POST['Close'])){
unset($_POST['Meeting_id']);
unset($meeting_id);
}
//////////////////////////////////////////////////////////////////////////////
////////////////////// Build Status Table Rows ///////////////////////////////
//////////////////////////////////////////////////////////////////////////////
$display_rows = "";
// Check for POST meeting ID and query for data.
if(isset($_POST['Meeting_id']) && !empty($_POST['Meeting_id'])){
$Mgr_id = mysqli_real_escape_string($db, $Mgr_id);
$Meeting_id = mysqli_real_escape_string($db, $_POST['Meeting_id']);
$sql = "SELECT
m.meeting_name,
m.date_modified,
i.item_id,
i.item_name,
i.status
FROM meetings as m
LEFT JOIN items as i
ON i.meeting_id = m.meeting_id AND i.mgr_id = m.mgr_id
WHERE m.meeting_id = '$Meeting_id' AND m.mgr_id = '$Mgr_id' ORDER BY i.item_id ASC";
$result = $db->query($sql) or die(mysqli_error($db));
$h=0;
while($row = $result->fetch_array()) {
$meeting_name = $row['meeting_name'];
$date_modified = $row['date_modified'];
$item_id = $row['item_id'];
$item_name = $row['item_name'];
$status = $row['status'];
if($h==0){
$display_rows .= "<tr>
<th colspan=\\"2\\">
<div style=\\"width:25%; float:left;\\"> </div>
<div style=\\"width:50%; float:left;\\">" . $meeting_name ."</div>
<div style=\\"width:25%; float:left;\\">" . $date_modified . "</div>
</th>
</tr>
<tr>
<td colspan=\\"2\\">
<div style=\\"width:75%; float:left; text-align:center;\\">
<input type=\\"text\\" name=\\"newitem\\" value=\\"\\" class=\\"name\\" placeholder=\\"Enter NEW Item Name Here\\" />
</div>
<div style=\\"width:25%; float:left; text-align:center;\\">
<input type=\\"submit\\" name=\\"AddItem\\" value=\\"Add Item\\" />
</div>
</td>
</tr>
<tr class=\\"subheading\\">
<td>
Item Name
<input type=\\"hidden\\" name=\\"Meeting_id\\" value=\\"" . $Meeting_id ."\\" />
<input type=\\"hidden\\" name=\\"Mgr_id\\" value=\\"" . $Mgr_id ."\\" />
</td>
<td>Status</td>
</tr>\\r";
$h++;
}
// check for status
if(isset($status) && array_key_exists($status,$status_types['sname'])){
$status_name = $status_types['sname'][$status];
$status_color = $status_types['bgcolor'][$status];
$status_color_text = $status_types['txtcolor'][$status];
$display_rows .= "<tr>
<td style=\\"text-align:center; width:50%;\\">
<input type=\\"text\\" name=\\"item_name[$item_id]\\" value=\\"" . $item_name ."\\" class=\\"name\\" />
</td>
<td style=\\"text-align:center; width:50%;\\">
<input type=\\"text\\" name=\\"item_status[$item_id]\\" id=\\"item$item_id\\" value=\\"" . $status_name ."\\" class=\\"clr" . $button_type ."\\" style=\\"background-color:" . $status_color . "; color: " . $status_color_text . "\\" onclick=\\"return change('item$item_id');\\" />
</td>
</tr>\\r";
}
}
$display_rows .= "<tr>
<td colspan=\\"2\\" style=\\"text-align:center;\\">
<input type=\\"submit\\" name=\\"Save\\" value=\\"Save\\" />
<input type=\\"submit\\" name=\\"Close\\" value=\\"Close\\" />
</td>
</tr>
<tr>
<td colspan=\\"2\\" style=\\"text-align:center;\\">
Item names can be editing above. Click the Status button to toggle throught options.
</td>
</tr>\\r";
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<head>
<style type="text/css">
.clr {
border:1px solid #515151;
box-shadow:0 15px 10px -15px rgba(0,0,0,0.7);
overflow:hidden;
text-align:center;
font-family: Arial;
font-size: 13px;
font-weight:bold;
box-shadow:inset 2px 2px 5px rgba(255, 255, 255, 0.5), inset -2px -2px 5px rgba(0, 0, 0, 0.5);
border-radius:20px 20px 20px 20px;
}
.clr2 {
width: 100%;
border:1px solid #515151;
box-shadow:0 15px 10px -15px rgba(0,0,0,0.7);
overflow:hidden;
text-align:center;
font-family: Arial;
font-size: 13px;
font-weight:bold;
box-shadow:inset 2px 2px 5px rgba(255, 255, 255, 0.5), inset -2px -2px 5px rgba(0, 0, 0, 0.5);
}
.display {
width:700px;
margin: 5px auto 15px;
background-color:#DFDFDF;
color:#00000;
font-family: Arial;
font-size: 13px;
}
.display th{
background-color:#848484;
color:#FFF;
font-size: 12px;
font-weight:bold;
text-align:center;
white-space:nowrap;
}
.display .subheading td{
background-color:#E9E9E9;
color:#3E3E3E;
font-size: 11px;
font-weight:bold;
text-align:center;
white-space:nowrap;
}
.display td{
background-color:#FBFBFB;
color:#00000;
font-weight:normal;
white-space:nowrap;
}
.display .name {
width:98%;
background-color:#FDFDFD;
color:#00000;
font-weight:bold;
white-space:nowrap;
border: none;
border:1px solid #DFDFDF;
</style>
</head>
<body>
<table border="0" cellspacing="1" cellpadding="2" class="display">
<?php echo $meeting_rows; ?>
</table>
<form action="" method="post">
<table border="0" cellspacing="1" cellpadding="2" class="display">
<?php echo $display_rows; ?>
</table>
</form>
<!-- Using $status_types array we echo the value for each element in JS -->
<script type="text/javascript">
function change(objid) // no ';' here
{
var elem = document.getElementById(objid);
if (elem.value=="<?php echo "{$status_types['sname'][0]}"; ?>") elem.value = "<?php echo "{$status_types['sname'][1]}"; ?>", elem.style.backgroundColor = "<?php echo "{$status_types['bgcolor'][1]}"; ?>", elem.style.color = "<?php echo "{$status_types['txtcolor'][1]}"; ?>";
else if (elem.value=="<?php echo "{$status_types['sname'][1]}"; ?>") elem.value = "<?php echo "{$status_types['sname'][2]}"; ?>", elem.style.backgroundColor = "<?php echo "{$status_types['bgcolor'][2]}"; ?>", elem.style.color = "<?php echo "{$status_types['txtcolor'][2]}"; ?>";
else if (elem.value=="<?php echo "{$status_types['sname'][2]}"; ?>") elem.value = "<?php echo "{$status_types['sname'][3]}"; ?>", elem.style.backgroundColor = "<?php echo "{$status_types['bgcolor'][3]}"; ?>", elem.style.color = "<?php echo "{$status_types['txtcolor'][3]}"; ?>";
else if (elem.value=="<?php echo "{$status_types['sname'][3]}"; ?>") elem.value = "<?php echo "{$status_types['sname'][0]}"; ?>", elem.style.backgroundColor = "<?php echo "{$status_types['bgcolor'][0]}"; ?>", elem.style.color = "<?php echo "{$status_types['txtcolor'][0]}"; ?>";
}
</script>
</body>
</html>