Compare $_POST array with database - php

Ok, I'm pretty sure I'm not the first one to run into this, I just don't know the right words to search for a solution.
Consider the following database tables (simplified for this question):
database tables http://www.nth-root.nl/public/images/wolf/cat_attr.png
As you can see in the picture, there is a category table that holds product categories and an attribute table that holds different attributes. For instance: Color (for clothing), Capacity (for mp3 players), etc.
The third table, in the center of the picture, links the attributes to the categories.
Now in the backend I have an 'Edit category' page with a form to edit (or add) a category. On the same page I want to display a list of dropdown lists in order to manage which attributes are linked to the current product category.
backend http://www.nth-root.nl/public/images/wolf-commerce/edit_category.png
It uses jquery to add / delete dropdowns.
The problem is: how do I manipulate the data in the category_attribute table, using the $_POST data of the form.
The lazy solution would be (and yes I've done this in the past):
1. Delete all current rows in category_attribute for the given category
2. Then insert all selected attributes to category_attribute
But this isn't very efficient, it deletes and re-adds all rows even if someone only changes the category's title.
So I'm looking for a way to do the right inserts, updates and deletions:
1. Delete the rows that don't appear in $_POST (the dropdown has been removed)
2. Update the rows that are changed (another option from the dropdown has been selected)
3. Insert rows that are new (a new dropdown has been added)
I'm just not sure how to do it or where to find a good example. It's probably easy to do with some ajax but I am looking for a solution that's purely php, comparing the $_POST values with the values in the database.
If someone knows a good article that explains the best (and most efficient) solution, please let me know, I'd be very grateful.
-edit-
I had included some pictures to clarify my question, but stack overflow refused to include them because my reputation is too low, so I hope my question is clear enough without the images.
-edit2-
Now I have enough reputation, I've added the images.
Thanks in advance,
Nic

Insert and Update:
You can insert a hidden input with the ID records.
Those who are new will reset this ID, so you do the insert.
The ID's that have filled you make an update. If you only want to perform the update really changed the items you can create a hidden input "UPDATED" with value "0". Via javascript you can check if the combo has changed, so that input marked as "1".
Exclusion:
Likewise you can do to exclusion, creating a hidden input scoring records that should be deleted when the user clicks to remove.
Do not know if it's the appropriate way for you, or if you expect a more practical solution.

Related

dynamic treeview with html,php and javascript and mysql

I have created a table in mysql database using php, and also i can displayed that table on the web form, But the thing is that i want to do, I have six field in my table.
That are,Sr_no which is auto increment, Process_no, Process Name, Ownership, Sheet Revision_no and Revision Date.
Now i want to make a dynamic tree view type in my field Process_no.
For eg: i have the value in that field M01, so when i'll click on M01 then there should be a sub-list under it, such as M01.1, M01.2,.... and so on. and this thing i want in every column of that field.
i had tried a lot but fail to do it.
if you have any solution or any code for it, then please help me.
i'm not a experience candidate, new at php and mysql.
so please help.

Database Concurrency Problems

I'm currently developing a database/website server interface to facilitate inputting data for a data collection project. There are two types of additions being made to the database: A and B here. Some of the tables in the database that handle these entries are as follows:
dcs_projectname_a
dcs_projectname_b
Each of these have tables for all the required input fields in addition to things like creator, timestamp, etc.
The pages on the website facilitate three different options: add, view, and edit. Each page for each type of entry performs the respective function. That is, the add page adds, view page views, etc.
I am just about done; however, there is a major challenge I haven't really confronted yet: concurrency. There will be multiple users adding content to the database at the same time. Each entry is given its own specific id and there CANNOT be any duplicate id's. That is, the first a entry is A000001, the next is A000002, and so on.
On the add and edit pages, there is a disabled field for the user to view the id for other uses when physically documenting entries.
What I need to figure out is how to implement concurrency management so that when users are concurrently adding a's that they will not be under the same id and row.
The add page automatically generates the id to be used by accessing the database's most recent id and adding one.
My thought was to create a new row in the table every time the add page is opened and give it the calculated id. Then, when information is added it performs a modification to that existing row. This way, if another user opens the add page while another entry is currently being added it will be given the future id, not the same one.
With this method I need a way to delete this entry if the user leaves the add page or closes the browser. Then, I also need other users with open add pages to automatically update their id's to one less when the first user (or any other user less than the most id being used) leaves their add page and cancels the addition.
This is kind of a complicated explanation and if you don't understand let me know and I'll try to answer as best as I can. Any help is much appreciated!
Thanks
There's a number of solutions to your problem, but you seem to have made things harder by having your application generate the record IDs for you.
Instead, you could just be using MySQL's AUTO_INCREMENT functionality to automatically generate/increment the record ID for you (upon insert). MySQL will ensure that there are no duplicates, and you can get rid of the extra database call to retrieve the most recent ID.

Multiple Listboxes to Select Data(table1) and Insert to MySQL Table2

I have two tables, incldues and TIncludes which are used for storing three columns of data; An ID, macro_num, macro
The includes(table1) has all of the items possible to list, these are shown by macro_num in a listbox on the form (select1). The user can add the wanted items to the second box, select2. The listboxes populate and move the data to eachother, add/remove buttons using javascript.
I can't seem to get the data from select2 to insert into my database. Not sure what I setup incorrectly or overlooked.
Are there any working examples of such a thing? I have only seen where one item is inserted and that is fine, but I have some reports which need 40 items.
Thanks in advance for reading this. I hope there may be a quick solution as I have not dealt with this problem before.. my first large form item requirement.
The PHP form handler is only going to know about the options that are selected in your list. You'll need to do more than just add the items to a select element to get them to your code. You can add them to hidden elements, or you might be able to force a select-all through javascript as the form is submitted.

UI design challenge for this situation

Screenshot mockup: http://tinypic.com/r/y2qex/5
Problem: I have a table that has 53 columns; one for each week of the year, plus one with the user name. It will have anywhere between 10 and 80 rows, depending on the number of users for each area.
The users need to be set a “flag” for each week, such as Annual Leave, Training etc.
I currently have a table, which has a select box in each cell. The problem is this works for 5 rows, but once I start getting 20+ rows, the browser wont open the page, because there are just too many select boxes.
Whatever new selections are picked must be able to be queried, so I can save them in my DB.
What I’m after are some generic ideas (i.e. not specific code) on how I can better solve this problem. Once I get a good idea, I’ll go off an work out the exact coding.
My ideas so far:
- Make all cells text only, with the current selection, then have an ‘edit’ option beside each user, which opens their row as a modal window which can be editted
- Make all cells have a “onClick” event, causing a dropdown list to be generated at the point of click
But I’d be keen to hear how other people might approach/solve this problem?
If the options are the same for many select boxes, you could consider using one datalist for all of them, this would be more performant, and I'm guessing allow you to have more per page. Unfortunately this is an HTML 5 feature, so it would not be backwards compatible with all browsers.
http://www.w3schools.com/html5/tag_datalist.asp
Other than that, you could consider pagenating your table if it gets over a certain number of columns. Or do like a tumbr thing, where more columns load via ajax if they scroll to the right far enough. You idea also should work.
You might want to look at using a calendar feature, I'm sure there's a ton of Javascript calendars out there. I also have had a lot of success lately using DataTables. You could use DataTables + jEditable to create a click to edit table representation, that when clicked gives you a select box, but otherwise shows only text.
Perhaps you could have a single hidden select box on the page and display it on a cell when clicked, and handle the result of the click by writing a data-attribute to the cell, and perhaps doing a simultaneous XHR?
You could also just have a bunch of hidden form elements, but that would be gross.
Implementation-wise, you could do it with a single event handler attached to the table, with each cell having data-attributes representing name and week.
Anyway, this should be performant, even though it would require an extra 20 or so lines of js.
Maybe something like this could work for you:
var td=document.getElementsByTagName('td');
for(var i=0; i<td.length; i++)
{
td[i].id='cellID_'+i;
td[i].onclick=function()
{
//make menu appear on this element id
}
}

What's the best way for the user to add multiple details to single area of input?

In my project the user has to detail his management expenses in four different areas. At first we had a simple table (this one) where he had to write all the data grouping all the expenses description in a textarea, now we want him to detail each area, so i have to give the user the possibility to add "rows" to each area. That means that if his "Impegni" expenses consists of three entries he must enter three "rows".
My idea is to use jquery to let the user add rows and modify the rowspan of the first column when the user adds/remove a row (that is, if you need to enter another row for "Impegni" i create a new row under the third row and add "colspan=2" to the "impegni" cell) but maybe there is a better way to handle this kind of input.
I try to explain it even better: the four rows that are present in my table are the "master" areas and the user must add details to those areas. Creating new "sub-rows" for each area it's an idea, but maybe there is a better way to handle it!
EDIT - another idea is to have four different tables (one for each one of the "master" rows) and show hide them with tabs (look at example five on this page) but i don't know how to handle the sum of all the rows
What do you suggest?
My friend has made a plugin for it, you can look here.
I think jquery is the best way, you can access your database with simple ajax command from jquery, like .load()
:)

Categories