php中的html - 显示表

时间:2016-11-11 12:44:53

标签: php html arrays mongodb-query

我有简单的数据库查询,它将输出组国家/地区代码和数据库中的实例数。我正在尝试使用PHP在表格中显示这些数据。

查看页面时,没有任何内容显示在<tbody>标记之后。我已经搜索了如何正确运行PHP内联HTML,我没有看到我的错误。建议?

<table id="countryList" class="table table-striped">
     <thead>
         <tr>
             <th width="50%" class="tableheading">Country</th>
             <th width="50%" class="tableheading" style="text-align:center;">Count</th>
         </tr>
      </thead>
      <tbody>
      <?php
      for($x = 0; $x < count($cn_count['result']); $x++){
          echo "<tr>";
          echo "<td class='tablebody' title='Unkown'>".$cn_count['result'][$x]['_id']['cn']."</td>";
          echo "<td class='tablebody' style='text-align:center;'>".$cn_count['result'][$x]['count']."</td>";
          echo "</tr>";
          if($x == 14){
          break;
          }
        }?>
    </tbody>
</table>

mongoDB查询

$cn_count = $collection->aggregate([['$group' => ['_id' => ['cn' => '$cn'],'count'=>['$sum'=>1]]],['$sort' => ['count' => -1]]]);

查询返回预期的结果,只是看起来内联PHP没有在<tbody>标记之后运行。否则页面仍会显示其余内容。

---- ---- VarDump

    php > var_dump($cn_count);
object(MongoDB\Driver\Cursor)#11 (2) {
  ["cursor"]=>
  array(17) {
    ["stamp"]=>
    int(0)
    ["is_command"]=>
    bool(false)
    ["sent"]=>
    bool(true)
    ["done"]=>
    bool(false)
    ["end_of_event"]=>
    bool(false)
    ["in_exhaust"]=>
    bool(false)
    ["has_fields"]=>
    bool(false)
    ["query"]=>
    object(stdClass)#13 (0) {
    }
    ["fields"]=>
    object(stdClass)#10 (0) {
    }
    ["read_preference"]=>
    array(2) {
      ["mode"]=>
      int(1)
      ["tags"]=>
      array(0) {
      }
    }
    ["flags"]=>
    int(0)
    ["skip"]=>
    int(0)
    ["limit"]=>
    int(0)
    ["count"]=>
    int(2)
    ["batch_size"]=>
    int(0)
    ["ns"]=>
    string(9) "spam.mail"
    ["current_doc"]=>
    object(stdClass)#15 (2) {
      ["_id"]=>
      object(stdClass)#14 (1) {
        ["cn"]=>
        string(2) "NA"
      }
      ["count"]=>
      int(264)
    }
  }
  ["server_id"]=>
  int(1)
}
php >

1 个答案:

答案 0 :(得分:0)

由于tbody内部没有任何内容,因此可以肯定地说您的问题就在这一行:

for($x = 0; $x < count($cn_count['result']); $x++){

由于$x已正确初始化,因此$cn_count存在问题。它是:

  • 未初始化
  • 不是count
  • 的正确参数