簡體   English   中英

Codeigniter - 使用json將控制器數據發送到視圖

[英]Codeigniter - send controller data to view with json

您好我想將控制器數據發送到json查看,但我被阻止了。

我的模型(contrat_model):

function presence($id_personnel,$debut,$fin){    
        $presence='';    
        $query="select statut from presence where id_employee=$id_employee and (work_date>=$debut and work_date<$fin)";
        $presence=$this->db->query($query);
        return $presence;
}

我的控制器(contrat_controller):

public function presence($id_employee,$debut,$fin){
       $presence=$this->contrat_model->presence($id_employee,$debut,$fin);
       $i=0;
       $all=0;
       foreach ($presence as $row){
            $statut=$row;
            $all++;
            if($statut=="green"){
                $i++;
            }
        }

        $data=array("i"=>$i, "all"=>$all);
        echo json_encode($data); 
}

我的觀點(合同):

function afficher(id_personnel,debut,fin)
{
        $.ajax({
        url : "<?php echo site_url('Contrat_controller/presence')?>/"+id_personnel+"/"+debut+"/"+fin,       
        type: "GET",
        dataType: "JSON",
        success: function(data)
        {
            alert(data.i);
        },
        error: function (jqXHR, textStatus, errorThrown)
        {
            alert('Erreur d\'affichage du graphe'); 
        }
    });
}

您可以參考以下鏈接中提到的答案中的javascript代碼:

通過POST(ajax)發送JSON數據並從Controller(MVC)接收json響應

希望這有助於你。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM