Php 显示多维数组中的表数据
这是我的数组Php 显示多维数组中的表数据,php,arrays,Php,Arrays,这是我的数组 Array ( [0] => Array ( [0] => 1 [1] => 1 [2] => 1 [3] => 1 [4] => no ) [1] => Array ( [0] => 2 [1] =&g
Array
(
[0] => Array
(
[0] => 1
[1] => 1
[2] => 1
[3] => 1
[4] => no
)
[1] => Array
(
[0] => 2
[1] => 2
[2] => 2
[3] => 2
[4] => yes
)
[2] => Array
(
[0] => 3
[1] => 3
[2] => 3
[3] => 3
[4] => yes
)
[3] => Array
(
[0] => 4
[1] => 4
[2] => 4
[3] => 4
[4] => no
)
[4] => Array
(
[0] => 5
[1] => 5
[2] => 5
[3] => 5
[4] => yes
)
)
这就是我所尝试的,我正在创建一个tables tr部分,它所做的是为显示的每个记录复制它自己。还可以如何编写此文件以使其正确显示?我知道这一定很简单,我只是不能把我的头围绕着它
<?php
$units = json_decode($tablerow);
foreach($units as $unit) : for($i=0;$i<=count($unit)-1;$i++) : ?>
<tr class="rows">
<td><input type='text' name='bedroom[]' value='<?=$unit[$i];?>' class='input-small'></td>
<td><input type='text' name='bath[]' value='<?=$unit[$i];?>' class='input-small'></td>
<td><input type='text' name='sqrt[]' value='<?=$unit[$i];?>' class='input-small'></td>
<td><input type='text' name='price[]' value='<?=$unit[$i];?>' class='input-small'></td>
<td>
<select name='avail[]' class='input-small'>
<option value='yes' <?=($unit[$i] == 'yes') ? 'selected="selected' : '';?>>Yes</option>
<option value='no' <?=($unit[$i] == 'no') ? 'selected="selected' : '';?>>No</option>
</select>
</td>
<td><button type="button" class="btn btn-danger btn-small removeRow">remove</button></td>
</tr>
<?php endfor; endforeach; ?>
>对
我想你不需要循环,只要通过索引引用每个$unit
数组的元素即可:
foreach($units as $unit) :?>
<tr class="rows">
<td><input type='text' name='bedroom[]' value='<?=$unit[0];?>' class='input-small'></td>
<td><input type='text' name='bath[]' value='<?=$unit[1];?>' class='input-small'></td>
<td><input type='text' name='sqrt[]' value='<?=$unit[2];?>' class='input-small'></td>
<td><input type='text' name='price[]' value='<?=$unit[3];?>' class='input-small'></td>
<td>
<select name='avail[]' class='input-small'>
<option value='yes' <?=($unit[4] == 'yes') ? 'selected="selected' : '';?>>Yes</option>
<option value='no' <?=($unit[4] == 'no') ? 'selected="selected' : '';?>>No</option>
</select>
</td>
<td><button type="button" class="btn btn-danger btn-small removeRow">remove</button></td>
</tr>
<?php endforeach; ?>
foreach($units作为$unit):?>
>对
你可以用两个泡泡把这个循环的小东西泡起来
只需取下前刀,取出内刀。放下$i
并将$units
替换为$unit
。我也看到了
例如:
$units = json_decode($tablerow);
foreach ($units as $unit) : ?>
<tr class="rows">
<td><input type='text' name='bedroom[]' value='<?=$unit[0];?>' class='input-small'></td>
<td><input type='text' name='bath[]' value='<?=$unit[1];?>' class='input-small'></td>
...
非常感谢,我的变量是$unit,而不是整个变量的单位,我可能只是复制了一个旧版本,好眼力。而且我喜欢“你这个双倍泡泡的蠢蛋。”LMAO。感谢您的帖子注意不要允许HTML注入,例如内部属性值和内部标记值。这在你的例子中是完全缺失的,你应该一直注意这一点。是的,有趣的是:这是口香糖的名字吗?我当时在看食品工厂,看到了这个制造双泡泡的过程。黄色、粉色和蓝色包装,对吗?@hakre,是的,我使用mvc添加/显示数据,每个输入都被清除。谢谢
foreach ($units as $unit) {
$unit[4] = $unit[4] === 'yes';
$unit= array_map('intval', $unit); # escape all values to prevent injection
?>
...
<td>
<select name='avail[]' class='input-small'>
<option value='yes' <?= $unit[4] ? 'selected="selected' : '';?>>Yes</option>
<option value='no' <?= !$unit[4] ? 'selected="selected' : '';?>>No</option>
</select>
</td>
...
<? } ?>