I have a table in MySQL test_info . This table gets an insert command at random times such that a new record is added. This insert can be fired from anywhere.
Actually what have to do is?
front end should display the data which was inserted into the table recently in a real-time fashion.
please suggest me how solve this problem
You would use $mysqli->insert_id to retrieve the id that was auto generated and used by the last query. Check it out here.
Edit: Based on Thorsten Kettner's comment, you could also use SELECT LAST_INSERT_ID(); which to my understanding isn't based on the current connection and instead is referenced globally.
You could even use SELECT MAX(ID) to get the last ID within a table, which most likely would be the most recent if set to AUTO_INCREMENT.
First, for your client side, check out the Server-Sent-Events.
Secondly, you need a mechanism to check for latest updated data at your server side
You can use a mysql query to get last records. In my example shows how to get last 10 records.
here is the example
select * from table order by date DESC LIMIT 10
insert_id has some restrictions and I'm not sure how you will differ inserts from deletes.
What I would do will be *on insert trigger in each table where you need to track the insert
(may be you will want to track on update and on delete as well)
triggers
.
what you probably need is a separated table - historical_data_tab which holds all the historical data triggered by any event which you want to track.
for mysql :
USE `db_name`;
DELIMITER $$
CREATE DEFINER=`root`#`%` TRIGGER track_insert AFTER INSERT ON your_table
FOR EACH ROW BEGIN
insert into historical_data_tab
values (new.ID, 'INSERT',NOW());
END
pay attention to new.ID and NOW(). In case you don't need the original PK, just insert NULL in place of new.ID and any Auto Increment PK will take care of the rest. NOW will populate historical_data_tab table with a timestamps of the time the event was triggered. INSERT should tell you what type of event it was.
Replace it according to the trigger you'r using.
you might want to keep data about any of the following events*
after delete, insert, update
or
before delete, insert, update
p.s. There are a lot of ways to do what you need. Don't go crazy with triggers and try catching front end events in the front end itself
select * from your_table where ID = Max(ID)
Related
I needed a little help over here on adding dynamic columns to table and I'm using smart-table project https://lorenzofox3.github.io/smart-table-website/
Please look at plunker
https://plnkr.co/edit/uTDa6hfwdgGtGWkYqL7S?p=preview
Click on add new column, it will update the table header, but I can't update the td element because I haven't created or binded any td element for new column.
Need help on two things.
1) Any number of new columns might come in future from server dynamically, so I can't bind any element statically in html like I did right now.
2) This is a little long one(I want to create a only one header as MV and in colspan I want current and next) and How do I accomplish that one ? If I divide like that, how do I go frame the data ?
Additional Note on Question 1: I have tried dynamically showing the data in table. please look at line 27 in index.html. But again, If I go with this approach, the values get binded to different different columns since, data's are framed dynamically on controller. To be simple, there is no guarantee that data will be in in this order.
(SNo, companyName, companyFullName, MarketValueCurrent, MarketValueNext, QuarterCurrent, QuarterNext)
companyName might come first or at last. If I go by dynamic looping using ng-repeat, it binds MarketValueNext in first column, then companyName, etc in whichever the order the data is coming.
Thanks in advance.
I am developing a web application which will have images displayed in a slider. It is important that I am able to modify the order that they are displayed in.
Currently, I have a column in the table of images called 'order', which must be set to ensure the ordering works correctly, but changing it requires changing every other, too, which becomes tricky when dealing with hundreds of records.
This table, as well as queries for modifying it, are in development, so it's completely fine to change the way I do this.
My questions are:
When I'm inserting a new row, how can I make sure it appears at the end of the list? Auto-Increment in SQL tends to leave gaps if you delete entries from the table. How do I make sure that the row is assigned an order 1 greater than the highest order in the table?
How do I reorder the table? Imagine that I use a drag and drop interface (or similar) to reorder one image, bringing it to the top, or a different part of the list. If I do that, I need to reset the numbering of every item, which is, again, inefficient when dealing with hundreds of rows.
How do I prevent two rows from having the same order at any given time? The UNIQUE flag on the column? Presumably if I set that flag, changing the order from, say, 8 in one row opens up another to use that number. Right?
Thank you for your time, and I hope this isn't too vague a question to be easily answered!
Query the table you are about to insert into to find the MAX id in the table. Then on your insert just do this +1.
When re-ordering, say you want to set image order 6 to order 3. You will want to update all rows from order 3 and onwards to order+1 then set the image to order 3.
UPDATE table SET id = id+1 WHERE id IN (SELECT * FROM table where id >= 3)
If you are using unique on the order column to re-order you will have to update the current value to a temp value such as 99999, then use method in 2. However method 2 should keep this table from receiving any duplicated values.
sidenote
You could on delete of a picture re-evaluate the ids to keep no gaps
UPDATE table SET id = id-1 WHERE id IN (SELECT * from TABLE WHERE id > 3)
Where 3 is the deleted id
It will be in the end of the list. Just use auto increment feature and do not set ID manually (like lastID + 1) and you'll never have described problem.
You can allow user to change order of the list and then just update all order cells (for each member of the list). Or you can use Ajax and exchange order value for two members every time user drag-and-drops.
Use one of approaches described in 2 and you'll never have this problem.
I have a page to edit the 'utilization' rate of each employee, based on their designations. So the page has all the designations listed as , next to which a textbox, for the user to fill in the utilization rate. Scenario is that the user will not save each designation's utilization rate immediately after filling it. User will keep going till he fills the last item and then hit the submit button to save. Now, in php I can get all the values from the Request Array. But, how will I know which designations these values belong to? So, what i have as a solution is to name the textboxes with the designation_ids as suffix. May be like;
utilTextbox_1, utilTextbox_2, utilTextbox_3 etc...
Then when the form is submitted;
Check each Request Array element to see if its name starts with 'utilTextbox'
If Yes, split it using '_' to get the designation_id
Update the db table with the value of the text box
Check the next Array Element.....and so on...
is this the correct method or is there a better way of doing this?
You can have array as names in html forms. So something like utilTextbox[1], utilTextbox[2], utilTextbox[3] will work perfectly fine. In php you will get an array in $_REQUEST and you need not to convert in an array because it is already an array.
is it possible to drag row that contains Id for a Order att drop it on anothe table and get all data with this Id on the id table on MVC?
What im trying to do is that i have table for "ProductionOrders" and i have table for "Orders"
i want to drag a Order to ProductionOrder table and save that. so that means the dropes Order will be in Production.
so Order table has rows with order name and another column with order id. i want to drag it and when i drop it on the other table i want to got the details like " order name, date, description, and so in "
Any idea? tutorial that can help ?
*update on 6/16 * :
please refer to my answer at
how to set Individual rows marked as non-draggable and/or non-droppable? for more information
sure its possible, use jquery ui draggable and jquery dropable, and an ajax post.
when the item is dropped into the target list fire an ajax post event.
the post event would hit a method that does the database work to remove the record from the 1st table and add it to the 2nd table.
http://jqueryui.com/droppable/
http://jqueryui.com/draggable/
Specifically see the demo at
http://jqueryui.com/droppable/#shopping-cart
you're going to do something like this, with an extra ajax event to hit the server side
I'm working with GWT-2.1.0 and I've draw a table through CellTable which contains a column, the third, of EditTextCell. I'm trying to modify the value of that cell for each visible rows by code using:
table.getRowElement(i).getCells().getItem(2).setInnerHTML("<div style=\"outline:none;\" tabindex=\"-1\">0</div>");
Window.alert("Pause");
Thanks the alert I can see that all the rows have been updated correctly to the new value, but, once the cycle ends, a refresh of the table restore the user's input nullify the job done.
Is there some temporary cache that EditTextCell uses to mantain the data? Can I erase the text inserted by the user in another way? Can I reach the Column of the CellTable so allowing me to use the setValue(...) field?
Anyone can help?
Thanks in advance!
P.S. Using *.setInnerText("0"); fails too.
P.P.S I've read that GWT2.2.0 should have a CellTable.getColumn(int index) method to do so, but I don't know if it's useful to me - and, more important, when it should comes out.
EditTextCell is a subclass of AbstractEditableCell, which has the clearViewData method. You can call that method for all objects in the table for which you want the data to be cleared.