li标签中的内嵌输入字段

时间:2016-02-15 07:06:26

标签: php

我想使用implode显示输入:

enter image description here

我的代码:

$qty = array('2', '2', '1');
$price = array('200', '400', '1000');
$subtotal = array('400', '800', '1000');
echo "<li>
Quantity: <input type='text' class='qty' name='qty' value='".implode("'>
Quantity: <input type='text' class='qty' name='qty'   value='",$qty)."'>
Price: <input type='text' class='price' name='price' value='".implode("'>
Price: <input type='text' class='price' name='price' value='",$price)."'>
Subtotal: <input type='text' class='subtot' name='subtot' value='".implode("'>
Subtotal: <input type='text' class='subtot' name='subtot' value='",$subtotal)."'>
</li>";

我的代码已经给出了结果:

enter image description here

编辑:

我希望我的代码格式应该是这样的:

<li>
    Quantity: <input ..>
    Price: <input ..>
    Subtotal: <input ..>
<li>
<li>
    Quantity: <input ..>
    Price: <input ..>
    Subtotal: <input ..>
<li>
<li>
    Quantity: <input ..>
    Price: <input ..>
    Subtotal: <input ..>
<li>

...

3 个答案:

答案 0 :(得分:1)

你需要添加一个循环来按顺序添加li元素。尝试类似下面的内容。以下是使用相同长度的所有数组的想法。

$qty = array('2', '2', '1');
$price = array('200', '400', '1000');
$subtotal = array('400', '800', '1000');
for($i = 0; $i < count($qty); $i++) {
    echo "<li>
      Quantity: <input type='text' class='qty' name='qty' value='".$qty[$i]."'>
      Price: <input type='text' class='price' name='price' value='".$price[$i]."'>
      Subtotal: <input type='text' class='subtot' name='subtot' value='".$subtotal[$i]."'>
    </li>";
}

使用foreach示例更新:

$qty = array('2', '2', '1');
$price = array('200', '400', '1000');
$subtotal = array('400', '800', '1000');
foreach($qty as $key=>$value) {
    echo "<li>
    Quantity: <input type='text' class='qty' name='qty' value='".$qty[$key]."'>
    Price: <input type='text' class='price' name='price' value='".$price[$key]."'>
    Subtotal: <input type='text' class='subtot' name='subtot' value='".$subtotal[$key]."'>
    </li>";
}

答案 1 :(得分:1)

我们开始,这是一个非常简单的工作,你只需要通过任何一个数组,之后只需一次迭代打印/回显整数李的数量,价格,小计。

新脚本

<?php
$qty = array('2', '2', '1');
$price = array('200', '400', '1000');
$subtotal = array('400', '800', '1000');
for($i = 0; $i < count($qty); $i++){?>
    <li>
        Quantity: <input type='text' class='qty' name='qty' value='<?php echo $qty[$i];?>'/>
        Price: <input type='text' class='price' name='price' value='<?php echo $price[$i];?>'/>
        Subtotal: <input type='text' class='subtot' name='subtot' value='<?php echo $subtotal[$i];?>'/>
</li>
<?php }?>

使用foreach的脚本

<?php
$qty = array('2', '2', '1');
$price = array('200', '400', '1000');
$subtotal = array('400', '800', '1000');
foreach($qty as $key => $value){?>
    <li>
        Quantity: <input type='text' class='qty' name='qty' value='<?php echo $qty[$key];?>'/>
        Price: <input type='text' class='price' name='price' value='<?php echo $price[$key];?>'/>
        Subtotal: <input type='text' class='subtot' name='subtot' value='<?php echo $subtotal[$key];?>'/>
</li>
<?php }?>

答案 2 :(得分:0)

如果你在php($ _ POST)中提交它已经是一个Array(),你不必再破坏这个了。