2011-01-30 119 views
0

的foreach($ $作爲匹配匹配){。。回波匹配[1] 「 - 」 匹配[2];}如何將循環放入html表格?

match[1]match[2]值應與兩列顯示在一個HTML表格。例如

Name | Percent 
--------|----------- 
Mathew | 95% 

其中馬修是match[1]和百分比是match[2]

我如何添加HTML標籤之間,併爲您的信息大約有50名。

+0

你的匹配數組不能做你想要的東西。你從數據庫中拉出它們嗎? – borayeris 2011-01-31 21:36:30

回答

0
<table> 
<?php 
foreach($matches as $match) { 
?> 
<tr><td><?php echo $match[1]; ?></td> 
    <td><?php echo $match[2]; ?></td> 
</tr> 
<?php 
} 
?> 
</table> 
+0

但這是一個循環,所以有更多的行 – 2011-01-30 17:08:38

+0

除了忘記表頭,我沒有看到這是什麼錯誤...他們在循環中打印行... – 2011-01-30 17:23:51

5
<table> 
<tr> 
    <th>Name</th> 
    <th>Percent</th> 
</tr> 
<?php foreach($matches as $match): ?> 
    <tr> 
    <td><?php print $match[1]; ?></td> 
    <td><?php print $match[2]; ?></td> 
    </tr> 
<?php endforeach; ?> 
</table> 

要小心,你真的是比賽[1]和匹配[2]和不匹配[0]搭配[1] ...在PHP數組的第一個元素是0

0

你的匹配數組無法做你想要的東西。我想你是從數據庫中提取數據。

<?php 
$query = "SELECT * FROM `db_table` "; 
$result= mysql_result($query); 
?> 
<table> 
while($matches = mysql_fetch_assoc($result)){ 
?> 
     <tr> 
     <?php 
     foreach($matches AS $match) { 
     ?> 
     <td><?php echo $match[0]; ?></td> 
     <td><?php echo $match[1]; ?></td>   
     <?php 
     } 
     ?> 
     </tr> 

<?php 
} 
?> 
</table>