gpt4 book ai didi

php - 如果表为空,如何处理 json 格式错误显示

转载 作者:行者123 更新时间:2023-11-29 19:28:22 25 4
gpt4 key购买 nike

我正在从表中获取值,它工作得很好。当表为空时,我收到警报框 json 格式错误。我该如何处理该错误?

我的sql查询如下,我使用php zend框架

public function getProductsAction(){

$oProductModel = new Application_Model_Db_Table_Products();
$oSelect = $oProductModel->fetchAllProductItems();

echo Zend_Json::encode($this->_helper->DataTables($oSelect, array('product_id','e.ename as employee_name','name','brand','conditions','about','image_path','reserved_price','Max(b.bid_amount) as amount')));

}

查看页面是

$(document).ready(function(){

jQuery('#product-table').dataTable({
<?php if(Qsm_User::isAdmin()){ ?>
"sDom": 'Tlfrtip',
"oTableTools": {
"sSwfPath": "/js/DataTables/extras/TableTools/media/swf/copy_csv_xls_pdf.swf",
"aButtons": [{
"sExtends": "collection",
"sButtonText": 'Save <span class="caret" />',
"aButtons": [ "csv", "xls", "pdf" ]
}]
},
<?php } ?>
"sPaginationType": "full_numbers",
"bProcessing": true,
"bServerSide": false,
"sAjaxSource": '/buyproduct/api/get-products',

"aoColumns": [
{"sTitle":"ID", "bVisible":false},
{"sTitle":"Emp Name", "bVisible":true},
{"sTitle":"Product", "sWidth": '10%'},
{"sTitle":"Brand", "sWidth": '10%'},
{"sTitle":"Condition", "sWidth": '10%'},
{"sTitle":"Description", "sWidth": '20%'},
{"sTitle":"Image","sWidth": '18%',
"mData": null,
"bSearchable": false,
"bSortable": false,
"fnRender": function (oObj){
var actions = "";
actions += "<span class=''>\n\
<img src='/uploads/gallery/" + oObj.aData[6] + "' alt='Image' title='Image' class='style_prevu_kit' width='0px' height='0px' />";
actions +="</span>";
return actions;
}
},
{"sTitle":"Starting Bid Price", "sWidth": '10%'},
{"sTitle":"Current Bid Price", "sWidth": '10%'},
{
"sTitle":"Bid",
"sWidth": '17%',
"mData": null,
"bSearchable": false,
"bSortable": false,
"fnRender": function (oObj){
var actions = "";
actions += "<span class='data_actions iconsweet'>\n\
<a title='Buy' href='/buyproduct/index/bid/id/" + oObj.aData[0] + "'><img src='/images/buy.png' alt='Buy' title='Buy' /></a>";
actions +="</span>";
return actions;
}
}
<?php if(Qsm_User::isAdmin()){ ?>
,{
"sTitle":"Transaction",
"sWidth": '22%',
"mData": null,
"bSearchable": false,
"bSortable": false,
"fnRender": function (oObj){
var actions1 = "";
actions1 += "<span class='data_actions iconsweet'>\n\
<a title='Transaction' href='/buyproduct/index/transaction/id/" + oObj.aData[0] + "'><img src='/images/icons/edit3.png' alt='Edit' title='Transaction' /></a>";

actions1 +="</span>";
return actions1;
}
}
<?php } ?>
]
});

});

请有人帮我处理这个 json 格式错误

最佳答案

如果没有行条件,您的操作将返回空字符串,这是无效的 json 数据,请检查返回的值

$this->_helper->DataTables($oSelect, array('product_id','e.ename as employee_name','name','brand','conditions','about','image_path','reserved_price','Max(b.bid_amount) as amount'))

如果为空/或没有行回显类似这样的内容或空行数据

{
"msg": "no-data"
}

更新:

public function getProductsAction(){

$oProductModel = new Application_Model_Db_Table_Products();
$oSelect = $oProductModel->fetchAllProductItems();
if($oSelect){
echo Zend_Json::encode($this->_helper->DataTables($oSelect, array('product_id','e.ename as employee_name','name','brand','conditions','about','image_path','reserved_price','Max(b.bid_amount) as amount')));
}else{
echo '{"msg": "no-data"}'; //or edit with empty data matching required by your view
}
}

关于php - 如果表为空,如何处理 json 格式错误显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41983225/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com