0

我有疑问如何在 codeigniter 中获取结果查询,我需要获取结果查询的值以使用 json_encode 发送到 ajax。

这样的剧本。。

public function getPost()   
    { 
    $getCode = $_POST['part_code']; 
    $query = $this->db->query('SELECT count(*) + 1 as count FROM TB_TRANSACTION WHERE PART_CODE ='%$getCode%'');
    foreach ($query->result('TB_TRANSACTION') as $row)
    {
       echo $row->count; // call attributes
    }
    $phpVar = array("STATUS"=>$row->count); 
    echo json_encode ($status) ;    
    }

我的 Ajax 函数是这样的..

<script> function makeAjaxCall()
{ 
$.ajax({ 
        type: "post", 
        url: "http://localhost/IWOS_CI/trans_invent_controller/getPost", 
        cache: false,   
        data: $('#form1').serialize(), 
        success: function(json){    
try{    
        var obj = jQuery.parseJSON(json); 
        var r = obj['STATUS'];
}catch(e)
        {   
        alert('Exception while request..'); 
        }   
}, 
        error: function(){  
        alert('Error while request..'); 
} 
});
}

我在控制器而不是模型中创建函数。感谢您的帮助和关注。

4

1 回答 1

1

尝试这样,您将需要修改它以适合您的代码:

<script type="text/javascript">
function makeAjaxCall()
{ 
    $.ajax({ 
        type: "post", 
        url: "http://localhost/IWOS_CI/trans_invent_controller/getPost", 
        cache: false,   
        data: $('#form1').serialize(), 
        success: function(json){    
            if(json){
                var statusR = json.STATUS;
                alert( "status : "+statusR );
            }else{
                alert( "Error In JSON" );
            } 
        }, 
        error: function(){  
            alert('Error while request..'); 
        } 
    });
}

</script>
<?php
    function getPost()   
    { 
        $getCode    = $_POST['part_code']; 
        $query      = $this->db->query( "SELECT count(*) + 1 as count FROM videos WHERE id = '%$getCode%'" );
        //echo $this->db->last_query(); die;
        $queryData  = $query->row_array();
        $phpVar     = array( "STATUS" => $queryData['count'] ); 
        echo json_encode ( $phpVar ) ;    
    }
?>
于 2013-08-23T08:48:31.240 回答