2017-02-01 2 views
0

테이블에서 값을 가져 오는 중입니다. 완벽하게 작동합니다. 테이블이 비어있을 때 aler box json이 오류를 발생시키고 있습니다. 어떻게 그 오류를 처리 할 수 ​​있습니까?테이블이 비어있는 경우 json 형식 오류를 처리하는 방법

내 SQL 쿼리를 PHP는 젠드 프레임 워크를

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으로 형식화하는 오류를

답변

0

액션을 처리하기 위해 도와주세요입니다 잘못된 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 
} 
} 
+0

내 코드에서 이것을 사용하는 방법, 당신은 내가 어디에 사용해야 설명해주십시오 수 있습니다 이 –

+0

행이없는 경우에만 테이블에 데이터가없는 경우에만이 오류가 발생합니다. 그래서이 json 형식 오류를 처리하는 방법. –

+0

도우미 클래스에 의해 반환 된 데이터 형식을 모르는 경우 빈 행 조건에서 $ oSelect가 null 인 경우 update :에서 위 코드를 사용하면 도우미에서 빈 행 조건을 처리 할 수 ​​있음 → DataTables – Raj