如何辨别我的桌子是否空?

时间:2014-06-17 15:56:58

标签: php html html-table

我需要弄清楚如何知道我的表何时是空的,以便我可以显示错误消息而不是只显示一个空表。这是我的代码:

<?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 个答案:

答案 0 :(得分:1)

空和其他功能必须工作。在解析json对象后尝试在$ phpArray上调用它。如果它为空,它应该返回一个空数组,如下所示:

$jsonData = '[]';

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