【发布时间】:2018-03-12 02:38:30
【问题描述】:
我使用 simple-php-dom 从其他网站获取一些 html。 然后我使用 foreach 查找 3 件事:公司名称、地址和电话。 最后,我想使用 php 循环在我的表中打印这 3 个信息。
问题:如何在表格内写循环?
这是我从其他网站获取 3 信息的 PHP 代码:
<?php
//Get Biz Name
foreach($html->find('a[class=biz-name js-analytics-click]') as $biz_name_root){
foreach ($biz_name_root->find('span') as $biz_name) {
echo $biz_name->plaintext . "<br>";
}
}
//Get Address
foreach($html->find('address') as $address){
echo $address . '<br>';
}
// Get Phone
foreach($html->find('span[class=biz-phone]') as $phone){
echo $phone . '<br>';
}
?>
这是我要将这 3 个信息存储到的表:
我想将$biz_name->plaintext 存储到<td>biz_name</td>,将$address 存储到
<td>address</td> 和 $phone 到 <td>phone</td>field。
<table class="table">
<thead class="thead-dark">
<tr>
<th scope="col">No</th>
<th scope="col">Biz Name</th>
<th scope="col">Address</th>
<th scope="col">Phone</th>
</tr>
</thead>
<tbody>
<?php
for ($i=1; $i < count($html->find('address'))+2 ; $i++) { ?>
<tr>
<th scope="row"><?php echo $i ?></th>
<td>biz_name</td>
<td>address</td>
<td>phone</td>
</tr>
<?php }
?>
</tbody>
</table>
【问题讨论】: