PHP,序列中 table 格式的数组结果

PHP, Array result in table format in a sequence

我正在使用下面的代码在 table、

中获取数组结果
$presents = @json_decode(fBGetDataStore('presents'), true);
foreach ($presents as $key=>$value){
     $icon = $_SESSION['data']->table('units')->byCode($value['itemCode'])->data();   
     $i = 1;
echo '<table border=1 class="sortable" align="center">
<tr>
<td class="sortable" align="center">#</td>
<td class="sortable" align="center">Sender IMG</td>
<td class="sortable" align="center">Sender ID</td>
<td class="sortable" align="center">Item Code</td>
<td class="sortable" align="center">IMG</td>
</tr>';
    echo '<tr><td align="center">'.$i++.'</td>';
    echo '<td>&nbsp;<img src="http://graph.facebook.com/'. $value['sender'] .'/picture" width="70" height="50"&nbsp;</td>';
    echo '<td align="center">'. $value['sender'] . '</td>';
    echo '<td align="center">'. $value['itemCode'] . '</td>';
    echo '<td><img src="http://static-0.farmville.zgncdn.com/assets/hashed/' . fBImageGetHash($icon['iconname']). '" width=45px height=45px ></td>' ;
    echo '</tr>';
}

而且我得到了一个 table 但我想按顺序计算 # 个结果,因为现在我的结果显示在多个 table 中其中仅包含一行。

我得到的结果是:

echo '<table border=1 class="sortable" align="center">
<tr>
<td class="sortable" align="center">#</td>
<td class="sortable" align="center">Sender IMG</td>
<td class="sortable" align="center">Sender ID</td>
<td class="sortable" align="center">Item Code</td>
<td class="sortable" align="center">IMG</td>
</tr>';

$presents = @json_decode(fBGetDataStore('presents'), true);
foreach ($presents as $key=>$value){
  $icon = $_SESSION['data']->table('units')->byCode($value['itemCode'])->data();   
  $i = 1;
  echo '<tr><td align="center">'.$i++.'</td>';
  echo '<td>&nbsp;<img src="http://graph.facebook.com/'. $value['sender'] .'/picture" width="70" height="50"&nbsp;</td>';
  echo '<td align="center">'. $value['sender'] . '</td>';
  echo '<td align="center">'. $value['itemCode'] . '</td>';
  echo '<td><img src="http://static-0.farmville.zgncdn.com/assets/hashed/' . fBImageGetHash($icon['iconname']). '" width=45px height=45px ></td>' ;
  echo '</tr>';
}

您需要在循环外打印 table,因此只有相关行会自己重复。

刚刚测试了第一个和第二个答案(通过它们你会得到 1 表示与 # 列相同的数字所以我的意见是你应该使用来计算行数:

$presents = @json_decode(fBGetDataStore('presents'), true);

$i = 1;
// Table starting 
echo '<table border=1 class="sortable" align="center">
<tr>
<td class="sortable" align="center">#</td>
<td class="sortable" align="center">Sender IMG</td>
<td class="sortable" align="center">Sender ID</td>
<td class="sortable" align="center">Item Code</td>
<td class="sortable" align="center">IMG</td>
</tr>';

foreach ($presents as $key=>$value){
   $icon = $_SESSION['data']->table('units')->byCode($value['itemCode'])->data();   

echo '<tr><td align="center">'.$i++.'</td>';
echo '<td>&nbsp;<img src="http://graph.facebook.com/'. $value['sender'] .'/picture" width="70" height="50"&nbsp;</td>';
echo '<td align="center">'. $value['sender'] . '</td>';
echo '<td align="center">'. $value['itemCode'] . '</td>';
echo '<td><img src="http://static-0.farmville.zgncdn.com/assets/hashed/' . fBImageGetHash($icon['iconname']). '" width=45px height=45px ></td>' ;
echo '</tr>';
}
echo '</table>'; //Table end

希望您能按需得到。