2014-11-23 109 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>