2014-06-17 56 views
0

我需要弄清楚如何知道我的表何時爲空,以便我可以顯示錯誤消息而不是僅顯示空表。這裏是我的代碼:我怎麼知道我的桌子是空的?

<?php 

$search = $_REQUEST['search']; 
if ($search == "") { 
    echo "Please enter a query."; 
    break; 
} 
else { 
$data = array('key' => $API_KEY, 
       /*'consignorId' => '1',*/ 
       'query' => $search, 
       'includeItemsWithQuantityZero' => 'false'); 

$data_string = json_encode($data); 

$context = stream_context_create(array(
    'http' => array(
    'method' => "POST", 
    'header' => "Accept: application/json\r\n". 
      "Content-Type: application/json\r\n", 
    'content' => $data_string 
) 
)); 

$result = file_get_contents('https://user.traxia.com/app/api/inventory', false, $context); 

$jsonData = $result; 
$phpArray = json_decode($jsonData, true); 
$phpArray = $phpArray['results']; 
$mykeys = array('name','category','color','size','currentPrice'); 
} 
?> 
<html> 
<head> 
<style> 
body {background-image:url(http://theloftames.com/images/ColorboxBKG.jpg);} 
</style> 
<link rel="stylesheet" type="text/css" href="/test/css/search-results.css"> 
<script type="text/javascript" src="/test/js/tablesorter/jquery-latest.js"></script> 
<script type="text/javascript" src="/test/js/tablesorter/jquery.tablesorter.js"></script> 
<script> 
$(document).ready(function() 
    { 
     $("#myTable").tablesorter(); 
    } 
); 
</script> 
</head> 
<form action="/test/results.php" method="post"> 
<center><table> 
<tbody> 
<tr> 
<td> 
<input type="text" name="search" autofocus><br> 
</td> 
<td> 
<input type="submit"><br> 
</td> 
</tr> 
</tbody> 
</table> 
</center> 
</form> 
<div class="CSSTableGenerator" style="margin:auto;"> 
<table id="myTable" class="tablesorter"> 
    <thead> 
     <tr> 
      <?php 
     foreach($mykeys as $k) { 
      if ($k == "name") { 
       $k = "Name"; 
      } 
      if ($k == "sku") { 
       $k = "SKU"; 
      } 
      if ($k == "category") { 
       $k = "Category"; 
      } 
      if ($k == "color") { 
       $k = "Color"; 
      } 
      if ($k == "size") { 
       $k = "Size"; 
      } 
      if ($k == "currentPrice") { 
       $k = "Price"; 
      } 
      echo "<th style='cursor:pointer'>$k<img src='/test/images/UpDown.png' width='8px' height='auto' style='margin: 0px 20px'></th>"; 
     } 
     ?> 
     </tr> 
     </thead> 
     <tbody> 
     <?php 
     foreach($phpArray as $key => $values) { 
      if ($values['name'] == '') echo "<span style='color:red;'>No results found. Please try another search.</span>"; 
      if ($values['category'] == 'UNCATEGORIZED') continue; 
      if ($values['status'] == '') continue; 
      echo '<tr>'; 
      foreach($mykeys as $k) { 
       $value = $k == "currentPrice" ? '$' . number_format($values[$k]/100,'2') : $values[$k]; 
       echo "<td>" . $value . "</td>"; 
      } 
      echo '</tr>'; 
     } 
     ?> 
    </tbody> 
    </table> 
    </div> 
</html> 

我試過的東西,如isset()empty(),並且is_null(),但不起作用。我確信這很簡單,我只是想念它:P。謝謝!

回答

1

空等功能必須工作。嘗試在解析json對象後在$ phpArray上調用它。如果它是空的,它應該返回一個空的數組,如下所示:

$jsonData = '[]'; 

$encoded = json_decode($jsonData, true); //converts the json object to an array 
var_dump(empty($encoded)); 
->bool(true) 
+0

奇怪的是...我再次嘗試它,它工作...非常感謝! :P – bojo

相關問題