我想在html表的每一行显示数据,我该怎么做?
<?php
$test = $object2->schoolname;
$array = explode(',', $test);
echo $array[0];
//print_r($myArray); --> output: Array( [0] => testSchool2 [1] => test)
?>
我能想到的唯一方法是使用这样的东西:
<td>
<input type="text" name='schoolname[]' value="<?php
$test = $object2->schoolname;
$array = explode(',', $test);
echo $array[0];
?>" class="form-control"/>
</td>
但我必须为每一行做这件事,我想表明如果我在数组中有2个数据,那么2行必须包含那些数据,我不想手动放入数组[0 ] 像这样。我怎样才能做到这一点?谁能帮我?非常感谢。
我的html表基于此: https://bootsnipp.com/snippets/featured/dynamic-table-row-creation-and-deletion
答案 0 :(得分:1)
答案是循环。
foreach($array as $i => $value) {
//code for each array item, as $value
}
例如
$test = $object2->schoolname;
$array = explode(',', $test);
foreach($array as $i => $value) {
echo "
<tr>
<td>
<input type='text' name='schoolname[".$i."]' value='".$value."' class='form-control'/>
</td>
</tr>
";
}
php中有几种不同类型的循环; for,foreach,while and do-while
答案 1 :(得分:0)
这是一个非常基本的foreach循环:
<table>
<?php foreach ($array as $value): ?>
<tr><td><?= $value ?></td></tr>
<?php endforeach; ?>
</table>