Well, in order to compare old and new values you'll need to have these old values available at POST, which could be gotten from an extra hidden field in your form to pass old values back or making a query within POST for old values. I opted for the later in this example. We can then use array_diff() to see if values are different however this comparison is one-way meaning if A is different than B so we need to also check if B is different than A, if you follow what I'm saying. I've set the results of these two checks as $value1 and $value2. Then a simple check if !empty() qualifies the particular record as needing to be updated. Keeping a new query after POST processing gives you updated results for display.
PHP Code:
<?php
//Process "save"
if (isset($_POST['save'])){
//query books and build array
$books_array = array();
$sql= "SELECT id, bookname, booktype FROM books";
$result=mysql_query($sql);
while($row=mysql_fetch_array($result)){
$books_array[$row[0]]['id'] = $row[0];
$books_array[$row[0]]['bookname'] = $row[1];
$books_array[$row[0]]['booktype'] = $row[2];
}
//build a little array with book id being primary key, types as values
$bookkeys = array();
foreach ($_POST['branchname'] as $type => $selected_books){
foreach ($selected_books as $k => $bookid){
$bookkeys[$bookid][] = $type;
}
}
////////////////
if (!empty($bookkeys)){
foreach($bookkeys as $id => $array){
$old_types = explode(",",$books_array[$id]['booktype']);
$values1 = array_diff($array,$old_types);
$values2 = array_diff($old_types,$array);
if (!empty($values1) || !empty($values2)){
$types = implode(",",$array);
$sql = "UPDATE books SET booktype = '$types' WHERE id = '$id'";
$result=mysql_query($sql);
}
}
}
}
//query books and build array
$books_array = array();
$sql= "SELECT id, bookname, booktype FROM books";
$result=mysql_query($sql);
while($row=mysql_fetch_array($result)){
$books_array[$row[0]]['id'] = $row[0];
$books_array[$row[0]]['bookname'] = $row[1];
$books_array[$row[0]]['booktype'] = $row[2];
}
//query book types and build array
$books_types = array();
$sql= "SELECT typename FROM booktypes";
$result=mysql_query($sql);
while($row=mysql_fetch_array($result)){
$books_types[] = $row[0];
}
$content = '<form name="myform" method="post" action="">
<table>';
$booktype_cnt = count($books_types);
$rowcnt = $booktype_cnt+2;
$content .= "<tr><th colspan=\"$rowcnt\">Select Book Types</th></tr>\r";
$content .= '<tr>';
//use $books_types array to build form table headings
foreach ($books_types as $book_type){
$content .= "<td align=\"center\">$book_type</td>\r";
}
$content .= "<td>books</td>
<td>no</td>
</tr>";
//use foreach to run through array of books
foreach ($books_array as $books){
$content .= '<tr>';
//use $books_types array to build form checbox set
foreach ($books_types as $book_type){
$types_array = explode(",",$books['booktype']);
$checked = (in_array($book_type,$types_array) ? " checked=\"checked\"" : '');
$content .= "<td><input type=\"checkbox\" name=\"branchname[$book_type][]\" value=\"{$books['id']}\"$checked /></td>\r";
}
$content .= "<td>{$books['bookname']}</td>
<td align=\"center\">{$books['id']}</td>
</tr>";
}
$content .= "<tr><td colspan=\"$rowcnt\"><input type=\"submit\" value=\"save\" id=\"save\" name=\"save\" /></td></tr>\r";
$content .= '</table>
</form>';
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
</head>
<body>
<?php
echo "$content";
?>
</body>
</html>
Bookmarks