2014-01-08 50 views
0

數據顯示在頁面上後,我想在表格旁邊顯示記錄的數量。我怎麼做?下面是我的編碼:顯示數據的數量

</tr> 
<?php 
$xml = simplexml_load_file("write.xml"); 

echo $xml->getName(); 

foreach($xml->children() as $child) 
    { 
    echo "<tr>"; 
    echo "<td>"; 
    echo $child[0]->attributes(); 
    echo "</td>"; 

     foreach($child[0]->children() as $child2) 
     { 
     echo "<td>"; 
     echo $child2->getName() . ": " . $child2 ; 
     echo "</td>"; 
     } 

    echo "</tr>"; 
    } 

?> 

回答

1

嘗試添加計數器,並添加+1到每個孩子,然後輸出echo $count;,你將有你的計數,

$count = 0; 
foreach($xml->children() as $child) 
    { 
    $count++; 
    echo "<tr>"; 
    echo "<td>"; 
    echo $child[0]->attributes(); 
    echo "</td>"; 

     foreach($child[0]->children() as $child2) 
     { 
     echo "<td>"; 
     echo $child2->getName() . ": " . $child2 ; 
     echo "</td>"; 
     } 

    echo "</tr>"; 
    } 
+0

我得到了錯誤。請參閱我編輯的帖子。 @ user1381537 – Pony

+0

看到我的更新,我的答案有一個錯字,它應該是'$ count ++;'現在嘗試 –

0

這有什麼錯一個簡單的計數器?

$count = 0; 
foreach(...) { 
    ... display xml row 
    $count++; 
} 
echo "Total rows: $count"; 
0
$i = 0; 

foreach{ 
     //your code 
     echo $i++; 
}