CodeIgniter——使用setInterval自动从数据库中获取数据

时间:2022-10-07 00:09:15

In codeigniter, I want to display the change in the database automatically without reloading the page so i use ajax to run a controller function in my view. I'm using setInterval to run the function over and over again until the controller function listen_auth returns 1 and the view displays 1.

在codeigniter中,我希望在不重新加载页面的情况下自动显示数据库中的更改,因此我使用ajax在视图中运行控制器函数。我使用setInterval反复运行这个函数,直到控制器函数listen_auth返回1,视图显示1。

VIEW:

观点:

<h4 class="qr-title" id="status"></h4>

<script>
    var username = <?php echo(json_encode($username)); ?>;

    function checkAuthStatus() {
        setInterval(getStatus, 1000);
    }

    function getStatus() {
        var isAuth = <?php echo(json_encode($isAuth)); ?>;
        $.ajax({
            url: '<?php echo base_url('auth/listen_auth'); ?>',
            type: 'post',
            data: {username:username},
            success: function(data){
                console.log(data);
            }
        });
        document.getElementById("status").innerHTML = isAuth;
    }
</script>

here's the listen_auth() function in my CONTROLLER:

这是我的控制器中的listen_auth()函数:

public function listen_auth(){
        $username = $this->input->post('username');
        $isApproved = $this->adminmodel->get_auth($username);
        if($isApproved == 1){
            return 1;
        } else{
            return 0;
        }
    }

The problem is that isAuth variable will only change once the page has been reloaded... Am I doing something wrong? Or is there any better way to do this?

问题是isAuth变量只有在页面被重新加载之后才会改变……我做错什么了吗?或者有更好的方法吗?

2 个解决方案

#1


1  

The function from server 'listen_auth()' should print text not return.

服务器“listen_auth()”中的函数应该不会返回文本。

public function listen_auth(){
    $username = $this->input->post('username');
    $isApproved = $this->adminmodel->get_auth($username);
    if($isApproved == 1){
        echo 1;
    } else{
        echo 0;
    }
}

And then get the answer from server in AJAX request:

然后在AJAX请求中得到服务器的答案:

<script>
var username = <?php echo(json_encode($username)); ?>;

function checkAuthStatus() {
    setInterval(getStatus, 1000);
}

function getStatus() {
    var isAuth = <?php echo(json_encode($isAuth)); ?>;
    $.ajax({
        url: '<?php echo base_url('auth/listen_auth'); ?>',
        type: 'post',
        data: {username:username},
        success: function(data){
            document.getElementById("status").innerHTML = data;
        }
    });

}
</script>

#2


1  

You need to declare $isAuth and assign its value in ajax request, because php variable will not change its value without server request.

您需要声明$isAuth并在ajax请求中分配它的值,因为php变量在没有服务器请求的情况下不会改变它的值。

#1


1  

The function from server 'listen_auth()' should print text not return.

服务器“listen_auth()”中的函数应该不会返回文本。

public function listen_auth(){
    $username = $this->input->post('username');
    $isApproved = $this->adminmodel->get_auth($username);
    if($isApproved == 1){
        echo 1;
    } else{
        echo 0;
    }
}

And then get the answer from server in AJAX request:

然后在AJAX请求中得到服务器的答案:

<script>
var username = <?php echo(json_encode($username)); ?>;

function checkAuthStatus() {
    setInterval(getStatus, 1000);
}

function getStatus() {
    var isAuth = <?php echo(json_encode($isAuth)); ?>;
    $.ajax({
        url: '<?php echo base_url('auth/listen_auth'); ?>',
        type: 'post',
        data: {username:username},
        success: function(data){
            document.getElementById("status").innerHTML = data;
        }
    });

}
</script>

#2


1  

You need to declare $isAuth and assign its value in ajax request, because php variable will not change its value without server request.

您需要声明$isAuth并在ajax请求中分配它的值,因为php变量在没有服务器请求的情况下不会改变它的值。