Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/codeigniter/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 刷新页面时如何从控制器获取数据?_Php_Codeigniter - Fatal编程技术网

Php 刷新页面时如何从控制器获取数据?

Php 刷新页面时如何从控制器获取数据?,php,codeigniter,Php,Codeigniter,我正在创建一个仪表板,每次刷新页面时都需要在其中更新通知栏,以便在刷新页面时,必须执行控制器并将数据从数据库返回到该页面 最好的方法是什么?我尝试了一些关于onload事件的东西,但是我似乎找不到一种方法来进入控制器 目前我正在尝试: Jquery/Ajax: $( document ).ready(function() { $.ajax({ type: 'POST', url:'<?=base_url('notificacoes') ?>' }).do

我正在创建一个仪表板,每次刷新页面时都需要在其中更新通知栏,以便在刷新页面时,必须执行控制器并将数据从数据库返回到该页面

最好的方法是什么?我尝试了一些关于onload事件的东西,但是我似乎找不到一种方法来进入控制器

目前我正在尝试: Jquery/Ajax:

 $( document ).ready(function() {
   $.ajax({
    type: 'POST',
    url:'<?=base_url('notificacoes') ?>'
   }).done(function(e){
    console.log(e);
    $('#notbadge').html(e);
   }) 
});
$(文档).ready(函数(){
$.ajax({
键入:“POST”,
url:“”
}).完成(功能(e){
控制台日志(e);
$('#notbadge').html(e);
}) 
});
控制器:

<?php

    defined('BASEPATH') OR exit('No direct script access allowed');

    class Notificacoes extends CI_Controller {
        function __construct(){
            parent::__construct();
            $this->load->model('Notificacoes', '', TRUE);
            $this->load->model('Viatura', '', TRUE);
            $this->load->library('session');
        }

        public function index(){
            $matricula = $this->session->viatura;
            $viatura = $this->Viatura_model->read($matricula);

            return $viatura;

        }
    }

要获得通知引用,每次刷新页面不是一个好主意,您应该使用javascript或angular js。试试这样的

$scope.getNotifications = function () {
        $.ajax({
            url: "<?php echo base_url() . "dashboard/getNotifications" ?>",
            type: 'POST',
            dataType: 'json',
            success: function (data, textStatus, jqXHR) {
                //console.log(data);
                $scope.notifications = data;
                $scope.total_unread_noti = data.total_unread_noti;
                $scope.$apply();
            }

        });
    };
    $scope.getNotifications();


    setInterval(function () {
        $scope.getNotifications();
    }, 3000);
$scope.getNotifications=函数(){
$.ajax({
url:“”,
键入:“POST”,
数据类型:“json”,
成功:函数(数据、文本状态、jqXHR){
//控制台日志(数据);
$scope.notifications=数据;
$scope.total\u unread\u noti=data.total\u unread\u noti;
$scope.$apply();
}
});
};
$scope.getNotifications();
setInterval(函数(){
$scope.getNotifications();
}, 3000);

谢谢你的回答。你能解释一下你使用的ajax代码吗?我不熟悉Ajax,你能发布一个代码示例和你尝试过的东西吗?更新了,请检查
$scope.getNotifications = function () {
        $.ajax({
            url: "<?php echo base_url() . "dashboard/getNotifications" ?>",
            type: 'POST',
            dataType: 'json',
            success: function (data, textStatus, jqXHR) {
                //console.log(data);
                $scope.notifications = data;
                $scope.total_unread_noti = data.total_unread_noti;
                $scope.$apply();
            }

        });
    };
    $scope.getNotifications();


    setInterval(function () {
        $scope.getNotifications();
    }, 3000);