2016-11-11 37 views
-1

我有簡單的數據庫查詢,它會輸出組國家代碼和數據庫中的實例數。我試圖用PHP在表格中顯示這些數據。PHP在HTML - 顯示錶

查看頁面時,沒有任何內容會顯示在<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 > 
+2

可以顯示數據庫查詢+的代碼正確的參數$ cn_count可變分配 – erwan

+0

var_dump'ed $ cn_count ['結果']? –

+1

另外添加:ini_set('display_errors','on');在你的文件的頂部,併發布輸出,請。 – deChristo

回答

0

既然你不進去tbody任何東西,它是安全的,說你的問題是在這條線:

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

由於$x已正確初始化,您遇到問題$cn_count。它可以是:

  • 沒有初始化
  • 不是count
+1

他爲什麼會得到反對票。這似乎是其中一個建議是原因。你有沒有print_r $ cn_count ['結果']看看你得到了什麼樣的結果? –

+0

問題在於$ cn_count未初始化。 $ cn_count是查詢遊標,我沒有使用Iterator倒帶。 –