我有幾個輸入,我從數據庫中的數組中獲取值。我需要將所有輸入作為一個塊重復,而不是重復 html 我試圖只編寫一次 html,但是不知道如何使用 foreach 獲取值。所以我有這樣的輸入:<input type="text" name="x[]" value="<?php echo $p;?>"><input type="text" name2="y[]" value="<?php echo $refarray;?>">etc.我需要填寫這樣的值:foreach ($x as $p){foreach ($y as $r){ ?>我想重復該塊 3 次,所以我在一個 while 循環中運行它 3 次:<?php$i = 0;$times_to_run = 3;while ($i++ < $times_to_run){ ?> <input type="text" name="x[]" value="<?php echo $p;?>"> <input type="text" name2="y[]" value="<?php echo $r;?>"><?php}?>這就是我希望打印輸出的方式:Block 1<input type="text" name="x[]" value="<?php echo $p[0];?>"><input type="text" name2="y[]" value="<?php echo $r[0];?>">Block 2<input type="text" name="x[]" value="<?php echo $p[1];?>"><input type="text" name2="y[]" value="<?php echo $r[1];?>">Block 3<input type="text" name="x[]" value="<?php echo $p[2];?>"><input type="text" name2="y[]" value="<?php echo $r[2];?>">如果不寫 html 3 次,我怎么能做到這一點?
1 回答

揚帆大魚
TA貢獻1799條經驗 獲得超9個贊
假設您有數組$x,$r鍵為 0,1,2,3....等。
使用 foreach 將很容易和干凈地與 html 綁定
foreach ($x as $key => $p){
echo '<input type="text" name="x[]" value="<?php echo $p;?>">';
echo '<input type="text" name="y[]" value="<?php echo $r[$key];?>">';
}
- 1 回答
- 0 關注
- 115 瀏覽
添加回答
舉報
0/150
提交
取消