php method to implement product notification: 1. Create js code and set Ajax to request the interface every 10 seconds; 2. Query the database for new orders; 3. Through "public function sendOrderNotice(){ ...}" to implement order reminder.
#The operating environment of this article: Windows 7 system, PHP version 7.1, Dell G3 computer.
How to implement product notification in php?
PHP uses AJax polling to realize real-time reminder of new orders
Business logic: Ajax request every 10 seconds One-time interface, this interface will query the database to see if there are new orders. If so, it will return the number of new orders. The background will receive a sound prompt and change the number of background reminders.
The reminder box can be linked to the order list, and the background changes The reminder will disappear after completing the order status
This logic can also be used to implement the background notification function, and it can also be implemented using scheduled tasks
1, JS code
<audio id="mp3" src="/admin/mp3/remind.mp3"> </audio> <script src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script> <script> var remind = 1; var mp3 = $("#mp3")[0]; var play= 0; if(sessionStorage.num){ $(".remind").text(sessionStorage.num); } $.ajax({ url:"/sendOrderNotice", success:function (data) { sessionStorage.num = data; $(".remind").text(data); remind = data;play=data; remind<=0?$(".remind").hide():$(".remind").show(); } }) setInterval(function () { $.ajax({ url:"/sendOrderNotice", success:function (data) { remind = data; sessionStorage.num = data; if(play==remind){ remind<=0?$(".remind").hide():$(".remind").show() }else{ $(".remind").show().text(remind); mp3.play(); play=remind; } } }) },10000) </script>
2. PHP interface
/** * 订单提醒 */ public function sendOrderNotice(){ //查询order表是否有新订单 $NewOderCount=Order::getNewOderCount(); if ($NewOderCount) { echo json_encode($NewOderCount); } else { echo 0; } }
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of How to implement product notification in php. For more information, please follow other related articles on the PHP Chinese website!