PHP,Array以序列形式生成表格式

时间:2015-04-19 19:30:08

标签: php html-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>';
}

我也得到一张桌子,但我想在一个序列中计算#个结果,因为现在我的结果显示在多个只包含一行的表中。

我得到的结果是:

http://prntscr.com/6vrjlg

2 个答案:

答案 0 :(得分:2)

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>';
}

你需要在循环外打印表格,所以只有相关的行才会重复。

答案 1 :(得分:1)

刚刚测试了第一个和第二个答案(两个人都会得到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

我希望你能得到它的需要。