This is a question that I have been pondering for a long time, but didn't want to ask because I wasn't sure how to describe it.. I'm still not sure if I can describe it well but here it goes..
I have a web app that allows you to manipulate a bunch of elements on the page, but has one save button. When I hit save I would like to create/update all of these changes in one POST (not incrementally). If these html elements were created for the first time on the page, I would like to insert them as new entries into the database. When these changes are saved in the database for the first time (created), I return the index ID, so if I make changes again, they will be updated in the database instead of created again.
What makes it tough is batching this save so it doesn't take up all this bandwidth. I want to be able to mix and match creates and updates, but sending back IDs from the created elements and mapping them to the correct html elements (so they will be updated next time) requires me to know something about the order of each batched element which leads to some issues. I was wondering if there is a clever way to do creates or updates, and map the IDs correctly back to various elements under one ajax request.
Hopefully this was clear, let me know if you need clarification
Thanks,
Matt Mueller
You can just use negative auto-decremented ids for newly created elements and return a map from negative to positive ids. E.g., send [{id: 507, name: "foo"}, {id: -1, name: "bar"}, {id: -2, name: "baz"}], return {-1: 510, -2: 511}, and have your javascript update its ids based on the map.
well, upon loading, fetch next ID from db, so you know, what ID will have next page part in database. then, upon creation, in javascript you can work with this - i.e:
Next ID is 15, so when you add next field, it's ID is 16 and there is ID 17 in stack... and when you delete one newly created field, isn't problem to shift every higher ID one down.
Then, in your saving script, you know, that every ID larger than what you previously fetched is "to be saved" and every smaller is to be updated.
If your page knows when a new element is created, then it should arrange for the "id" parameter for that group of parameters (i.e., the attributes of the entity to be added) to be either null (not supplied) or some marker value. Already-existing entities have their ID value, which would not be changeable by the client. The server simply has to separate out the groups of parameters with empty ID values from those with non-empty values.
Your client may also want to mark elements for deletion. In that case, the already-existing entities would have their ID sent back with some flag parameter indicating "DELETE ME". Entities that are created and then deleted before "submit" would need no server processing.
Related
I have two pages, one is a new player page which takes information from a user via text box and adds a new entry to the players db and creates a new auto-incremented primary key called playerID. This is working fine.
I then have another page called stat input where the user will enter statistics for an individual year for an existing player. I need a way for this second page to know the playerID of the player whose information is being added. I am not unsure of a good way to do this. Any ideas are appreciated.
Edit: I guess I wasn't very clear. The user may be going directly to the stat input page to add a new entry to an already existing player. The user will not know the playerID and likely only the name of the player.
There are a few ways you could do this. Once you have the player ID, you can store it in a PHP session variable (if you are open to using server-side sessions). You could also pass it in a GET or POST parameter to the second page.
Edit:
As per the additional information you gave, I would go with a dropdown box with each player name. In the box, the "value" attribute of each can be the primary key ID of the player.
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.
When a PHP page is displayed then I want to start a thread searching for the last value entered in a MySQL table column so that the value of a text input field should be this last value incremented by 1 (it's a number). I want a thread because the page may be displayed for a long time and during that time the column may has been updated many times, so I want to get the value each period of a time interval. When clicking the submit button then the value should be last one in the column. Is that possible ?
Consider this scenario :
one user displays the page , so the field contains the incremented value. Then another user displays the same page, so the field contains also the same incremented value. When they submit the form then there is concurrency problem because they submit the same incremented data ! So how to resolve that situation ?
one user displays the page , so the field contains the incremented value. Then another user displays the same page, so the field contains also the same incremented value. When they submit the form then there is concurrency problem because they submit the same incremented data ! So how to resolve that situation ?
You will not be able to keep abreast of all the possible changes to the highest value in real time. An Ajax based solution which periodically fetches the value will not be fast and reliable enough. What happens if the highest value changes in exactly those 100ms that my request takes to make it to the server? What if a user's network connection is temporarily down, he clicks "save", and two values end up overwriting each other?
The usual solution is to determine the highest value not in the user interface, but at the time of storing the record in the database. A way to achieve this effect automatically is using an auto increment field in the database.
If you absolutely need to display the current value to the user, you will need to employ some sort of locking mechanism. Ie. determine the currently highest value, and reserve that value for the current user until they either save the record, or leave the page. But there is simply no way to display the highest current value to every user in a 100% reliable way. You will end up with collisions this way. Like #N.B. says in the comments, you probably should rethink your design.
You can achieve this through AJAX easily by calling the database or a controller asynchronously every 1 minutes for example - or on a particular mouse event perhaps - and returning JSON data or a view with displayed data. Many popular social network sites do this to get the latest feed / data displayed on their home page.
More info on: http://ajaxpatterns.org/Periodic_Refresh
I have a form with a dynamic table on it plus other fields. You start with one row and can add more at execution time. It's used to track incoming and outgoing items on a store. You add each item and then fill some common fields (time, date, person who received them, etc).
General fields are stored in a table, detail ones in another table which is related with ID field of the first table. I send first the general fields with jquery ajax, save them on db and then use the ID for saving the details with other ajax call. When finished saving, I clean the inputs with jquery and if I add another registry the previous items are also added unless I reload the whole page. I use mysql+php+jquery 1.7.1
How do I clean the Array? or what is the best method for doing this?
Tell me if you need some code, thanks in advance...
In a PHP application I'm building, I'd like to have an 'editable' table. The idea is that each row will have an edit button, which, when clicked, will replace certain fields with text fields and select lists and change to a save button. When the user clicks save, the data data should be validated and changed if appropriate.
I'm mainly tackling this as a learning project (I'm aware there's a ton of stuff already out there) and to see if I can get anything 'cool' working. I've created a PHP table-generating class that can take an array of objects as a datasource, and can have columns created based on those class methods.
e.g.
$table = new Table($dataSource);
$table->addColumn('Name', 'getName');
$table->addColumn('Amount Due', array('getOrdersManager', 'getTotalAmountDue')); //First calls getOrdersManager() on each data item and then calls the getTotalAmountDue() on the result
I'd like to try my hand at extending this to be able to the table row and have those changes reflect on the corresponding object in the data source.
I don't really have very much experience with AJAX although it's clearly going to play a very important role in getting this to work correctly.
Any tips on how I should approach such a task?
Edit: I'm not really interesting in looking at Ajax libraries at this point (I do have some experience with jQuery). I'm more interested in learning the basics of Ajax at this point.
my tip is to use jquery(does most of the heavy lifting for you and is easy to learn).
The idea is that each row will have an
edit button, which, when clicked, will
replace certain fields with text
fields and select lists and change to
a save button
http://api.jquery.com/click/
When the user clicks save, the data
data should be validated and changed
if appropriate.
http://api.jquery.com/jQuery.post/
Some things to be aware of/think about:
Are you going to send every field change to the server, or only the whole row (the latter is more resource efficient, but not necessarily as accurate)
How are you going to ensure the data displayed stays accurate even if the update to the server fails for some reason (either a network failure or a DB/validation error)
How will you ensure the user has permission to update the record and that you don't open a security hole by allowing the AJAX responder just to update whatever record it is told to. My approach has been that if a record is shown in the interactive table then the user has the permission to update it, so a cache of record IDs is held in the session when the table is created
Are you going to load options dynamically? If you don't, then a long table can end up containing a lot of HTML because of repetition of the select controls, but again it is more resource efficient not to have a request every time a user clicks into a dropdown. One compromise might be to put the options into a hidden HTML field and load them dynamically into the correct place when a user clicks a dropdown