2014-11-23 53 views
2

我正在考虑一种方法来使td rowspans与相同的项目?..我的意思是如何将td rowspans添加到表中?

看看下面的这张表,这是如何输出看起来像,只是一个例子。

img

我怎样才能使输出这个样子的,所以在处理事情会容易..

img


这里代码

$get = file_get_contents('URL'); 
$json = json_decode($get, true); 

$items = $json['Catalogue'][0]['Accessories']; 
$quantity = $json['Accessories'][1]['Quantity']; 
$price = $json['Accessories'][2]['Price']; 
echo 
"<table border=1> 
    <tr> 
    <th width=200px>Items</th> 
    <th width=100px>Quantity</th>  
    <th width=100px>Price</th> 
    </tr> 
    <tr> 
    <td>".$items."</td> 
    <td>".$quantity."</td>  
    <td>".$price."</td> 
    </tr> 
</table>"; 
+0

你能不能给我们说,你正在使用的'$ json'阵列的一个更好的主意?它不会出现会有多于一个标题行和一个正文行。 – Scopey 2014-11-23 21:43:57

+0

这是一个来自服务器的api请求,所以更多的物品会将它们显示为json – fcbrmadrid 2014-11-23 22:01:19

+0

但是您需要说明如何执行此操作。在这一点上,你甚至没有在你的例子中有一个循环,所以我看不出你是如何创建表的。 – Scopey 2014-11-23 22:03:11

回答

2

你可以使用rowspan创建你喜欢的表。看下面的演示。

有两件事情需要注意,

  1. 添加rowspan="N"的起始行,你要显示TD。 N是要合并的行数
  2. 对于下一行N行不包括TD。合并行应该只有一个TDTD应该有rowspan

在呈现表格时,您需要在迭代中处理上述2条规则。

td, th { text-align: center; }
<table border="1"> 
 
    <tr> 
 
    <th style="width:200px">Items</th> 
 
    <th style="width:100px">Quantity</th>  
 
    <th style="width:100px">Price</th> 
 
    </tr> 
 
    <tr> 
 
    <td rowspan="3">Sports Watch</td> 
 
    <td>1</td>  
 
    <td>$89.99</td> 
 
    </tr> 
 
    <tr>  
 
    <td>2</td>  
 
    <td>$179.98</td> 
 
    </tr> 
 
    <tr>  
 
    <td>3</td>  
 
    <td>$267.97</td> 
 
    </tr> 
 
    <tr> 
 
    <td rowspan="2" style="text-align: center;">Portable Digital Camera</td> 
 
    <td>1</td>  
 
    <td>$196.99</td> 
 
    </tr> 
 
    <tr> 
 
    <td>2</td>  
 
    <td>$393.98</td> 
 
    </tr> 
 
</table>