我有一个PHP shell_exec命令,为每个结果输出5行数据,如果shell_exec
有1个结果,它将输出5行,如下所示:
john
richmond
27
london
dogs
该命令可能有超过1个结果,这是3个结果的示例:
john
richmond
27
london
dogs
dave
archibold
34
new york
cats
harry
harris
33
dublin
fish
正如您所看到的,每个结果都有5行,姓名,姓氏,年龄,城市和宠物。我希望创建一个带有结果的html表,但是因为每次结果的数量可能不同而且需要分组为5我不知道如何实现这一点。这是所需的HTML输出:
| Name | Surname | Age | City | Pet |
--------------------------------------------------
| john | richmond | 27 | london | dogs cat |
| dave | archibold | 34 | newyork | cats cow |
| harry | harris | 33 | dublin | fish horse |
根据我自己的研究,我认为我可能不得不使用php explode
并创建一个数组,我已经提出了以下内容,但是不确定如何实现foreach以及我需要使其工作的其他内容:
<div class="table-responsive">
<?php
$str = shell_exec( "shell command" );
$arr = explode(PHP_EOL, $str);
$arr = array_chunk($arr,5);
foreach *
?>
<table>
<thead>
<tr>
<th>Name</th>
<th>Surname</th>
<th>Age</th>
<th>City</th>
<th>Pet</th>
</tr>
</thead>
<tbody>
<?php foreach(*) { ?>
<tr class="1">
<td class="1">
<i class="cc <?php echo $name;?></i>
</td>
<td class="1">
<i class="cc <?php echo $surname;?></i>
</td>
<td class="1">
<i class="cc <?php echo $age;?></i>
</td>
<td class="1">
<i class="cc <?php echo $city;?></i>
</td>
<td class="1">
<i class="cc <?php echo $pet;?></i>
</td>
</tr>
</tbody>
</table>
</div>
使用array_chunk
,你确实已经拥有了你需要的大部分内容,你只需要预知你生成的数组,并使用一点list
魔法来分配你的变量。
简而言之,list
正在做的是逐个获取数组元素并将它们分配给您在list
语句中定义的变量。
<div class="table-responsive">
<?php
$array = array_chunk(
explode(
PHP_EOL,
shell_exec( "shell command" )
),
5
);
?>
<table>
<thead>
<tr>
<th>Name</th>
<th>Surname</th>
<th>Age</th>
<th>City</th>
<th>Pet</th>
</tr>
</thead>
<tbody>
<?php foreach($array as $element) { ?>
<?php list($name, $surname, $age, $city, $pet) = $element; ?>
<tr class="1">
<td class="1">
<i class="cc"><?php echo $name ?></i>
</td>
<td class="1">
<i class="cc"><?php echo $surname ?></i>
</td>
<td class="1">
<i class="cc"><?php echo $age ?></i>
</td>
<td class="1">
<i class="cc"><?php echo $city ?></i>
</td>
<td class="1">
<i class="cc"><?php echo $pet ?></i>
</td>
</tr>
<?php } ?>
</tbody>
</table>
</div>