Hello, I have store and I want to add box on the page that show if some product is still available. Is it possible to print the number of product items left liveon the page ? live mean - if someone buy product, the box will show the new total without refreshing for the other visitors... ? Thank you in advanced, Roi.
Hi Roi. You can do it with AJAX. In javascript create a function which repeatedly will send requests to a server side script. This script asks database for actual data and return them into javascript, where you can display it to user. If the interval will be so high than database will be under pressure. You can also display to user when data from AJAX were refreshed. Feel free to PM me and I can do it for you.
can you send me link to example? What will be the frequency of the "asking" ("This script asks database for ...") ?
Here is an example how AJAX works - javascript + PHP + database MySQL: http://www.w3schools.com/PHP/php_ajax_database.asp The frequency "This script asks database for ..." depends on you. If you don't need LIVE results (e.g every 3 hours refresh is enough), than you can add something like cache on server side. So server side script will not request everytime into database, but into valid cache. It is all about your available resources on server side. If you have resources, than you can ask from AJAX e.g. every 10 seconds .
In your example, if someone will add new row to the DB, will it immediately appear on the page (without refreshing) for the other users?
No, I didn't find the script you exactly need, but this script is close to your needs with changes I have described before. :-;
Emm, I'll tell why I need that: I have store with 10 products. because some ad I publish I got 1000 users that enter to my store and want in the same time to buy it. So in the first second they all see that I have products to sell, but during the buy progress stock may end and I need to print the right msg...
You can add another logic to your eshop. Every user will send AJAX request every e.g. 10 seconds which returns actual stock status + actual reserved items status. 1) User 1 adds product 1 into cart(see reserved items 0 for this product). 2) Ajax send info about this into database 3) In database you will have new table - reserved items, and you add this product with this user into this table + time when the reservation expire 4) User 2 want to add product 1 into cart and see actual status in stock and actual reserved item: 1 (step 2). So user 2 knows that there are some orders which are not finished and that somebody has in cart also this product. So user 2 knows that if he not be fast somebody else will buy this product from stock. 5) But User 1 will have this info too. You can display items from cart on every page with actual stock info and actual reservations info for every user. But if you have a scenario with 1000 users which want buy 10 products within a short time than I suppose the one way is decrease AJAX requests frequency directly into database. As I know you cannot see live status of database when database is changed. You have to ask via AJAX(so there is a risk that you will display not correct data).