Home > Backend Development > PHP Problem > How to implement product notification in php

How to implement product notification in php

藏色散人
Release: 2023-03-14 18:12:01
Original
1564 people have browsed it

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.

How to implement product notification in php

#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>
Copy after login

2. PHP interface

/**
     * 订单提醒
     */
    public function sendOrderNotice(){
        //查询order表是否有新订单
        $NewOderCount=Order::getNewOderCount();
        if ($NewOderCount) {
            echo json_encode($NewOderCount);
        } else {
            echo 0;
        }
    }
Copy after login

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!

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template