我有问题如何在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..');
}
});
}
我在控制器而不是模型中创建函数。 感谢您的帮助和关注。
答案 0 :(得分: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 ) ;
}
?>