doug_mize
Posts: 12
|
| Posted: 04/08/2008, 7:41 PM |
|
Hello!
I am making an editable grid where users can add/delete records by simply clicking a custom "Add/Delete" button at the end of the each row. I didn't want to use the standard "Delete" checkbox and "Submit" button because I want the record to be created or deleted on the fly, one record at a time, by simply clicking one, single button.
So, I built an editable grid and removed the "Delete" checkbox and added my own "Add/Delete" button/row that uses the following custom code to trigger a php file that adds or deletes a record in a completely different table (than the tables being displayed in the grid) when a user clicks the button, by first checking to see if the record already exists and then acting accordingly.
Button code (via the wizard):
<Form> Properties
Form name: add_delete_master
Target frame: (left blank)
Action: add_delete_master.php (my php file that does the work)
Method: Post
Encoding type: (left blank)
/////////////////////////////////////////////////////////////////////////////////////////////////
And here is the code from my add_delete_master.php file:
// Checking for a duplicate record
$query2 = "SELECT * FROM custlist WHERE custnum='$user' AND listname='$custom_list_name' AND symbol='$symbol'";
// Execute the SQL query
$result = mysql_query($query2);
// Loop through the result set
$row = mysql_fetch_array($result);
// Retrieve the columns from the results and set row into local variables
$cusid = $row[custnum];
$list = $row[listname];
$sym = $row[symbol];
// Check local variables against one another
if ($cusid == $user && $list == $custom_list_name && $sym == $symbol) {
mysql_query("DELETE FROM custlist WHERE custnum='$cusid' AND listname='$list' AND symbol='$sym'");
}
else
{
mysql_query("INSERT IGNORE INTO custlist (custnum, listname, symbol)
VALUES('$user', '$custom_list_name', '$symbol')");
}
/////////////////////////////////////////////////////////////////////////////////////////////////
You see, I'm first querying the table where the record is or will be written to see if there is already a record that matches the data I'm about to write, AND, if there is a matching record it will delete it. If there is not match, it will write one.
Notice that in order for me to check and see if there is, indeed already a matching record I queried the custlist table and then broke out the individual fields into variables ($cusid, $list, $sym) that I could compare against the input data (also variables like a customer number taken from a cookie and a custom list name taken from a custom list listbox on the user's main page).
All is working like a champ...
EXCEPT for one last detail:
Now I need to figure out how to pass a particular grid field value to my php driven duplicate check.
In my code you will see a variable called $symbol. For testing purposes I hard coded the value on the add_delete_master.php file and, like I said, all works like a champ.
But for my "Add/Delete" button to add or delete more than a single, measly record (with my hard coded $symbol variable) I need to populate that $symbol variable with the value of the symbol field in my grid, and of course this needs to be the symbol field from the same row where the custom "Add/Delete" button resides in my grid.
Anyone that can help me get that value and pass it to my $symbol variable will be a lifesaver.
And for anyone that wants a single "Add/Delete" button at the end of the grid row that checks for duplicates and acts accordingly... enjoy my code. It took me two days of brain wracking to make this work.
And yes, I'm a super newbee. I just got CCS 4 about a week ago.
Much love in advance!
Doug
|
 |
 |
|